【问题标题】:Is it possible to find columns using multiple values in Doctrine and Symfony2?是否可以在 Doctrine 和 Symfony2 中使用多个值查找列?
【发布时间】:2014-02-03 09:48:46
【问题描述】:

图片我有以下数据库结构

class voters
{
    protected $voterid;
    protected $imageid;
    protected $action;
}

// $voterid = is the current voter
// $imageid = is the id of the voted image
// $action = is upvote/downvote,delete

如果我想一次查找多个项目以检查列是否存在,会发生什么情况? 像

$dummy = findOneBy('voterid'=>1,'imageid'=>2,action=>"upvote");

if($dummy)
{
   //column exists!
}

这可能吗?

【问题讨论】:

    标签: php symfony doctrine-orm


    【解决方案1】:

    见Databases and Doctrine

    如果你想检索一个产品,关于某些属性,你只需要使用findOneBy方法,参数是一个数组:

    $product = $repository->findOneBy(array('name' => 'foo', 'price' => 19.99));
    

    【讨论】:

    • 请记住,如果没有找到记录,findOneBy 会抛出异常 (IIRC)。
    • @Maerlyn 在最新的 Doctrine (2.5) 中,如果没有找到记录,findOneBy() 返回 null - doctrine-project.org/api/orm/2.5/…
    【解决方案2】:

    您必须像这样将所有值作为数组传递:

    $dummy = $this->getDoctrine()->getRepository("AcmeDemoBundle:User")->findOneBy(array(
        'voterid'=>1,
        'imageid'=>2,
        'action'=>'upvote',
    ));
    
    if($dummy)
    {
       //column exists!
    }
    

    其中数组的key是列名,value是该列的值。

    注意:AcmeDemoBundle:User - 是您的实体

    【讨论】:

      猜你喜欢
      • 2014-12-20
      • 2017-02-16
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 2013-01-08
      • 2013-02-26
      相关资源
      最近更新 更多