Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot flush data into child Table using doctrine
    primarykey
    data
    text
    <p>Following is my Code snippet, what i am trying to do is insert ID of Parent Table to child table along with inserting Data to the Child table at same time. Following is my code Snippet</p> <pre><code>public function addAction() { $ViewModel = new ViewModel(); $form = new TestForm(); $form-&gt;get('submit')-&gt;setValue('Add'); $request = $this-&gt;getRequest(); if ($request-&gt;isPost()) { $TestFilter = new TestFilter(); $test = $this-&gt;getServiceLocator()-&gt;get('Test'); $form-&gt;setInputFilter($TestFilter-&gt;getInputFilter()); $form-&gt;setData($request-&gt;getPost()); if ($form-&gt;isValid()) { $test-&gt;populate($form-&gt;getData()); $this-&gt;getEntityManager()-&gt;persist($test); $this-&gt;getEntityManager()-&gt;flush(); $TestDetail = $this-&gt;getServiceLocator()-&gt;get('TestDetail'); $TestDetail-&gt;populate($form-&gt;getData()); $TestDetail-&gt;setTest($test); $this-&gt;getEntityManager()-&gt;persist($TestDetail); $this-&gt;getEntityManager()-&gt;flush(); return $this-&gt;redirect()-&gt;toRoute('test'); } } return array('form' =&gt; $form); } </code></pre> <p><strong>My Entities are as Follows</strong></p> <pre><code>&lt;?php namespace Test\Entity; use Doctrine\ORM\Mapping as ORM; use Zend\Form\Annotation; use Test\Entity\Test; /** * An Test entity. * * @ORM\Entity * @ORM\Table(name="testdetail") * * @property int $id * @property string $fname * @property string $lname * @property string $description * @property datetime $creation_date * @property datetime $modification_date * * @Annotation\Name("Test_Detail") * */ class TestDetail { /** * @ORM\Id * @ORM\Column(type="integer"); * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(type="string") * * @Annotation\Required(true) */ protected $first_name; /** * @ORM\Column(type="string") * * @Annotation\Required(true) */ protected $last_name; /** * @ORM\Column(type="string") * * @Annotation\Required(true) */ protected $description; /** * @ORM\ManyToOne(targetEntity="TestDetail", inversedBy="test_details") * * @Annotation\Required(true) */ protected $test; public function populate($data) { $this-&gt;id = isset($data['id']) ? $data['id'] : $this-&gt;id; $this-&gt;first_name = isset($data['first_name']) ? $data['first_name'] : $this-&gt;first_name; $this-&gt;last_name = isset($data['last_name']) ? $data['last_name'] : $this-&gt;last_name; $this-&gt;description = isset($data['description']) ? $data['description'] : $this-&gt;description; } /* * Constructor */ public function __construct() { $now = new \DateTime("now"); $this-&gt;modification_date = $now; } /** * Magic getter to retrieve protected properties. * * @param string $property */ public function __get($property) { return $this-&gt;$property; } /** * Magic setter to save protected properties. * * @param string $property * @param mixed $value */ public function __set($property, $value) { $this-&gt;$property = $value; } public function setTest(Test $test) { $this-&gt;test = $test; } } ?&gt; &lt;?php namespace Test\Entity; use Doctrine\ORM\Mapping as ORM; use Zend\Form\Annotation; /** * An Test entity. * * @ORM\Entity * @ORM\Table(name="test") * * @property int $id * @property string $name * @property string $address * @property smallint $status * @property datetime $creation_date * @property datetime $modification_date * * @Annotation\Name("Test") * */ class Test { /** * @ORM\Id * @ORM\Column(type="integer"); * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(type="string") * * @Annotation\Required(true) */ protected $name; /** * @ORM\Column(type="string") * * @Annotation\Required(true) */ protected $status; /** * @ORM\Column(type="datetime") * * @Annotation\Required(false) */ protected $creation_date; /** * @ORM\Column(type="datetime") * * @Annotation\Required(false) */ protected $modification_date; /** * @ORM\OneToMany(targetEntity="TestDetail", mappedBy="test", orphanRemoval=true) * * @Annotation\Required(false) */ protected $test_details; public function populate($data) { $this-&gt;id = isset($data['id']) ? $data['id'] : $this-&gt;id; $this-&gt;name = isset($data['name']) ? $data['name'] : $this-&gt;name; $this-&gt;status = isset($data['status']) ? $data['status'] : $this-&gt;status; } /* * Constructor */ public function __construct() { $now = new \DateTime("now"); $this-&gt;creation_date = $now; $this-&gt;modification_date = $now; } /** * Magic getter to retrieve protected properties. * * @param string $property */ public function __get($property) { return $this-&gt;$property; } /** * Magic setter to save protected properties. * * @param string $property * @param mixed $value */ public function __set($property, $value) { $this-&gt;$property = $value; } } </code></pre> <p><strong>I am getting this error after Flush</strong></p> <pre><code>Found entity of type Test\Entity\Test on association Test\Entity\TestDetail#test, but expecting Test\Entity\TestDetail </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload