我是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''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();
}
最后是控制器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 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却没有?
我自己解决了。解决方案不是手动设置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;
}