簡體   English   中英

Symfony主義多對多關系:無法識別的領域

[英]Symfony Doctrine ManyToMany Relationship : unrecognized field

我遇到了幾天無法解決的問題。

我有一個分組表和一個statistiquequestion表。

1 statistiquequestion->幾個組1 groupe->幾個statistiquequestion

我建立了ManyToMany關系。

實體:

分組:

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

....
}

統計問題:

/**
 * 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;

....
}

當我嘗試查找StatistiqueQuestion對象時,出現此錯誤:

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

這是我獲取對象的代碼:

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

當我轉儲$ groupes時,將得到預期的對象數組。

也許您可以嘗試一些線索

php app/console doctrine:mapping:info

php app/console doctrine:schema:validate

如果數據庫不同步,則可能需要doctrine:schema:update(或在使用“ Doctrine遷移”的情況下生成遷移)

找到了解決方案。

問題是findBy方法不允許獲取具有ManyToMany關系的對象。

解決方案:

   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;
}

這不是最佳選擇,但這是我發現問題的唯一解決方案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM