php - symfony2: unable to persist entity and insert into database -


i'm new symfony2, please me solve it. have entity report , entity organization.

report mapping looks like:

 <entity name="acme\reportsbundle\entity\report" table="reports_report">     <lifecycle-callbacks>         <lifecycle-callback type="prepersist" method="setcreatedat"/>         <lifecycle-callback type="prepersist" method="setupdatedat"/>     </lifecycle-callbacks>         <many-to-one field="organization" target-entity="acme\organizationbundle\entity\organization" inversed-by="reports">         <join-column name="organization_id" referenced-column-name="id" />         </many-to-one>     <many-to-one field="year" target-entity="acme\reportsbundle\entity\year" inversed-by="reports">         <join-column name="year_id" referenced-column-name="id" />     </many-to-one>     <id name="id" type="integer" column="id">         <generator strategy="identity"/>     </id>      <field name="organization_id" type="integer" lenght="11" nullable="false" />     <field name="year_id" type="integer" lenght="11" nullable="false" />     <field name="status" type="smallint" nullable="true" />     <field name="user_created" type="integer" lenght="11" nullable="false" />     <field name="user_updated" type="integer" lenght="11" nullable="false" />     <field name="created_at" type="datetime" nullable="false" />     <field name="updated_at" type="datetime" nullable="false" /> </entity> 

and organization part:

 /**      * @orm\onetomany(targetentity="acme\reportsbundle\entity\report", mappedby="organizations")      *       */     protected $reports;      /**      * constructor      */     public function __construct()     {         $this->users = new \doctrine\common\collections\arraycollection();         $this->reports = new \doctrine\common\collections\arraycollection();     } 

i'm trying set report properties using constructor: acme\reportsbundle\entity\report

public function __construct(\acme\organizationbundle\entity\organization $organization, \acme\reportsbundle\entity\year $year, \application\sonata\userbundle\entity\user $user)     {        $this->organization_id = $organization->getid();         $this->year_id = $year->getid();         $this->user_created = $user->getid();         $this->user_updated = $user->getid();      } 

and controller acme/reportsbundle/controller/defaultcontroller:

public function indexcreate()     {         $organization = $this->getdoctrine()              ->getrepository('acmeorganizationbundle:organization')              ->find(2);          $year = $this->getdoctrine()              ->getrepository('acmereportsbundle:year')              ->find(888);          $user = $this->getdoctrine()              ->getrepository('applicationsonatauserbundle:user')              ->find(20);              $report = new report($organization, $year, $user);              // var_dump shows properties set              $em = $this->getdoctrine()->getmanager();              $em->persist($report);              $em->flush();           } 

so, visiting route get:

an exception occurred while executing 'insert reports_report (organization_id, year_id, status, user_created, user_updated, created_at, updated_at) values (?, ?, ?, ?, ?, ?, ?)' params [null, null, null, 20, 20, "2014-09-21 15:07:41", "2014-09-21 15:07:41"]:  sqlstate[23000]: integrity constraint violation: 1048 column 'organization_id' cannot null  

so, whats wrong properties whitch set using constructor? why user_id persisted, organization_id , year_id not?

i resolved myself. solution not set report organization_id or year_id manually, referencing properties ($organization, $year) in constructor. acme/reportbundle/entity/report.php constructor should like:

public function __construct(\acme\organizationbundle\entity\organization $organization, \acme\reportsbundle\entity\year $year, \application\sonata\userbundle\entity\user $user)     {         $this-organization = $organization;        $this->year = $year;        $this->user_created = $user->getid();        $this->user_updated = $user->getid();            $this->status = 2;     } 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -