Question

I use doctrine 1.2, in symfony 1.4.8 . Here is my code:

private function getWithMaxProductsQ($limit){
    $query = Doctrine_Query::create()
                            ->select('m.name, COUNT(p.manufacturer_id) AS c')
                            ->from('manufacturer m')
                            ->innerJoin('m.Product p')
                            ->where('p.is_published = 1')
                            ->addWhere('p.is_available = 1')
                            ->groupBy('p.manufacturer_id')
                            ->orderBy('c DESC')
                            ->limit($limit);
    //die($query->getSqlQuery());
    return $query;
}
public function getManufacturersForCategoryDeep(Category $cat, $limit = 7){
    $ids = $cat->getDescendantsAndMeIds();
    $q = $this->getWithMaxProductsQ($limit);
    $q->innerJoin('p.Category c')->whereIn('c.id', $ids);

    die($q->getSqlQuery());
    $mans = $q->execute();
    return $mans;
}

As you may notice there is die call inside getManufacturersForCategoryDeep which gives me info what query will be executed. Here is sql query:

SELECT `m`.`id` AS `m__id`, `m`.`name` AS `m__name`, COUNT(`p`.`manufacturer_id`) AS `p__0` FROM `manufacturer` `m` 
INNER JOIN `product` `p` ON `m`.`id` = `p`.`manufacturer_id` 
INNER JOIN `category` `c` ON `p`.`category_id` = `c`.`id` 
WHERE `m`.`id` IN ('49', '43', '127', '42', '65', '134', '113') 
AND (`p`.`is_published` = 1 
AND `p`.`is_available` = 1 
AND `c`.`id` IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)) 
GROUP BY `p`.`manufacturer_id` ORDER BY `p__0` DESC

And I dont know why this line appear in query WHERE m.id IN ('49', '43', '127', '42', '65', '134', '113') I used apc and memcached caching for DQL query parsing caching, but it was disabled a month ago. So how i can find out what is happening with this query ?

UPDATE: this line is dissapears after removal limit() invocation

UPDATE2: here is open bug http://www.doctrine-project.org/jira/browse/DC-1052?page=com.atlassian.streams.streams-jira-plugin:activity-stream-issue-tab

Was it helpful?

Solution

When you add limit() to a query in DQL Doctrine actually breaks your query into two separate ones. In the first query only primary keys are selected using all the condition you have used (also the LIMIT).

The second query selects the actual objects using the list of the primary keys found in the first one to limit the results. That's why you can see the additional line in your query.

I believe this is used because of performance reasons.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top