在我的symfony应用程序中,我使用的是嵌入式表单。就我而言,一个对象" CompetenceGroupe"可以有多个对象" CompetenceItem",但是一个对象" CompetenceItem"只属于一个对象" CompetenceGroupe",所以关系是很多的.O /。
表单完美无缺,我有两个表(每个实体一个表),并且它保存在数据库中。
但是当我在控制器中选择带有教义的CompetenceGroupe对象时,我掌握了对象的所有信息,并且他有一个空的" compeItems"属性,所以我无法检索子对象(CompetenceItem)。
我的" CompetenceGroupe"实体:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="competences_groupes")
*/
class CompetenceGroupe
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id_competence_groupe;
/**
* @var User $user
*
* @ORM\ManyToOne(targetEntity="User", cascade={"persist", "merge"})
* @ORM\JoinColumn(name="id_user", referencedColumnName="id_user", nullable=false)
*/
private $user;
/**
* @ORM\Column(type="string", length=60, nullable=true)
*/
protected $titre;
protected $competence_items;
public function __construct()
{
$this->competence_items = new ArrayCollection();
}
public function getCompetenceItems()
{
return $this->competence_items;
}
/**
* Get idCompetenceGroupe
*
* @return integer
*/
public function getIdCompetenceGroupe()
{
return $this->id_competence_groupe;
}
/**
* Set titre
*
* @param string $titre
*
* @return CompetenceGroupe
*/
public function setTitre($titre)
{
$this->titre = $titre;
return $this;
}
/**
* Get titre
*
* @return string
*/
public function getTitre()
{
return $this->titre;
}
/**
* Set user
*
* @param \AppBundle\Entity\User $user
*
* @return CompetenceGroupe
*/
public function setUser(\AppBundle\Entity\User $user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \AppBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
public function addItem(CompetenceItem $item)
{
$this->competence_items->add($item);
}
public function removeItem(CompetenceItem $item)
{
// ...
}
/**
* Set competenceItems
*
* @param \AppBundle\Entity\CompetenceItem $competenceItems
*
* @return CompetenceGroupe
*/
public function setCompetenceItems(\AppBundle\Entity\CompetenceItem $competenceItems = null)
{
$this->competence_items = $competenceItems;
return $this;
}
}
我的" CompetenceItem"实体:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="competences_items")
*/
class CompetenceItem
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id_competence_item;
/**
* @ORM\Column(type="string", length=60, nullable=false)
*/
protected $libelle;
/**
* @var CompetenceNiveau $niveau
*
* @ORM\ManyToOne(targetEntity="CompetenceNiveau", cascade={"persist", "merge"})
* @ORM\JoinColumn(name="id_competence_niveau", referencedColumnName="id_competence_niveau", nullable=true)
*/
private $niveau;
/**
* @var CompetenceGroupe $competence_groupe
*
* @ORM\ManyToOne(targetEntity="CompetenceGroupe", cascade={"persist", "merge"})
* @ORM\JoinColumn(name="id_competence_groupe", referencedColumnName="id_competence_groupe", nullable=false)
*/
private $competence_groupe;
/**
* Get idCompetenceItem
*
* @return integer
*/
public function getIdCompetenceItem()
{
return $this->id_competence_item;
}
/**
* Set libelle
*
* @param string $libelle
*
* @return CompetenceItem
*/
public function setLibelle($libelle)
{
$this->libelle = $libelle;
return $this;
}
/**
* Get libelle
*
* @return string
*/
public function getLibelle()
{
return $this->libelle;
}
/**
* Set niveau
*
* @param \AppBundle\Entity\CompetenceNiveau $niveau
*
* @return CompetenceItem
*/
public function setNiveau(\AppBundle\Entity\CompetenceNiveau $niveau = null)
{
$this->niveau = $niveau;
return $this;
}
/**
* Get niveau
*
* @return \AppBundle\Entity\CompetenceNiveau
*/
public function getNiveau()
{
return $this->niveau;
}
/**
* Set competenceGroupe
*
* @param \AppBundle\Entity\CompetenceGroupe $competenceGroupe
*
* @return CompetenceItem
*/
public function setCompetenceGroupe(\AppBundle\Entity\CompetenceGroupe $competenceGroupe)
{
$this->competence_groupe = $competenceGroupe;
return $this;
}
/**
* Get competenceGroupe
*
* @return \AppBundle\Entity\CompetenceGroupe
*/
public function getCompetenceGroupe()
{
return $this->competence_groupe;
}
}
我认为我缺少对#34; compe_items"的注释。 CompetenceGroupe实体中的属性,但我真的不确定......
感谢您的帮助!
答案 0 :(得分:0)
一个好的做法可能是拥有一个能力表格,这将在你的能力组表格中调用
您可以将CollectionType添加为parrent并包含查询以搜索已存在的权限
symfony演示博客中有post form type的一些很好的例子
或者您可以使用form events(onSubmit,preSubmit等等)向您的实体收取所需的权限。此示例显示了一个消息表单,允许从预设数据中选择好友,这是一个很好的例子。
答案 1 :(得分:0)
对于ArrayCollection
,您可以移除方法setCompetenceItems
,而是在addCompetenceItem
实体中定义方法CompetenceGroupe
。
class CompetenceGroupe
{
/**
* @ORM\OneToMany(targetEntity="CompetenceItem", mappedBy="competence_groupe")
*/
protected $competenceItems;
public function __construct()
{
$this->competenceItems= new ArrayCollection();
}
/**
* Add competenceItem
*
* @param CompetenceItem $competenceItem
* @return CompetenceGroupe
*/
public function addCompetenceItem(CompetenceItem $competenceItem)
{
$this->competence_items->add($competenceItem);
return $this;
}
}
您还需要定义拥有方以使所有这些工作。
答案 2 :(得分:0)
你有两个选择,即使是创建一个Many-To-One, Unidirectional ,在这种情况下,你需要清理一些代码,看看:
在CompetenceGroupe课程中:
class CompetenceGroupe
{
/**
* Many competence have One Group.
* @ManyToOne(targetEntity="CompetenceItem")
* @JoinColumn(name="id_competence_item", referencedColumnName="id_competence_item")
*/
protected $competence_items;
public function __construct()
{
// $this->competence_items = new ArrayCollection();
//delete that line
}
在CompetenceItem类中:
class CompetenceItem
{
您需要使用其注释删除private $competence_groupe;
属性:
通过这种方式,当您转储 CompetenceGroupe 对象时,您将找到能力项目。
此外,如果您想从反面和从拥有方获取数据,您可以使用One-To-Many, Bidirectional 执行此操作。