可捕获的致命错误:类<class>的对象无法转换为... \ vendor \ doctrine \ orm \ lib \ Doctrine \ ORM \ Query \ Expr.php第568行</class>中的字符串

时间:2013-08-30 15:22:56

标签: php symfony orm doctrine-orm doctrine

我正在创建一个市场,标签和排序顺序,标签正常工作,但当我有分类代码时,它说:

Catchable Fatal Error: Object of class \Entity\Category could not beconverted to
string in \vendor\doctrine\orm\lib\Doctrine\ORM\Query\Expr.php line 568

我搜索谷歌,没有人有同样的问题? 我的代码:

public function tagAction($tag, $sort) {

    $rep = $this->getDoctrine()
            ->getRepository('RSHubMarketplaceBundle:Modification');
    $tags = $this->getDoctrine()
    ->getRepository('RSHubMarketplaceBundle:Category')
    ->findAll();
    if($tag == -1){
            $stags = $tags;

    } else {
        $tag = $this->getDoctrine()
    ->getRepository('RSHubMarketplaceBundle:Category')
    ->find($tag);
        $stags = array($tag); 
    }
    switch ($sort) {
    case 'popularity':
        $mods = $rep->getByPopularity( array($tag));
        break;
    case 'downloads':
        $mods = $rep->getByDownloads( array($tag));
        break;
    case 'newest':
        $mods = $rep->getByNewest(array($tag));
        break;
    case 'name':
        $mods = $rep->getByName( array($tag));
        break;

    }
    return $this->render('RSHubMarketplaceBundle:Marketplace:index.html.twig',
                    array('categories' => $tags, 'mods' =>$mods,
                            'tag' => $tag));
}

我遇到问题的方法:

public function getWithCategoriesOrdered(array $nom_categories, $orderCol, $order) {
    $qb = $this->createQueryBuilder('a');

    $qb->join('a.categories', 'c')
            ->where($qb->expr() // HERE
                    ->in('c.name', $nom_categories));
    $qb->add('orderBy', 'a.'.$orderCol.' '.$order);

    return $qb->getQuery()
            ->getResult();
}

1 个答案:

答案 0 :(得分:0)

有两种方式

a)在类别实体中添加__toString()方法,返回其名称

b)假设$ nom_categories是一个类别,只需要在查询表达式中传递$ nom_categories-&gt; getName()

在这种错误中(使用外部库),您需要调试并仔细检查堆栈跟踪