2016-04-09 6 views
1

Wie kann ich die Task-ID für die TaskLine-Entität festlegen?So legen Sie die übergeordnete ID für eine untergeordnete Entität beim Übermitteln des symfony3-Formulars fest

ähnlichen Fragen:

  1. Doctrine: How to insert foreign key value
  2. Best practice for inserting objects with foreign keys in symfony2
  3. Doctrine 2 entity association does not set value for foreign key

ich diesen Fehler:

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" 

HINWEIS: Normalerweise I w Ork mit PropelORM.

Ich versuche, eine neue TaskLine-Entität zu speichern, die mit der Entität Task verknüpft ist. Ich poste JSON-Payload, die ungefähr so ​​aussieht.

{ 
    "id": null, 
    "task_id": 1, 
    "note" : "new note" 
} 

In der Steuerung ich die Anfrage Nutzlast json_decode und tragen vor, zu $form->submit($note_data) ist $ form eine Instanz:

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' 
     )); 
    } 
} 

Hier ist meine Aufgabe Einheit ist

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 Einheit

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; 
    } 
} 

Antwort

1

fand ich meine Antwort hier: Best Practice for inserting objects with foreign keys in Symfony2

Beantwortet von: Tuan nguyen

In ORM you have to set Foreign key by an object which your entity associated with. You could use EntityManager#getReference to get a reference to category entity without loading it from DB. Like this

$category = $entityManager->getReference('YourProject\Entity\Category', $categoryId); 
$product = new Product(); 
$product->setCategory($category); 

ähnlichen Fragen:

  1. Doctrine: How to insert foreign key value

Following function you should have already in your Slider entity (or similar).

public function addImage(Image $image) { 
$image->setSlider($this); // This is the line you're probably looking for 
$this->images[] = $image; 

return $this; } 

What it does is if you persist the entity it writes the ID of the Slider (sid) into your Image.

  1. Doctrine 2 entity association does not set value for foreign key

I found something in the Doctrine 2 documentation:

Changes made only to the inverse side of an association are ignored. Make sure to update both sides of a bidirectional association (or at least the owning side, from Doctrine’s point of view) As in my case the owning side is the User I must update it. Doctrine 1 was able to manage it automatically... too bad.

Verwandte Themen