duan1930 2016-09-08 20:48
浏览 54
已采纳

Symfony Doctrine ManyToMany关系:无法识别的领域

I came into a problem I can't solve for several days now.

I have a groupe table and a statistiquequestion table.

1 statistiquequestion --> several groupes 1 groupe --> several statistiquequestion

I set up a ManyToMany relationship.

Entities :

Groupe :

/**
 * Groupe
 *
 * @ORM\Table(name="groupe")
 * @ORM\Entity
 */
class Groupe
{
    ...
    /**
 * @ORM\ManyToMany(targetEntity="\PACES\StatistiqueBundle\Entity\StatistiqueQuestion", mappedBy="groupes",
 * cascade={"all"})
 */
private $statistiquesquestion;

....
}

StatistiqueQuestion :

/**
 * StatistiqueQuestion
 *
 * @ORM\Table(name="statistiquequestion")
 * @ORM\Entity
 */
class StatistiqueQuestion
{
   ...
   /**
 * @ORM\ManyToMany(targetEntity="\PACES\UserBundle\Entity\Groupe",inversedBy="statistiquesquestion" , cascade={"persist"})
 * @ORM\JoinColumn(name="groupe_id", referencedColumnName="id")
 */
private $groupes;

....
}

When I try to find a StatistiqueQuestion object, I have this error :

SQLSTATE[42S22]: Column not found: 1054 Unrecognized field 'statistiquequestion_groupe.groupe_id' in where clause

Here's my code to get the object :

                    $statsQuestion[]=$em->getRepository( StatistiqueQuestion::class )->findOneBy( [ 'question'  => $colle,
                                                                                             'groupes' => $groupes
                                                                                           ] );

When I dump $groupes, I get an array of objects as intended.

  • 写回答

2条回答 默认 最新

  • doudihuang7642 2016-09-09 17:44
    关注

    Found the solution.

    Problem was that findBy method doesn't allow to get an object with a ManyToMany Relationship.

    Solution :

       public function getStatColleForGroupes($colle, $groupes){
        $requete = $this->_em->createQuery('SELECT s
                                            FROM PACESStatistiqueBundle:StatistiqueColle s
                                            WHERE s.colle = :colle
                                            ');
        $requete->setParameters(array('colle'=>$colle));
        $resultats= $requete->getResult();
    
    
        foreach ($resultats as $resultat)
        {
            if ($groupes == $resultat->getGroupes()->toArray())
                return $resultat;
        }
    
        return null;
    }
    

    It's not optimal but it's the only solution I found to my problem

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题