2014-09-21 86 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; 




}