2014-09-21 64 views
0

我是Symfony2的新手,所以請幫我解決它。有實體報告和實體組織。symfony2:無法堅持實體並插入數據庫

報告映射的樣子:

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

,並組織部分:

/** 
    * @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(); 
    } 

我嘗試使用構造函數設置報告屬性: 的Acme \ ReportsBundle \實體\報告

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(); 

    } 

最後控制器 Acme/ReportsBundle /控制器/ 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 that all properties were set 

      $em = $this->getDoctrine()->getManager(); 

      $em->persist($report); 

      $em->flush(); 

     } 

所以,參觀這條路線,我得到:

An exception occurred while executing 'INSERT INTO reports_report (organization_id, year_id, status, user_created, user_updated, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)' with 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 be null 

那麼,什麼是錯與性能whitch使用構造函數設置?爲什麼user_id是持久的,但是organization_id和year_id不是?

回答

0

我自己解決了。解決方案不是手動設置Report organization_id或year_id,而只是在構造函數中引用屬性($ organization,$ year)。 Acme/ReportBundle/Entity/Report.php的構造函數應該如下所示:

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; 




}