开发者

A newbie question about doctrine

开发者 https://www.devze.com 2022-12-19 16:06 出处:网络
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):
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

精彩评论

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