您好我试着解决这个问题好几天了。但我不明白,所以我希望你能帮助我。
我有实体Worker,它与Task实体有多对多关系。 现在我不希望我的用户一次完成所有输入。首先,用户应输入他的详细信息,如果他们有效,他可以选择他的任务。
所以我在标准生成的代码中添加了三个方法,并在 createAction()中更改了url路径。
但是,如果我按下AddTaskType表单中的提交按钮,则数据库中没有任何内容。
我希望你对这个问题有所建议。
//Worker Entity
namespace DLRG\HelferBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity
* @ORM\Table(name="Workers")
*/
class Worker {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
* @Assert\NotBlank()
*/
protected $name;
/**
* @ORM\Column(type="string")
* @Assert\NotBlank()
*/
protected $lastName;
/**
* @ORM\Column(type="string")
*/
protected $tel;
/**
* @ORM\Column(type="string")
* @Assert\NotBlank()
* @Assert\Email()
*/
protected $email;
/**
* @ORM\Column(type="string")
*/
protected $organisation;
/**
* @ORM\Column(type="integer")
* @Assert\NotBlank()
* @Assert\Range(min=6, minMessage="Du musst mindestens 6 Jahre Alt sein um bei uns helfen zu können.")
*/
protected $age;
/**
* @ORM\ManyToMany(targetEntity="Task", inversedBy="workers")
* @ORM\JoinTable(name="workers_tasks")
*/
protected $tasks;
/**
* @ORM\ManyToMany(targetEntity="Time", inversedBy="workers")
* @ORM\JoinTable(name="workers_times")
*/
protected $times;
/**
* Constructor
*/
public function __construct() {
$this->tasks = new \Doctrine\Common\Collections\ArrayCollection ();
$this->times = new \Doctrine\Common\Collections\ArrayCollection ();
}
/**
* Get id
*
* @return integer
*/
public function getId() {
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Worker
*/
public function setName($name) {
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName() {
return $this->name;
}
/**
* Set lastName
*
* @param string $lastName
* @return Worker
*/
public function setLastName($lastName) {
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* @return string
*/
public function getLastName() {
return $this->lastName;
}
/**
* Set tel
*
* @param string $tel
* @return Worker
*/
public function setTel($tel) {
$this->tel = $tel;
return $this;
}
/**
* Get tel
*
* @return string
*/
public function getTel() {
return $this->tel;
}
/**
* Set email
*
* @param string $email
* @return Worker
*/
public function setEmail($email) {
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail() {
return $this->email;
}
/**
* Set organisation
*
* @param string $organisation
* @return Worker
*/
public function setOrganisation($organisation) {
$this->organisation = $organisation;
return $this;
}
/**
* Get organisation
*
* @return string
*/
public function getOrganisation() {
return $this->organisation;
}
/**
* Set age
*
* @param integer $age
* @return Worker
*/
public function setAge($age) {
$this->age = $age;
return $this;
}
/**
* Get age
*
* @return integer
*/
public function getAge() {
return $this->age;
}
/**
* Add tasks
*
* @param \DLRG\HelferBundle\Entity\Task $tasks
* @return Worker
*/
public function addTask(\DLRG\HelferBundle\Entity\Task $task) {
$task->addWorker ( $this );
$this->tasks [] = $task;
return $this;
}
/**
* Remove tasks
*
* @param \DLRG\HelferBundle\Entity\Task $tasks
*/
public function removeTask(\DLRG\HelferBundle\Entity\Task $tasks) {
$this->tasks->removeElement ( $tasks );
}
/**
* Get tasks
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTasks() {
return $this->tasks;
}
/**
* Add times
*
* @param \DLRG\HelferBundle\Entity\Time $times
* @return Worker
*/
public function addTime(\DLRG\HelferBundle\Entity\Time $time) {
$time->addWorker ( $this );
$this->times [] = $time;
return $this;
}
/**
* Remove times
*
* @param \DLRG\HelferBundle\Entity\Time $times
*/
public function removeTime(\DLRG\HelferBundle\Entity\Time $times) {
$this->times->removeElement ( $times );
}
/**
* Get times
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTimes() {
return $this->times;
}
}
//TaskEntity
namespace DLRG\HelferBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="Tasks")
*/
class Task {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $name;
/**
* @ORM\Column(type="string")
*/
protected $description;
/**
* @ORM\ManyToMany(targetEntity="Time", mappedBy="tasks")
*/
protected $times;
/**
* @ORM\ManyToMany(targetEntity="Worker", mappedBy="tasks")
*/
protected $workers;
public function __toString() {
return $this->name;
}
/**
* Constructor
*/
public function __construct() {
$this->times = new \Doctrine\Common\Collections\ArrayCollection ();
$this->workers = new \Doctrine\Common\Collections\ArrayCollection ();
}
/**
* Get id
*
* @return integer
*/
public function getId() {
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Task
*/
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 Task
*/
public function setDescription($description) {
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription() {
return $this->description;
}
/**
* Add times
*
* @param \DLRG\HelferBundle\Entity\Time $times
* @return Task
*/
public function addTime(\DLRG\HelferBundle\Entity\Time $time) {
$this->times [] = $time;
return $this;
}
/**
* Remove times
*
* @param \DLRG\HelferBundle\Entity\Time $times
*/
public function removeTime(\DLRG\HelferBundle\Entity\Time $times) {
$this->times->removeElement ( $times );
}
/**
* Get times
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTimes() {
return $this->times;
}
/**
* Add workers
*
* @param \DLRG\HelferBundle\Entity\Worker $workers
* @return Task
*/
public function addWorker(\DLRG\HelferBundle\Entity\Worker $worker) {
$this->workers [] = $worker;
return $this;
}
/**
* Remove workers
*
* @param \DLRG\HelferBundle\Entity\Worker $workers
*/
public function removeWorker(\DLRG\HelferBundle\Entity\Worker $workers) {
$this->workers->removeElement ( $workers );
}
/**
* Get workers
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getWorkers() {
return $this->workers;
}
}
添加的方法
//WorkerController.php
/**
* Add task to a Worker
*
* @Route("/", name="task_add")
* @Method("POST")
* @Template("DLRGHelferBundle:Task:add.html.twig")
*/
public function addTask(Request $request, $id) {
$em = $this->getDoctrine ()->getManager ();
$entity = $em->getRepository ( 'DLRGHelferBundle:Worker' )->find ( $id );
if (! $entity) {
throw $this->createNotFoundException ( 'Unable to find Worker entity.' );
}
$form = $this->createAddTaskForm ( $entity );
$form->handleRequest ( $request );
if ($form->isValid ()) {
$formData = $form->get ( 'tasks' )->getData ();
foreach ( $formData as $task ) {
$entity->addTask ( $task );
}
$em->persist ( $entity );
$em->flush ();
return $this->redirect ( $this->generateUrl ( 'worker_show', array (
'id' => $entity->getId ()
) ) );
}
return array (
'entity' => $entity,
'form' => $form->createView ()
);
}
/**
* Creates a form to add a Task to Worker entity.
*
* @param Worker $entity
* The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createAddTaskForm(Worker $entity) {
$form = $this->createForm ( new AddTaskType (), $entity, array (
'action' => $this->generateUrl ( 'task_add', array (
'id' => $entity->getId ()
) ),
'method' => 'POST'
) );
$form->add ( 'submit', 'submit', array (
'label' => 'Zeiten'
) );
return $form;
}
/**
* Displays a form to create a new Worker entity.
*
* @Route("/new/{id}/addTask", name="worker_task")
* @Method("GET")
* @Template()
*/
public function addAction($id) {
$em = $this->getDoctrine ()->getManager ();
$entity = $em->getRepository ( 'DLRGHelferBundle:Worker' )->find ( $id );
if (! $entity) {
throw $this->createNotFoundException ( 'Unable to find Worker entity.' );
}
$form = $this->createAddTaskForm ( $entity );
return array (
'entity' => $entity,
'form' => $form->createView ()
);
}
我的AddTaskType如下所示:
namespace DLRG\HelferBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use DLRG\HelferBundle\Controller\HelferController;
class AddTaskType extends AbstractType {
/**
*
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add ( 'tasks', 'entity', array (
'class' => 'DLRGHelferBundle:Task',
'property' => 'name',
'multiple' => true
) );
}
/**
*
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults ( array (
'data_class' => 'DLRG\HelferBundle\Entity\Worker'
) );
}
/**
*
* @return string
*/
public function getName() {
return 'dlrg_helferbundle_addTask';
}
}