Skip to content

Commit

Permalink
release 0.2.0: orm::buildPDO()
Browse files Browse the repository at this point in the history
  • Loading branch information
rafageist committed Sep 19, 2019
1 parent 9f9cbd2 commit 688a70b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.idea
vendor/
vendor/
doc/
test/
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
September 18, 2019
- release 0.2.0 version
- New method for build a PDO instances, orm::buildPDO($config, $connectGlobal);
This code build PDO, and make global connection

```php
<?php

use divengine\orm;

orm::buildPDO([
'type' => 'pgsql',
'host' => 'localhost',
'port' => 5432,
'name' => 'mydb',
'user' => 'me',
'pass' => 'mysuperpass'
], true);
```

September 9, 2019
---------------
- release 0.1.0 version
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Div PHP Object Relational Mapping 0.1.0
# Div PHP Object Relational Mapping 0.2.0

This class allow to you make a mapping between your database objects and
your PHP objects.
Expand Down Expand Up @@ -61,8 +61,8 @@ Now look at an example of how to use your model:

use divengine\orm;

$pdo = new PDO();
orm::connectGlobal($pdo);
$pdo = new PDO(); // or use orm::buildPDO();
orm::connectGlobal($pdo); // or pass true to second param of orm::buildPDO()

$person = new Person(['name' => 'Peter']);
// $person::connect($pdo);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"DAL"
],
"homepage": "https://divengine.com/orm",
"version": "0.1.0",
"version": "0.2.0",
"require": {
"php": ">=7.1.0",
"ext-json": "*",
Expand Down
48 changes: 46 additions & 2 deletions src/orm.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,27 @@ public static function tryToSetProperty(
return $set;
}

/**
* Build PDO object
*
* @param mixed $data
*
* @param bool $connectGlobal
*
* @return PDO
*/
public static function buildPDO($data, $connectGlobal = false): PDO
{
$pdo = new PDO("{$data["type"]}:host={$data["host"]};port={$data["port"]};dbname={$data["name"]};", $data['user'], $data['pass']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

if ($connectGlobal) {
self::connectGlobal($pdo);
}

return $pdo;
}

/**
* Connect to database
*
Expand Down Expand Up @@ -732,6 +753,26 @@ public function addItem($obj, $index = null): void
}
}

/**
* Build a INSERT query
*
* @param $table
* @param $data
* @param null $returning
*
* @return string
*/
public function buildQueryInsert($table, $data, $returning = null)
{
$columns = array_keys($data);
$values = ':'.implode(",:", $columns);
$columns = implode(',', $columns);

$sql = "INSERT INTO {$table} ($columns) VALUES ($values) ".($returning === null ? "" : " RETURNING $returning ").";";

return $sql;
}

/**
* Add/insert object
*
Expand Down Expand Up @@ -784,6 +825,7 @@ public function insert($obj = null): ?array
$s_values = str_repeat('?,', $values_count - 1).'?';

// build the query
// TODO: build query with $this->buildQueryInsert
$sql = "INSERT INTO {$this->getMapName()} (".implode(',', array_keys($values)).") VALUES ($s_values) RETURNING *;";

$st = $this->db()->prepare($sql);
Expand Down Expand Up @@ -948,8 +990,9 @@ public function delete($filter = '', $params = [])
$paramsX = $this->getRawData();
$params = [];
foreach ($paramsX as $p => $v) {
if (stripos($filter,':'.$p) !== false)
if (stripos($filter, ':'.$p) !== false) {
$params[$p] = $v;
}
}
}

Expand Down Expand Up @@ -1133,7 +1176,8 @@ public function getRawData()
* @return bool
* @throws \ReflectionException
*/
public function exists($filters, $params, &$firstItem = null): bool {
public function exists($filters, $params, &$firstItem = null): bool
{

if ($this->__map_type === self::RECORD) {
// TODO: search inside record ?
Expand Down

0 comments on commit 688a70b

Please sign in to comment.