我有实体技能和语言有关系ManyToMany。
我创造技能并选择语言我在我的表中看到了很多很多id技能和id语言,但是当我创建语言并选择技能时,我没有新的行技能表语言,但是在刷新后我有:< / p>
$afetr_skills = $entity->getSkills()->getValues();
有技巧,但我没有在DB的表格中看到这一点。我做错了什么?请帮助。
我的实体:
class CodeDirectoryProgramLanguages
{
use Timestampable;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="languages", type="string", length=255)
*/
private $languages;
/**
* @ORM\ManyToMany(targetEntity="Artel\ProfileBundle\Entity\Skill", mappedBy="language", cascade={"persist", "remove"})
*/
protected $skills;
和技能:
class Skill
{
use Timestampable;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="skill", type="string", length=255)
*/
private $skill;
/**
* @var \Artel\ProfileBundle\Entity\CodeDirectoryProgramLanguages
*
* @ORM\ManyToMany(targetEntity="CodeDirectoryProgramLanguages", inversedBy="skills", cascade={"persist", "remove"})
*/
protected $language;
和表单添加语言:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('languages', 'text', array('label' => 'Skill Name'));
$builder->add('skills','entity',
array(
'class'=>'Artel\ProfileBundle\Entity\Skill',
'property'=>'skill',
'multiple'=>true,
'expanded' => true,
)
);
$builder
->add('save','submit')
;
}
和我的行动:
public function newLanguageAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$entity = new CodeDirectoryProgramLanguages();
$form = $this->createForm(new LanguageType(), $entity);
$form->submit($request);
if ($request->isMethod('POST')) {
if ($form->isValid()) {
$em->persist($entity);
$em->flush();
$skills = $entity->getSkills()->getValues();
if(!empty($skills)){
foreach($skills as $skill){
$skill->removeLanguage($entity);
$em->persist($skill);
$em->flush();
}
}
if(!empty($form_skills)){
foreach($form_skills as $skill){
$skill->addLanguage($entity);
$em->persist($skill);
$em->flush();
}
}
}
return $this->redirect($this->generateUrl('languages_show'));
}
}
return array(
'debug' => true,
'form' => $form->createView(),
);
}
当我编辑实体时,我首先删除所有技能,然后按表单添加技能,但我知道这很糟糕。 如何在学说中使用这种关系并在DB中设置?