简体   繁体   English

Symfony2对象

[英]Symfony2 objects

I am having problem in getting data from doctrine object. 我在从原则对象中获取数据时遇到问题。 When I use findOne(id) and try to access any variable like $result->getVariable() it works fine. 当我使用findOne(id)并尝试访问$result->getVariable()之类的任何变量时,它都可以正常工作。 But as soon as I use doctrine query builder and add some conditions, it says 但是,一旦我使用教义查询构建器并添加一些条件,它就会说

Attempted to call method "getVariable" on class "Doctrine\ORM\QueryBuilder....

My Code is 我的代码是

foreach ($Ids as $Id) {
            $result = $em->createQueryBuilder()->select("s")
                ->from("Entity", "s")
                ->where('s.id = :s_id')
                ->setParameters(array('s_id'=>$Id));
     if($category)
     {

        $result->innerJoin('s.cat','c');
        $result->where("c.primaryClassification =   :category");
        result->setParameter('category',$category);
    }

}

The Code which is working is 起作用的代码是

foreach ($Ids as $Id) {
$em->getRepository("Entity")->findOneById($Id);
}

I think it is the difference in data returned because of different types of methods used. 我认为这是由于使用的方法类型不同而导致返回数据的差异。

Thanks in advance! 提前致谢!

That's because the QueryBuilder is only for that, to build querys (BA-DUM-TSS). 这是因为QueryBuilder仅用于构建查询(BA-DUM-TSS)。 What you need is to execute the query after you build it and set the parameter correctly for a = : 你需要的是你建立了正确的设置参数后执行查询=

foreach ($Ids as $Id) {
    $query[] = $em->createQueryBuilder()->select("s")
        ->from("Entity", "s")
        ->where('s.id = :s_id')
        ->setParameter('s_id', $Id))
        ->getQuery()
        ->getResult();
}

also if you are looking for an array of data, is BEST if you use the IN statement without the foreach and pass the array directly to the setParameter : 如果您正在寻找数据数组,那么如果您使用不带foreach的IN语句并将该数组直接传递给setParameter ,则为BEST

$result = $em->createQueryBuilder()->select("s")
    ->from("Entity", "s")
    ->where('s.id IN (:s_id)')
    ->setParameter('s_id', $Ids)
    ->getQuery
    ->getResult();

If you need more info on the query builder check the docs . 如果您需要有关查询生成器的更多信息, 请检查docs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM