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 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效