Pregunta

I tried with normal sql query

SELECT activity_shares.id FROM `activity_shares` 
INNER JOIN (SELECT `activity_id` FROM `activity_shares`
GROUP BY `activity_id`
HAVING  COUNT(`activity_id`) > 1 ) dup ON activity_shares.activity_id = dup.activity_id

Which gives me record id say 10 and 11

But same query I tried to do in Doctrine query builder,

 $qb3=$this->getEntityManager()->createQueryBuilder('c')
           ->add('select','c.id')
           ->add('from','MyBundleDataBundle:ActivityShare c')
           ->innerJoin('c.activity', 'ca')
         //  ->andWhere('ca.id = c.activity')
           ->groupBy('ca.id')
          ->having('count(ca.id)>1');

Edited:

        $query3=$qb3->getQuery();
        $query3->getResult();

Generated SQL is:

SELECT a0_.id AS id0 FROM activity_shares a0_ 
INNER JOIN activities a1_ ON a0_.activity_id = a1_.id 
GROUP BY a1_.id HAVING count(a1_.id) > 1

Gives only 1 record that is 10.I want to get both.I'm not getting idea where I went wrong.Any idea?

My tables structure is:

ActivityShare
    +-----+---------+-----+---
    | Id  |activity |Share| etc...
    +-----+---------+-----+----
    | 1   | 1       |1    |
    +-----+---------+-----+---
    | 2   | 1       | 2   |
    +-----+---------+-----+---

Activity is foreign key to Activity table. I want to get Id's 1 and 2

¿Fue útil?

Solución 2

Thanks for your answers.I finally managed to get answer

My Doctrine query is:

$subquery=$this->getEntityManager()->createQueryBuilder('as')
      ->add('select','a.id')
      ->add('from','MyBundleDataBundle:ActivityShare as')
      ->innerJoin('as.activity', 'a')
      ->groupBy('a.id')
      ->having('count(a.id)>1');



 $query=$this->getEntityManager()->createQueryBuilder('c')
      ->add('select','c.id')
      ->add('from','ChowzterDataBundle:ActivityShare c')
      ->innerJoin('c.activity', 'ca');

    $query->andWhere($query->expr()->in('ca.id', $subquery->getDql()))
      ;

    $result = $query->getQuery();
    print_r($result->getResult());

And SQL looks like:

SELECT a0_.id AS id0 FROM activity_shares a0_ INNER JOIN activities a1_ ON a0_.activity_id = a1_.id WHERE a1_.id IN (SELECT a2_.id FROM activity_shares a3_ INNER JOIN activities a2_ ON a3_.activity_id = a2_.id GROUP BY a2_.id HAVING count(a2_.id) > 1

Otros consejos

Simplified SQL

first of all let me simplify that query so it gives the same result :

SELECT id FROM `activity_shares` 
GROUP BY `id`
HAVING  COUNT(`activity_id`) > 1 

Docrtrine QueryBuilder

If you store the id of the activty in the table like you sql suggests:

You can use the simplified SQL to build a query:

$results =$this->getEntityManager()->createQueryBuilder('c')
           ->add('select','c.id')
           ->add('from','MyBundleDataBundle:ActivityShare c')
           ->groupBy('c.id')
           ->having('count(c.activity)>1');
           ->getResult();

If you are using association tables ( Doctrine logic)

here you will have to use join but the count may be tricky

Solution 1

use the associative table like an entitiy ( as i see it you only need the id) Let's say the table name is activityshare_activity

it will have two fields activity_id and activityshare_id, if you find a way to add a new column id to that table and make it Autoincrement + Primary the rest is easy :

the new entity being called ActivityShareActivity

$results =$this->getEntityManager()->createQueryBuilder('c')
           ->add('select','c.activityshare_id')
           ->add('from','MyBundleDataBundle:ActivityShareActivity c')
           ->groupBy('c.activityshare_id')
           ->having('count(c.activity_id)>1');
           ->getResult();

the steps to add the new identification column to make it compatible with doctrine (you need to do this once):

  1. add the column (INT , NOT NULL) don' t put the autoincrement yet

    ALTER TABLE tableName ADD id INT NOT NULL

  2. Populate the column using a php loop like for

  3. Modify the column to be autoincrement

    ALTER TABLE tableName MODIFY id INT NOT NULL AUTO_INCREMENT

Solution2

The correction to your query

$result=$this->getEntityManager()->createQueryBuilder()
           ->select('c.id')
           ->from('MyBundleDataBundle:ActivityShare', 'c')
           ->innerJoin('c.activity', 'ca')
           ->groupBy('c.id') //note: it's c.id not ca.id
           ->having('count(ca.id)>1')
           ->getResult();

I posted this one last because i am not 100% sure of the output of having+ count but it should word just fine :)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top