2
votes

I'm using Doctrine ORM 1.2 and Symfony 1.4 and I want to create method in myUser class which will extend Doctrine_Query instance passed as an argument with some additional left joins and conditions. The trick is that I don't always want these these left joins to be made with root component of the query and I need to know with what table (record class) the alias corresponds - in some cases I would like to pass an alias of another component and expect it to be supplemented with some extra left joins and conditions.

My question is how can I check what component (essentially table) represents given alias? Suppose I create a Doctrine_Query instance:

$query = Doctrine_Query::create();
$query->from('Folder f')->leftJoin('f.ChildFolders cf');

Now I want my method in myUser class to add some joins to the table with alias 'cf' but varying depending on table which 'cf' aliases. I want to call it this way:

$this->getUser()->limitQueryResultsWithSomeCondition($query, 'cf');

That should return query with additional elements. How can I find out what table is symbolized by 'cf' alias and whether it exists at all in given query?

It is quite straightforward when it is root table of a query that needs to be extended, but I cannot find a way to do it in other cases.

1

1 Answers

2
votes

I guess I found my own solution to the problem. To find out to which table/record given alias corresponds one has to use getQueryComponent method. Suppose the query is same as in question above. The solution then reads as follows:

$alias = 'cf';
$query->getSqlQuery();
$component = $query->getQueryComponent($alias);
$recordClass = get_class($component['table']->getRecordInstance());

The trick is that before method getSqlQuery (or some method which is called inside of this method) is called the component will not be found and exception will be thrown.