繁体   English   中英

调用Doctrine中未定义的方法Slice

[英]Call to undefined method Slice in Doctrine

我有这个功能

public function getWall(){
    $q = $this->createQueryBuilder('f');
    $q->leftJoin("f.profilo", 'p');
    $q->leftJoin("p.utente", 'u');
    $q->where('(f.foto_eliminata IS NULL OR f.foto_eliminata != 1)');
    $q->andWhere('p.fase_registrazione = :fase');
    $q->andWhere('u.locked = :false');
    $q->slice(0, 20);
    $q->setParameter(':fase', 100);
    $q->setParameter('false', false);
    $q->orderBy('f.created_at', 'desc');
    $dql = $q->getQuery();
    $results = $dql->execute();

    return $results;
}

但是我得到这个错误,

Call to undefined method Doctrine\ORM\QueryBuilder::slice()

好的,因此,您得到此错误,因为QueryBuilder没有这种方法。 但是Collection有。 如果您想使用slice,可能的变体是:

use Doctrine\Common\Collections;
public function getWall(){
    $result = $this->createQueryBuilder('f')
        ->leftJoin("f.profilo", 'p')
        ->leftJoin("p.utente", 'u')
        ->where('(f.foto_eliminata IS NULL OR f.foto_eliminata != 1)')
        ->andWhere('p.fase_registrazione = :fase')
        ->andWhere('u.locked = :false')
        ->setParameter('fase', 100)
        ->setParameter('false', false)
        ->orderBy('f.created_at', 'desc')
        ->getQuery()
        ->getResult();

      // $result typed as array 
      return new Collections\ArrayCollection($result))->slice(0,20); // convert array to collection, then slice 
 }

顺便说一句,以这种方式“限制”查询结果不是一个好主意。 U可以使用setMaxResults(20) ,而根本不选择所有对象。

关于惰性集合( http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/extra-lazy-associations.html ):选择result对象后,可以从result集合中获取一些对象: $r = $result[0]之后:

$portfilos = $r->getPortfolio(); // returns for example Collection with some objects;
                                 // its Lazy, without SQL query!

$portfolios->slice(0, 20); // queries first 20 potfolios

如果您在某种关系中有很多对象,则使用slice是一个不错的主意。

ps sry,mb我没认出您的问题,但是尝试了:)

编辑的固定错误代码。

暂无
暂无

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

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