views:

15

answers:

2

I have a Doctrine_Collection object that was created with code that looks something like.

$collection = Doctrine_Query::create()
->from('FooBazBar')
->where('widget_id = ?',$some_var)
->execute();

Short of writing it down somewhere, is it possible to to retrieve the where clause that was used to create the collection?

That is, I want to be able to so something like

$collection->getWhereClauses(); //fake method

And get back the where clause in some form.

A definitive "no, this isn't exposed via the API" with an explanation is a perfectly reasonable answer here.

A: 

getSqlQuery() looks to be the method you're after...

http://www.doctrine-project.org/documentation/manual/1_0/en/dql-doctrine-query-language:debugging-queries

tsgrasser
That will let me get the sql for a **query** object. I have a collection that comes from calling exectue on the query object. I want to get the original sql used to generate the query.
Alan Storm
That's the point though, don't call execute() Just build the DQL and then var_dump() from a getSqlQuery call. That gives you the query as it will be executed.
tsgrasser
Right, but you're assuming I'm the one in controller of for the Doctrine_Collection gets instantiated. I appreciate the info though!
Alan Storm
+1  A: 

No, this isn't exposed via the API.

More seriously, you can't retrieve the query that generated the Doctrine_Collection. The easy way to do this is to create a method in your table, like :

//FooTable.php
public function findByWidgetQuery()
{
return $this->createQuery('foo')
  ->where('foo.baz = ?', 'bar');
}

And then you can use getDqlPart() like this :

$where = Doctrine_Core::getTable('Foo')
->findByWidgetQuery()
->getDqlPart('where');

That should give you an array like this one :

array(2) { [0] => string(8) 'widget = ?' [1] => string(10) 'widget = ?' }

Note that it doesn't return the actual value passed to the where() clause. To do this you need to use Doctrine_Query::getParams() or Doctrine_Query::getFlattenedParams()

You can find everything in the Doctrine API.

DuoSRX
What's what I thought, but one never knows with deep object hierarchies. Thanks!
Alan Storm