douyi1855 2014-07-09 18:45
浏览 37
已采纳

Symfony2来自第一实体表中第二实体形式的安全数据

how can I save the data from my created form , which is using 2 entitys to output different select and input fields, into the table of the first entity.

Main Entity is Tasks.php - in this form I get also data from 2 other Entities called TaskTypes and User.

It looks like this:

$AddTask = new Tasks();
$form = $this->createFormBuilder($AddTask)
    ->add('task_title', 'text', array('label' => 'Aufgabentitel', 'attr' => array('class' => 'first')))
    ->add('task_description', 'textarea', array('label' => 'Aufgabenbeschreibung'))
    ->add('task_types', 'entity', array(
        'class' => 'SeotoolMainBundle:TaskTypes',
        'property' => 'task_type_title',
        'label' => 'Aufgabentyp'))

    ->add('user', 'entity', array(
        'class' => 'SeotoolMainBundle:User',
        'property' => 'username',
        'label' => 'Kundenzuordnung',
        'query_builder' => function(EntityRepository $er ) use ( $request ) {
                return $er->createQueryBuilder('w')
                    ->orderBy('w.username', 'ASC')
                    ->where('w.isAdmin = 0')
                    ->andWhere('w.isActive = 1');
        }
    ))
    ->add('Aufgabe anlegen', 'submit')
    ->getForm();
$form->handleRequest($request);

if ($form->isValid()) {


    $task = new Tasks();
    $task->setTaskTitle($request->request->get('form')['task_title']);
    $task->setTaskDescription($request->request->get('form')['task_description']);

    $em = $this->getDoctrine()->getManager();
    $em->persist($task);
    $em->flush();

    return $this->redirect($this->generateUrl('add_task'));

}

My Entity/Tasks.php looks like this:

...
/**
 * @Assert\Type(type="Seotool\MainBundle\Entity\TaskTypes")
 */
protected $tasktypes;

/**
 * @Assert\Type(type="Seotool\MainBundle\Entity\User")
 */
protected $user;
...
    public function getTaskTypes()
{
    return $this->tasktypes;
}

public function setTaskTypes(TaskTypes $tasktypes = null)
{
    $this->tasktypes = $tasktypes;
}

public function getUser()
{
    return $this->user;
}

public function setUser(User $user = null)
{
    $this->user = $user;
}

I got these code snippets out of an example how to make forms with multiple entities. But I don't get it working to save the data from the "extern entity" in the table of the "main entity"

What I tried:

...
   /**
 * @Assert\Type(type="Seotool\MainBundle\Entity\TaskTypes")
 * @ORM\Column(type="integer")
 */
protected $tasktypes;

/**
 * @Assert\Type(type="Seotool\MainBundle\Entity\User")
 * @ORM\Column(type="string")
 */
protected $user;

....

public function getTaskTypes()
{
    return $this->tasktypes;
}

public function setTaskTypes()
{
   return $this->tasktypes;
}

public function getUser()
{
    return $this->user;
}

public function setUser(User $user = null)
{
    $this->user = $user;
}

It outputs following error:

Neither the property "task_types" nor one of the methods "addTaskTyp()"/"removeTaskTyp()", "addTaskType()"/"removeTaskType()", "setTaskTypes()", "taskTypes()", "__set()" or "__call()" exist and have public access in class "Seotool\MainBundle\Entity\Tasks". 500 Internal Server Error - NoSuchPropertyException

  • 写回答

1条回答 默认 最新

  • duanjue9889 2014-07-10 11:42
    关注

    There is a discrepancy between your property names, and the getter and setter methods. Regenerate your getters and setters to ensure that they are matched correctly.

    As it stands, you will not be able to add a null value into the task_type field in the database. To change this, you can declare it in your annotation:

    @ORM\Column(type="integer", nullable=true) 
    

    And then update the database schema.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题