Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doctrine-Table alias issue with CRUD #665

Open
imonteiro opened this issue Jan 18, 2013 · 1 comment
Open

Doctrine-Table alias issue with CRUD #665

imonteiro opened this issue Jan 18, 2013 · 1 comment

Comments

@imonteiro
Copy link
Member

Original author: [email protected] (March 29, 2010 14:15:22)

Create a doctrine query with a left join and specify certain fields in the
select. Then try to do an Edit action on one of the rows and it will fail.

I have found to solution to this, i just need to test it and commit it.

The ticket is purely a reminder for me about this issue.

Original issue: http://code.google.com/p/zfdatagrid/issues/detail?id=233

@imonteiro
Copy link
Member Author

From [email protected] on April 01, 2010 14:14:02
Hi,

Doctrine query definition:

$ab = Doctrine_Core::getTable('Model_UserAddressbook')
->createQuery('ab')
->leftJoin('ab.UserPersons up')
->leftJoin('ab.PersonsComms pc')
->leftJoin('pc.UserCommchannels cc')
->leftJoin('ab.AdminUsers au')

Only the last join table fields (AdminUsers) are used. Other join table fields
(UsersPerson, PersonsComm and UserCommChannels) are not present in select.

I think I found the problem in Doctrine.php, line 921, _setFromParts():

  • $this->_queryParts = array_merge($this->_queryParts, $this->_explodeFrom($field));
    } else {
    $join = explode('JOIN', $field);
    $join = array_map('trim', $join);

$joinType = strtolower($join[0]);

$this->_queryParts =

  • array_merge($this->_queryParts, $this->_explodeJoin($join[1], $joinType));

::_queryParts variable:

join Array [1]
left Array [1]

  • 0 Array [4]
    alias (string:2) pc
    tableModel (string:21) Model_UserPersonComms
    tableAlias (string:12) PersonsComms
    joinOn (string:2) ab

As you can see, this join has numeric key, and array_merge overwrites the previous
join. At the end, only the last join table has left.

My current solution for this issue is:

  1. Use array_merge_recursive,

  2. in _explodeJoin(), on line 1069 instead of
    $return['join'][strtolower($joinType)][] = array(
    use
    $return['join'][strtolower($joinType)][$alias] = array(

  3. in _findAndSetSelect(), on line 957 surround $this->_setFromParts(); with

    if (count($this->_queryParts['from']) == 0) {
    $this->_setFromParts();
    }

_findAndSetSelect() is calling _setFromParts() even though it is already set, and,
for some reason, the second pass throws Loader exception "Table.php" not found.

Please take a look, and I hope this workaround might help you to solve it.

With this "dirty" workaround, I get all the fields, no notices, all nice, except:

The query above, because of its left joins (hasMany), for some rows in Addressbook
returns several rows. Like

John email me@me
John mobile 2233
John home 4422 etc.

SQL and Doctrine query work correctly, but grid displays the incorrect number of
rows, both in pagination and on the screen.
Grid displays as there is just one "John", and not as in this case three.
In fact, grid displays the number of rows in main table (Addressbook), and not what
the query returns in reality.

Before this workaround, it worked correctly, but it is because all but last joined
tables fields were left out.

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

No branches or pull requests

1 participant