Skip to content

Commit 6fc5680

Browse files
committed
enhance where command array manipulation
Array of entities is replaced by array of entities' primary key values for IN clause in where command.
1 parent 6ce42a5 commit 6fc5680

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

LeanMapperQuery/Query.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -494,11 +494,12 @@ private function commandWhere($cond)
494494
$replacePlaceholders = TRUE;
495495
$field = &$args[0];
496496
list(, $field, $operator) = $matches;
497-
$value = $args[1];
497+
$value = &$args[1];
498498

499499
$placeholder = self::$defaultPlaceholder;
500500
if (!$operator) {
501501
if (is_array($value)) {
502+
$value = $this->replaceEntitiesForItsPrimaryKeyValues($value);
502503
$operator = 'IN';
503504
$placeholder = '%in';
504505
} elseif ($value === NULL) {
@@ -516,18 +517,24 @@ private function commandWhere($cond)
516517
$statement = &$args[0];
517518
$statement = $this->parseStatement($statement, $replacePlaceholders);
518519
$statement = "($statement)";
519-
// Replace instances of Entity for its values.
520-
foreach ($args as &$arg) {
521-
if ($arg instanceof LeanMapper\Entity) {
522-
$entityTable = $this->mapper->getTable(get_class($arg));
523-
$idField = $this->mapper->getEntityField($entityTable, $this->mapper->getPrimaryKey($entityTable));
524-
$arg = $arg->$idField;
525-
}
526-
}
520+
$args = $this->replaceEntitiesForItsPrimaryKeyValues($args);
527521
call_user_func_array(array($this->fluent, 'where'), $args);
528522
}
529523
}
530524

525+
private function replaceEntitiesForItsPrimaryKeyValues(array $entities)
526+
{
527+
foreach ($entities as &$entity) {
528+
if ($entity instanceof LeanMapper\Entity) {
529+
$entityTable = $this->mapper->getTable(get_class($entity));
530+
// FIXME: Column name could be specified in the entity instead of mapper provided by 'getEntityField' function.
531+
$idField = $this->mapper->getEntityField($entityTable, $this->mapper->getPrimaryKey($entityTable));
532+
$entity = $entity->$idField;
533+
}
534+
}
535+
return $entities;
536+
}
537+
531538
private function commandOrderBy($field)
532539
{
533540
if (is_array($field)) {

tests/LeanMapperQuery/Query.where.phpt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* @author Michal Bohuslávek
66
*/
77

8+
use LeanMapper;
89
use LeanMapper\Entity;
910
use Tester\Assert;
1011

@@ -125,3 +126,26 @@ $expected = getFluent('book')
125126
->where('([book_tag].[tag_id] = %i)', 2);
126127

127128
Assert::same($expected->_export('WHERE'), $fluent->_export('WHERE'));
129+
130+
////////////////
131+
132+
class TagRepository extends LeanMapper\Repository
133+
{
134+
public function findAll()
135+
{
136+
return $this->createEntities($this->createFluent()->fetchAll());
137+
}
138+
}
139+
140+
$tagRepository = new TagRepository($connection, $mapper, $entityFactory);
141+
$tags = $tagRepository->findAll();
142+
143+
$fluent = getFluent('book');
144+
getQuery()
145+
->where('@tags', $tags)
146+
->applyQuery($fluent, $mapper);
147+
148+
$expected = getFluent('book')
149+
->where('([book_tag].[tag_id] IN %in)', array(1 => 1, 2));
150+
151+
Assert::same($expected->_export('WHERE'), $fluent->_export('WHERE'));

0 commit comments

Comments
 (0)