0

Doctrine always includes an ID column in a query, for example:

 $new_fees = Doctrine::getTable('Stats')->createQuery('s')
  ->select('s.sid')->where('s.uid = ?', $this->uid)
  ->andWhere('s.operation = ?', PPOperationType::FEE_PAID_BY_REFERRED_OWNER)
  ->andWhere('s.created_at > ?', $lastwd)
  ->groupBy('s.sid')->execute();

won't work, because s.id is included (which I didn't ask doctrine for). How do I get rid of that id column? Having to use a raw SQL here kills the usefulness of doctrine.

Jacek Krysztofik
  • 1,266
  • 1
  • 13
  • 29

2 Answers2

2

You have to set some column of that table to be the primary in the setTableDefinition, so that doctrine doesn't use default primary as id. Let's say you sid is you actual primary key.. then...

    public function setTableDefinition(){
    ....
    $this->hasColumn('sid', 'decimal', 2, array(
                 'type' => 'decimal',
                 'length' => 2,
                 'unsigned' => 0,
                 'primary' => true,
                 'default' => '0',
                 'notnull' => true,
                 'autoincrement' => false,
                 ));
    }

Notice the 'primary' => true, this prevents doctrine to use id as the default primary key (even when it's not even defined in the table definition file.

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Jaguilar
  • 36
  • 2
1

This isn't the prettiest solution, but you can call isSubquery(true) on the Doctrine_Query to remove the primary key, in your case s.id.

http://www.doctrine-project.org/api/orm/1.2/doctrine/doctrine_query.html#isSubquery()

Chris Baclig
  • 543
  • 3
  • 9