symfony2:无法持久化实体并插入数据库

时间:2014-09-21 12:14:02

标签: php symfony doctrine

我是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();
    }

我尝试使用构造函数设置报告属性: 极致\ 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();

    }

最后是控制器 极致/ 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 

那么,使用构造函数设置的属性是什么问题?为什么user_id被保留,但organization_id和year_id没有?

1 个答案:

答案 0 :(得分: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;




}
相关问题