开发者

A newbie question about doctrine

Select * from tableNa开发者_StackOverflowme 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);
   }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜