【问题标题】:Catchable Fatal Error: Object of class...could not be converted to string可捕获的致命错误:类的对象...无法转换为字符串
【发布时间】:2015-03-08 04:59:29
【问题描述】:

我正在尝试从“课程”表中获取所有记录,其中由表单提交的 $speciality 的值在 arrayColloction() 中,该 arrayColloction() 由类 Cours 的每个单个对象返回。我正在使用下一行来获得该结果(但不幸的是它不起作用):

 public function andWhereSpeciality(QueryBuilder $qb, Speciality $speciality )
  {
    $qb
      ->andWhere($qb->expr()->in(':speciality','a.specialities'))
      ->setParameter('speciality', $speciality) ;
    return $qb;
  }

Cours 类具有 ManyToMany 关系,如下代码所示:

 /**
 * @ORM\ManyToMany(targetEntity="BacUp\GeneralBundle\Entity\Speciality", cascade={"persist"})
 * @ORM\JoinColumn(nullable=false)
 * @Assert\Count(min = 1, minMessage = "You have to choose at least one speciality")
 */
private $specialities;

执行返回以下错误:

CRITICAL - Uncaught PHP Exception Symfony\Component\Debug\Exception\ContextErrorException: "Catchable Fatal Error: Object of class BacUp\GeneralBundle\Entity\Speciality could not be converted to string in C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Expr.php line 452" at C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Expr.php line 452 

【问题讨论】:

    标签: php symfony orm doctrine-orm


    【解决方案1】:

    我解决了这个问题,我在这里发布解决方案,它也许可以帮助其他人...... 您必须使用“MEMBER OF”子句来处理多对多关系,并且您可能需要在 Expr.php 文件中直接注入以下代码:

     /**
     * Creates an instance of MEMBER OF function, with the given arguments.
     *
     * @param string $x Value to be checked
     * @param string $y Value to be checked against
     *
     * @return string
     */
     public function isMemberOf($x, $y)
     {
     return $x . ' MEMBER OF ' . $y;
     }
    

    欲了解更多信息,请继续here(也许您还需要使用 INSTANCE OF?方法同上) 现在,在我的代码中,我使用了“isMemberOf”函数,效果很好。

    public function andWhereSpeciality(QueryBuilder $qb, Speciality $speciality )
      {
        $qb
          ->andWhere($qb->expr()->isMemberOf(':speciality','a.specialities'))
          ->setParameter('speciality', $speciality) ;
        return $qb;
      }
    

    希望能有所帮助。

    【讨论】:

      【解决方案2】:

      我没有对此进行测试,但使用 Member of 应该可以解决问题

      public function andWhereSpeciality(QueryBuilder $qb, Speciality $speciality )
        {
          $qb
            ->andWhere($qb->expr()->isMemberOf(':speciality','a.specialities'))
            ->setParameter('speciality', $speciality) ;
          return $qb;
        }
      

      【讨论】:

      • 我现在收到以下异常:[code]CRITICAL - 未捕获的 PHP 异常 Symfony\Component\Debug\Exception\UndefinedMethodException: "Attempted to call method "isMemberOf" on class "Doctrine\ORM\Query \Expr" 在 C:\wamp\www\Symfony\src\BacUp\GeneralBundle\Entity\CoursRepository.php 第 84 行。"在 C:\wamp\www\Symfony\src\BacUp\GeneralBundle\Entity\CoursRepository.php 第 84 行 [/code]
      猜你喜欢
      • 2013-08-07
      • 2018-03-12
      • 2014-02-21
      • 1970-01-01
      • 2015-11-21
      • 2014-06-21
      • 2016-09-22
      相关资源
      最近更新 更多