我尝试构建一个用于创建实体Campaign的表单,我可以在其中链接用户。问题是当我坚持竞选实体时,连接表中没有任何事情发生。
以下是广告系列实体的摘录:
/**
* Campaign
*
* @ORM\Table(name="campaign")
* @ORM\Entity(repositoryClass="AppBundle\Repository\CampaignRepository")
* @ORM\HasLifecycleCallbacks
*/
class Campaign
{
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="campaigns", cascade={"all"})
*/
private $players;
/**
* Add players
*
* @param \AppBundle\Entity\User $players
* @return Campaign
*/
public function addPlayer(\AppBundle\Entity\User $players)
{
$players->addCampaign($this);
$this->players[] = $players;
return $this;
}
/**
* Remove players
*
* @param \AppBundle\Entity\User $players
*/
public function removePlayer(\AppBundle\Entity\User $players)
{
$this->players->removeElement($players);
}
/**
* Get players
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPlayers()
{
return $this->players;
}
}
以下是用户实体的摘录:
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
* @ORM\HasLifecycleCallbacks
*/
class User extends \FOS\UserBundle\Model\User
{
/**
* @ORM\ManyToMany(targetEntity="Campaign", inversedBy="players", cascade={"all"})
* @ORM\JoinTable(name="users_campaigns")
*/
protected $campaigns;
/**
* Add campaign
*
* @param \AppBundle\Entity\Campaign $campaign
* @return User
*/
public function addCampaign(\AppBundle\Entity\Campaign $campaign)
{
$campaign->addPlayer($this);
$this->campaigns[] = $campaign;
return $this;
}
/**
* Remove campaign
*
* @param \AppBundle\Entity\Campaign $campaign
*/
public function removeCampaign(\AppBundle\Entity\Campaign $campaign)
{
$this->campaigns->removeElement($campaign);
}
/**
* Get campaign
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCampaign()
{
return $this->campaigns;
}
}
这是我的表单类型:
class NewCampaignFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', array(
"label" => 'form.name',
'required' => true,
))
->add('description','ckeditor',array(
"label" => 'form.description',
"required" => true,
))
->add('players','entity',array(
'label'=>'form.campaign.players',
'class'=> 'AppBundle\Entity\User',
'multiple' => true,
));
}
public function getName()
{
return 'campaign';
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Campaign',
));
}
}
最后,这是我的控制器:
public function createAction(Request $request){
$em = $this->getDoctrine()->getManager();
$campaign = new Campaign();
$form = $this->createForm(new NewCampaignFormType(),$campaign);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$user = $this->getUser();
$campaign->setMJ($user->getId());
$players = $form->get('players');
var_dump($players);
$em->persist($campaign);
$em->flush();
return $this->redirectToRoute('show_campaign', array('id'=>$campaign->getId()));
}
return $this->render('campaign/createCampaign.html.twig', array(
'form' => $form->createView(),
'campaign' => $campaign,
));
}
我现在已经坚持了很长一段时间,所以任何有想法的人都试试吧。
感谢您阅读
答案 0 :(得分:1)
您处理的Campain
对象不属于该关系的一方,因此默认情况下它不会被Doctrine检查和保留。
像这里改变关系的一面:
运动
/**
* @ORM\ManyToMany(targetEntity="User", inversedBy="campaigns" , cascade={"all"})
* @ORM\JoinTable(name="users_campaigns")
*/
private $players;
用户
/**
* @ORM\ManyToMany(targetEntity="Campaign", mappedBy="players", cascade={"all"})
*/
protected $campaigns;