我是Symfony和PHP的新手,我试图理解,而没有结果,即Array Collection。
现在,我有两个实体,任务和用户,与Manytomany有关。我有一个形式来创建新任务和一个创建新用户的表格。
现在,我必须创建一个" ModifermissionAction",使我能够为该任务设置用户,但我不明白该怎么做。我在这里阅读文档,但这无济于事。我该怎么办?
谢谢
这是我的用户实体是:
abstract class User extends BaseUser
{
/**
* @var DoctrineCommonCollectionsArrayCollection
*
* @ORMManyToMany(targetEntity="AcmeManagementBundleEntityMission", inversedBy="users", orphanRemoval=true)
* @ORMJoinTable(name="user_mission")
*/
private $missions;
/**
* Add missions
*
* @param AcmeManagementBundleEntityMission $missions
* @return User
*/
public function addMission(AcmeManagementBundleEntityMission $missions)
{
$this->missions[] = $missions;
return $this;
}
//...
和我的任务实体:
<?php
namespace AcmeManagementBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
/**
* @ORMEntity
*/
class Mission {
/**
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
* @var integer
*/
protected $id;
/**
* @ORMColumn(type="string", length=60)
* @var String
*/
protected $name;
/**
* @ORMColumn(type="string", length=600)
* @var String
*/
protected $description;
/**
* @var DoctrineCommonCollectionsArrayCollection
*
* @ORMManyToMany(targetEntity="AcmeManagementBundleEntityUser", 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 AcmeManagementBundleEntityUser $users
* @return Mission
*/
public function addUser(AcmeManagementBundleEntityUser $users)
{
$this->users[] = $users;
return $this;
}
/**
* Remove users
*
* @param AcmeManagementBundleEntityUser $users
*/
public function removeUser(AcmeManagementBundleEntityUser $users)
{
$this->users->removeElement($users);
}
/**
* Get users
*
* @return DoctrineCommonCollectionsCollection
*/
public function getUsers()
{
return $this->users;
}
public function __toString()
{
return $this->name;
}
}
首先,不要忘记为两个类别和init arrayCollection添加__constructor:
//src/WebHQ/NewBundle/Entity/Mission.php
//...
public function __construct()
{
$this->users = new ArrayCollection();
}
//...
我假设您要添加控制器操作女巫允许您将用户对象或对象与任务对象相关联。请阅读Symfony Book的一部分Embbed
然后创建您的动作。最重要的是添加表单元素,并带有用户实体的嵌入式形式:
// src/WebHQ/NewBundle/Controller/MissionController.php
//...
public function newAction(Request $request)
{
$object = new WebHQNewBundleEntityMission();
$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}
参数进行编辑操作的路由。如果不适合参数的名称,则在路由和功能定义中更改它:
// 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 }
//...
然后为用户对象定义表单类型:
// src/WebHQ/NewBundle/Form/Type/UserType.php
namespace WebHQNewBundleFormType;
use DoctrineORMEntityRepository;
use SymfonyComponentFormAbstractType;
use SymfonyComponentOptionsResolverOptions;
use SymfonyComponentOptionsResolverOptionsResolverInterface;
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: WebHQNewBundleFormTypeUserType
tags:
- { name: form.type, alias: user }
#...
祝你好运!