如何在TaskLine實體上設置task_id?如何在提交symfony3表單時在子實體上設置父標識
類似的問題:
- Doctrine: How to insert foreign key value
- Best practice for inserting objects with foreign keys in symfony2
- Doctrine 2 entity association does not set value for foreign key
我得到這個錯誤:
Neither the property "task_id" nor one of the methods "getTaskId()", "taskId()", "isTaskId()", "hasTaskId()", "__get()" exist and have public access in class "AppBundle\Entity\TaskLine"
注:通常I W與PropelORM的ork。
我在嘗試保存與任務實體相關的新TaskLine實體。我發佈的JSON負載看起來像這樣。
{
"id": null,
"task_id": 1,
"note" : "new note"
}
在我json_decode請求負載並提交給$form->submit($note_data)
控制器,$形式的一個實例:
class TaskNoteType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('task_id', NumberType::class)
->add('note', TextType::class)
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\TaskLine'
));
}
}
這是我的任務實體
class Task
{
/**
* @var string
*
* @ORM\Column(name="description", type="string", length=150, nullable=true)
*/
private $description;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
}
TaskLine實體
class TaskLine
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var \AppBundle\Entity\Task
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Task")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="task_id", referencedColumnName="id")
* })
*/
private $task;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set task
*
* @param \AppBundle\Entity\Task $task
*
* @return TaskLine
*/
public function setTask(\AppBundle\Entity\Task $task = null)
{
$this->task = $task;
return $this;
}
/**
* Get task
*
* @return \AppBundle\Entity\Task
*/
public function getTask()
{
return $this->task;
}
}