Skip to content

Commit 319261e

Browse files
committed
Better in select logic
1 parent b4a2476 commit 319261e

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

Tests/Unit/MiscellaneousTest.php

+15-2
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,28 @@ public function testInOperator() : void
2323
{
2424
$in = new \PHPFUI\ORM\Operator\In();
2525
$this->assertTrue($in->correctlyTyped([1, 2]));
26-
$this->assertFalse($in->correctlyTyped([]));
26+
$this->assertTrue($in->correctlyTyped([]));
2727
$this->assertFalse($in->correctlyTyped(1));
2828

2929
$notIn = new \PHPFUI\ORM\Operator\NotIn();
3030
$this->assertTrue($notIn->correctlyTyped([1, 2]));
31-
$this->assertFalse($notIn->correctlyTyped([]));
31+
$this->assertTrue($notIn->correctlyTyped([]));
3232
$this->assertFalse($notIn->correctlyTyped(1));
3333
}
3434

35+
public function testInSelectOperator() : void
36+
{
37+
$productTable = new \Tests\Fixtures\Table\Product();
38+
$productTable->addSelect('product_id');
39+
$productTable->setWhere(new \PHPFUI\ORM\Condition('product_name', '%dried%', new \PHPFUI\ORM\Operator\Like()));
40+
$orderDetailTable = new \Tests\Fixtures\Table\OrderDetail();
41+
$orderDetailTable->setWhere(new \PHPFUI\ORM\Condition('product_id', $productTable, new \PHPFUI\ORM\Operator\In()));
42+
43+
$this->assertCount(8, $orderDetailTable);
44+
$orderDetailTable->setWhere(new \PHPFUI\ORM\Condition('product_id', $productTable, new \PHPFUI\ORM\Operator\NotIn()));
45+
$this->assertCount(50, $orderDetailTable);
46+
}
47+
3548
public function testLiteralCondition() : void
3649
{
3750
$condition = new \PHPFUI\ORM\Condition('field', new \PHPFUI\ORM\Literal('invoiceItem.storeItemId'));

src/PHPFUI/ORM/Condition.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function __toString() : string
6767
$retVal .= "{$first}{$escapedField}{$operator} ";
6868
$first = ' ';
6969

70-
if (\is_array($value) || $value instanceof \PHPFUI\ORM\Table)
70+
if (\is_array($value))
7171
{
7272
$count = \count($value);
7373

@@ -80,6 +80,11 @@ public function __toString() : string
8080
$retVal = 'FALSE';
8181
}
8282
}
83+
elseif ($value instanceof \PHPFUI\ORM\Table)
84+
{
85+
$input = [];
86+
$retVal .= '(' . $value->getSelectSQL($input) . ')';
87+
}
8388
elseif (null !== $value)
8489
{
8590
$retVal .= '?';
@@ -167,9 +172,7 @@ public function getInput() : array
167172

168173
if ($value instanceof \PHPFUI\ORM\Table)
169174
{
170-
$input = [];
171-
$sql = $value->getSelectSQL($input);
172-
$retVal = \array_merge($retVal, \PHPFUI\ORM::getValueArray($sql, $input));
175+
$value->getSelectSQL($retVal);
173176
}
174177
elseif (\is_array($value))
175178
{

src/PHPFUI/ORM/Operator/In.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ public function __construct()
1111

1212
public function correctlyTyped(mixed $variable) : bool
1313
{
14-
return (\is_array($variable) && \count($variable)) || $variable instanceof \PHPFUI\ORM\Table;
14+
return \is_array($variable) || $variable instanceof \PHPFUI\ORM\Table;
1515
}
1616
}

src/PHPFUI/ORM/Operator/NotIn.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ public function __construct()
1111

1212
public function correctlyTyped(mixed $variable) : bool
1313
{
14-
return (\is_array($variable) && \count($variable)) || $variable instanceof \PHPFUI\ORM\Table;
14+
return \is_array($variable) || $variable instanceof \PHPFUI\ORM\Table;
1515
}
1616
}

0 commit comments

Comments
 (0)