Skip to content

Commit

Permalink
Merge 84b2acc into ecf49a5
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed May 27, 2017
2 parents ecf49a5 + 84b2acc commit 249b4d9
Show file tree
Hide file tree
Showing 15 changed files with 671 additions and 264 deletions.
69 changes: 60 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ Get a collection using the query object:

```php
<?php
$query = new \ByJG\MicroOrm\Query();
$query->table('users')
$query = \ByJG\MicroOrm\Query::getInstance()
->table('users')
->fields(['id', 'name'])
->where('name like :part', ['part' => 'A%']);

Expand All @@ -89,8 +89,8 @@ Returning multiples entities with a query:

```php
<?php
$query = new \ByJG\MicroOrm\Query();
$query->table('order')
$query = \ByJG\MicroOrm\Query::getInstance()
->table('order')
->join('item', 'order.id = item.orderid')
->where('name like :part', ['part' => 'A%']);

Expand All @@ -108,7 +108,58 @@ $collection = $orderRepository->getByQuery(
);
```

#### Tables with no AutoIncrements fields
#### Using FieldAlias

Field alias is an alternate name for a field. This is usefull for disambiguation on join and leftjoin queries.
Imagine in the example above if both tables ITEM and ORDER have the same field called 'ID'.

In that scenario, the value of ID will be overriden. The solution is use the FieldAlias like below:

```php
<?php
// Create the Mapper and the proper fieldAlias
$orderMapper = new \ByJG\MicroOrm\Mapper(...);
$orderMapper->addFieldAlias('id', 'orderid');
$itemMapper = new \ByJG\MicroOrm\Mapper(...);
$itemMapper->addFieldAlias('id', 'itemid');

$query = \ByJG\MicroOrm\Query::getInstance()
->fields([
'order.id as orderid',
'item.id as itemid',
/* Other fields here */
])
->table('order')
->join('item', 'order.id = item.orderid')
->where('name like :part', ['part' => 'A%']);

// Will return a collection of Orders and Items:
// $collection = [
// [ $order, $item ],
// [ $order, $item ],
// ...
// ];
$collection = $orderRepository->getByQuery(
$query,
[
$itemRepository->getMapper()
]
);
```

You can also add a MAPPER as a Field. In that case the MAPPER will create the field and the correct aliases.

```php
<?php
$query = \ByJG\MicroOrm\Query::getInstance()
->fields([
$orderRepository->getMapper(),
$itemRepository->getMapper,
]);
```


#### Tables without auto increments fields

```php
<?php
Expand Down Expand Up @@ -136,22 +187,22 @@ $mapper->addFieldMap(
// Update Closure
// Returns the field value with a pre-processed function before UPDATE
// If sets to NULL this field will never be updated/inserted
function ($field) {
function ($field, $instance) {
return $field;
},
// Select Closure
// Returns the field value with a post-processed value AFTER query from DB
function ($field) {
function ($field, $instance) {
return $field;
}
)
);
```



## Install

Just type: `composer require "byjg/micro-orm=1.1.*"`
Just type: `composer require "byjg/micro-orm=2.0.*"`

## Running Tests

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "byjg/micro-orm",
"description": "A micro framework for create a very simple decoupled ORM",
"description": "A micro framework for create a very simple decoupled ORM (sqlite, mysql, postgres, sqlserver)",
"authors": [
{
"name": "João Gilberto Magalhães",
Expand Down
38 changes: 32 additions & 6 deletions src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
class Mapper
{

protected $entity;
protected $table;
protected $primaryKey;
protected $keygenFunction = null;

const FIELDMAP_FIELD = 'fieldname';
const FIELDMAP_UPDATEMASK = 'updatemask';
const FIELDMAP_SELECTMASK = 'selectmask';

protected $fieldMap = [];
private $entity;
private $table;
private $primaryKey;
private $keygenFunction = null;
private $fieldMap = [];
private $fieldAlias = [];

/**
* Mapper constructor.
Expand Down Expand Up @@ -72,6 +72,15 @@ public function addFieldMap($property, $fieldName, $updateMask = false, \Closure
return $this;
}

/**
* @param $fieldName
* @param $alias
*/
public function addFieldAlias($fieldName, $alias)
{
$this->fieldAlias[$fieldName] = $alias;
}

/**
* @return string
*/
Expand Down Expand Up @@ -108,6 +117,10 @@ public function getFieldMap($property = null, $key = null)
return $this->fieldMap;
}

if (!isset($this->fieldMap[$property])) {
return null;
}

$fieldMap = $this->fieldMap[$property];

if (empty($key)) {
Expand All @@ -117,6 +130,19 @@ public function getFieldMap($property = null, $key = null)
return $fieldMap[$key];
}

public function getFieldAlias($fieldName = null)
{
if (empty($fieldName)) {
return $this->fieldAlias;
}

if (!isset($this->fieldAlias[$fieldName])) {
return null;
}

return $this->fieldAlias[$fieldName];
}

/**
* @return mixed|null
*/
Expand Down
39 changes: 39 additions & 0 deletions src/ORMHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* User: jg
* Date: 19/05/17
* Time: 16:54
*/

namespace ByJG\MicroOrm;


class ORMHelper
{
public static function processLiteral($sql, &$params)
{
if (!is_array($params)) {
return $sql;
}

foreach ($params as $field => $param) {
if ($param instanceof Literal) {
$literalValue = $param->getLiteralValue();
$sql = preg_replace(
[
"/\\[\\[$field\\]\\]/",
"/:$field([^\\d\\w]|$)/"
],
[
$literalValue,
"$literalValue\$1"
],
$sql
);
unset($params[$field]);
}
}

return $sql;
}
}

0 comments on commit 249b4d9

Please sign in to comment.