Tutorial :A newbie question about doctrine


Select * from tableName order by id desc limit 10  

How to perform something like the above with doctrine by a demo?


Inside the model's Table class (eg tableNameTable.class.php):

function getResults()  {    $results = self::createQuery("q")      ->select("q.*")      ->orderBy("q.id DESC")      ->limit(10)      ->execute();      return $results;  }  

will give you a Doctrine Collection of results.


In my experience most people don't write specific table classes, but use auto generated Doctrine_Record classes via the CLI tool.

If that's your case, you can do something like

//instantiate your record class  $model = new TableName();    $model->getTable() //returns an instance of Doctrine_Table for current Doctrine_Record         ->createQuery() //returns a Doctrine_Query instance with the current table loaded        ->orderBy("id DESC")        ->limit(10)        ->execute();  

If you find that you are always ordering all results by ID DESC and limiting all queries to 10, you can also add a hook in the Doctrine Record class like so

class TableName extends Base_TableName //most Doctrine Records extend a base record with config info  {     //this hook will order all by id and limit all queries to 10     public function preDqlSelect(Doctrine_Event $event)     {        $event->getQuery()              ->addOrderBy("id DESC")              ->limit(10);     }    }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »