Multiple identifiers in `In` #5854

Closed
wants to merge 9 commits into
from

Projects

None yet

4 participants

@ojhaujjwal

I have an SQL query like this:


SELECT * FROM data da
INNER JOIN device_configuration d ON d.id = da.device_id
WHERE (da.device_id, da.recorded_time) IN
(
SELECT device_id, MAX(recorded_time) FROM data group BY device_id
)
AND d.parent_id IS NULL;

Yes, you are right, it is not supported directly by Zend Framework 2.

With this PR, you can do something like:

$select->where->in(array('column1', 'column2'), $anotherSelect);

So, the above query can be written in Zend Framework 2 as:

$maxSelect->columns(array('device_id', new SqlExpression('MAX(recorded_time)')));
$maxSelect->group('device_id'); 
$select->where->in(array('device_id', 'recorded_time'), $maxSelect);
$select->join(
    'device_configuration',
    'device_configuration.id = data.device_id'
);
$select->where->isNull('parent_id');
$resultSet = $this->select($select); 
@Ocramius Ocramius and 1 other commented on an outdated diff Feb 21, 2014
library/Zend/Db/Sql/Predicate/In.php
if ($values instanceof Select) {
+ if (is_array($identifier)) {
+ $identifiers = $identifier;
@Ocramius Ocramius commented on an outdated diff Feb 21, 2014
library/Zend/Db/Sql/Predicate/In.php
if ($values instanceof Select) {
+ if (is_array($identifier)) {
+ $identifiers = $identifier;
+ $specification = ' ( ' . implode(', ', array_fill(0, count($identifiers), '%s')) . ' ) ';
+ $specification .= ' IN %s';
+ $values = array_merge($identifiers, array($values));
@Ocramius
Ocramius Feb 21, 2014

Why are you merging values and identifiers? Doesn't make sense to me

@ojhaujjwal

@Ocramius I have updated the code. I do not know why the build is failing?

@samsonasik

see travis build here https://travis-ci.org/zendframework/zf2/jobs/19323397 , there is trailing_spaces need to be fixed. you can fix trailing space by php-cs-fixer and run :

php php-cs-fixer.phar fix /path/to/file.php --fixers=trailing_spaces
@ojhaujjwal

@samsonasik I tried that. No luck!

@samsonasik

you need to define what "No luck" is. what error you found ? have you follow the Readme ? https://github.com/fabpot/PHP-CS-Fixer/blob/master/README.rst#usage

@ojhaujjwal

@samsonasik I did exactly the same command as you said. And, then I committed and it displayed that no change was detected. That is NO LUCK!

@ralphschindler ralphschindler and 1 other commented on an outdated diff Feb 27, 2014
library/Zend/Db/Sql/Predicate/In.php
@@ -23,8 +23,8 @@ class In implements PredicateInterface
/**
* Constructor
*
- * @param null|string $identifier
- * @param array $valueSet
+ * @param null|string $identifier
@ralphschindler
ralphschindler Feb 27, 2014

This would need to change to null|string|array, and the later line needs to change to array|Select, i think

@ojhaujjwal
ojhaujjwal Feb 28, 2014

@ralphschindler Thanks for pointing out that!

@ralphschindler ralphschindler added this to the 2.3.0 milestone Feb 27, 2014
@ralphschindler ralphschindler self-assigned this Feb 27, 2014
@ralphschindler ralphschindler added the Db label Feb 27, 2014
@ojhaujjwal

@samsonasik It was my mistake. I only run cs-fixer in In.php and forgot to run in InTest.php
Now, I did and it worked!

@ralphschindler
Zend Framework member

After looking at this, I think I'd like to find a way to accomplish this without a "dynamic" specification. Primarily, this usage pattern would break the NotIn predicate.

@ralphschindler ralphschindler added a commit to ralphschindler/zf2 that referenced this pull request Mar 5, 2014
@ralphschindler ralphschindler Merging #5854 to develop
Merge branch 'master' of git://github.com/ojhaujjwal/zf2 into feature/5854

* 'master' of git://github.com/ojhaujjwal/zf2:
  CS fixes
  Removed trailing spaces
  Update In.php
  CS fixes
  Update In.php
  Fixed alignment
  Removed trailing spaces
  Updated Tests of identifier array
  Now, identifier can also be array
525a30f
@ralphschindler
Zend Framework member

Hey @ojhaujjwal, the solution you proposed looks to have a mutually exclusive condition where when the value is select, the identifier is processed as an array. I've made a different PR that will treat the identifier and values separately when processing them for the expression data, it is located here:

https://github.com/ralphschindler/zf2/tree/feature/5854-alternative

Can you tell me if this works for you?
This also takes into account the NotIn predicate as well.

@ralphschindler ralphschindler added a commit that referenced this pull request Mar 7, 2014
@ralphschindler ralphschindler Closes #5854
Merge branch 'feature/5854-alternative' into develop

* feature/5854-alternative:
  Zend\Db\Sql\Predicate\In & NotIn * Changed specification so that it would work with NotIn * Modified the IN with select solution to include edge cases where the array of identifiers and select as a value are processed independent of one another.
  CS fixes
  Removed trailing spaces
  Update In.php
  CS fixes
  Update In.php
  Fixed alignment
  Removed trailing spaces
  Updated Tests of identifier array
  Now, identifier can also be array
2ed2ac7
@ralphschindler
Zend Framework member

Merged to develop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment