我是Symfony和php的新手,我正在试图理解数组的结果。
现在我有两个实体,Mission和User,与ManytoMany有关。我有一个表单来创建新的任务和一个表单来创建新的用户。
现在我必须创建一个“modifyMissionAction”,允许我为这些任务设置用户,但我不明白该怎么做。 我阅读了文档here,但没有用。我该怎么办?
谢谢
这是我的用户实体:
abstract class User extends BaseUser
{
/**
* @var \Doctrine\Common\Collections\ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Acme\ManagementBundle\Entity\Mission", inversedBy="users", orphanRemoval=true)
* @ORM\JoinTable(name="user_mission")
*/
private $missions;
/**
* Add missions
*
* @param \Acme\ManagementBundle\Entity\Mission $missions
* @return User
*/
public function addMission(\Acme\ManagementBundle\Entity\Mission $missions)
{
$this->missions[] = $missions;
return $this;
}
//...
我的使命实体:
<?php
namespace Acme\ManagementBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
/**
* @ORM\Entity
*/
class Mission {
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @var integer
*/
protected $id;
/**
* @ORM\Column(type="string", length=60)
* @var String
*/
protected $name;
/**
* @ORM\Column(type="string", length=600)
* @var String
*/
protected $description;
/**
* @var \Doctrine\Common\Collections\ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Acme\ManagementBundle\Entity\User", mappedBy="missions", cascade={"all"}, orphanRemoval=true)
*/
private $users;
public function __construct(){
$this -> users = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Mission
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* @param string $description
* @return Mission
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Add users
*
* @param \Acme\ManagementBundle\Entity\User $users
* @return Mission
*/
public function addUser(\Acme\ManagementBundle\Entity\User $users)
{
$this->users[] = $users;
return $this;
}
/**
* Remove users
*
* @param \Acme\ManagementBundle\Entity\User $users
*/
public function removeUser(\Acme\ManagementBundle\Entity\User $users)
{
$this->users->removeElement($users);
}
/**
* Get users
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getUsers()
{
return $this->users;
}
public function __toString()
{
return $this->name;
}
}
答案 0 :(得分:2)
首先不要忘记为这两个类和init ArrayCollection添加__constructor:
//src/WebHQ/NewBundle/Entity/Mission.php
//...
public function __construct()
{
$this->users = new ArrayCollection();
}
//...
我假设您要添加控制器操作,允许您将用户对象或对象与任务对象相关联。请阅读Symfony book
的embbed表单部分然后创建您的Action。最重要的是添加表单元素,用户实体的嵌入形式:
// src/WebHQ/NewBundle/Controller/MissionController.php
//...
public function newAction(Request $request)
{
$object = new \WebHQ\NewBundle\Entity\Mission();
$form = $this->createFormBuilder($object)
->add('name', 'text')
//...
// Users objects embed form
->add('users', 'user')
//...
->add('save', 'submit')
->getForm();
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($object);
$em->flush();
return $this->redirect($this->generateUrl('web_hq_new_mission_index'));
}
}
return $this->render('WebHQNewBundle:Mission:new.html.twig', array(
'form' => $form->createView(),
//...
));
}
public function editAction($id, Request $request)
{
$object = $this->getDoctrine()
->getRepository('WebHQNewBundle:Mission')
->find($id);
$form = $this->createFormBuilder($object)
->add('name', 'text')
//...
->add('users', 'user')
//...
->add('save', 'submit')
->add('delete', 'submit')
->getForm();
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$form->get('save')->isClicked() ? $em->persist($object) : $em->remove($object);
$em->flush();
return $this->redirect($this->generateUrl('web_hq_new_mission_index'));
}
}
return $this->render('WebHQNewBundle:Mission:edit.html.twig', array(
'form' => $form->createView(),
//...
));
}
//...
检查您的路由。您应该使用{id}
param进行编辑操作的路由。如果param的名称不适合,则在路由和函数定义中更改它:
// src/WebHQ/NewBundle/Resources/config/route.yml
//...
web_hq_new_mission_new:
pattern: /mission/new
defaults: { _controller: WebHQNewBundle:Mission:new }
web_hq_new_mission_edit:
pattern: /mission/{id}/edit
defaults: { _controller: WebHQNewBundle:Mission:edit }
//...
然后为User对象定义Form Type:
// src/WebHQ/NewBundle/Form/Type/UserType.php
namespace WebHQ\NewBundle\Form\Type;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class UserType extends AbstractType
{
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'class' => 'WebHQNewBundle:User',
'property' => 'name',
'empty_value' => 'Choose',
'required' => true,
'multiple' => true,
'query_builder' => function (Options $options) {
return function(EntityRepository $er) use ($options) {
return $er->createQueryBuilder('c')
->orderBy('c.name', 'ASC');
};
},
));
}
public function getParent()
{
return 'entity';
}
public function getName()
{
return 'user';
}
}
在service.yml中注册一个类型:
# src/WebHQ/NewBundle/Resources/config/services.yml
#...
services:
web_hq_new.form.type.user:
class: WebHQ\NewBundle\Form\Type\UserType
tags:
- { name: form.type, alias: user }
#...
祝你好运!