Propel中Doctrine的等价findBy和fetchOne

时间:2011-12-27 20:05:58

标签: php symfony1 doctrine symfony-1.4 propel

在Doctrine我能做到:

   $article = Doctrine::getTable('Article')->createQuery('a')->where('a.user_id = ?', $this->getUser()->getAttribute('id') )->fetchOne()

$article = Doctrine::getTable('Article')->findBy('user_id', $this->getUser()->getAttribute('id'));

我怎样才能在Propel中制作它? 我使用Symfony 1

2 个答案:

答案 0 :(得分:2)

或推进1.6

ArticleQuery::create()
    ->filterByUserId($this->getUser()->getId())
    ->findOne();
 //returns Article instance or null

甚至

ArticleQuery::create()
    ->filterByUserId($this->getUser()->getId())
    ->findOneOrCreate();
 //always returns article instance, if created user id will be already initialized

答案 1 :(得分:1)

Propel非常简单。我给你推进1.4答案。这些看起来像是相同的查询。

$article = Doctrine::getTable('Article')->createQuery('a')->where('a.user_id = ?',     $this->getUser()->getAttribute('id') )->fetchOne()

$c = new Criteria; 
$c->add(ArticlePeer::USER_ID, $this->getUser()->getId()); 
$article = ArticlePeer::doSelectOne($c);