Select Distinct in Doctrine and Symfony

Asked

Viewed 255 times

1

Hi, I have a photo album creation table. My code makes 1 Insert for each image added in the photo album. I have a grid for displaying Albums for the user to view. I want to make a distinct of duplicate albuns.

I have this kind of code:

      $select = Doctrine_Query::create()
                                ->select('DISTINCT s.idAlbum')
                                ->addSelect('s.imagens_data, s.publicar')
                                ->from('tbimagens s')->getSqlQuery();
      print_r($select);
      die;

This is my Sql result:

 SELECT DISTINCT 
   t.id AS t__id, 
   t.idalbum AS t__idalbum, 
   t.imagens_data AS t__imagens_data, 
   t.publicar AS t__publicar 
 FROM tbimagens t

Did you see that select brings the table Id ? This Id is making it impossible for me to make the correct select because all Ids are already naturally distinct.

Can someone help me with this query? I’m using symfony 1.4 and Doctrine 1.2

  • What is your criteria for deciding whether an album is duplicated or not?

  • if the number of times it appears on the grid is greater than the number of images uploaded to it.

1 answer

1

$select = Doctrine_Query::create()
  ->from('tbimagens s')
  ->select('DISTINCT s.id_album')
  ->addSelect('s.imagens_data, s.publicar')
  ->getSqlQuery();

face tries to see if you are not passing a wrong parameter, in case the foreign key to the album. And also the order as you create the select, but really it cannot put in your select the table id.

Another solution would be to put a Unique in the foreign key.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.