我正在尝试使用 Symfony2 和 Doctrine 构建在某些站点上执行的某些操作的日志。我有 2 个表站点和日志。Logs 表将包含一个 siteid,它是 sites 表的 id 列的外键。日志表可以有同一个站点的多个日志。
当我尝试在日志表中插入一个条目时,我得到 siteid is null 错误。
这是我的代码:
网站实体:
<?php
namespace A\SHB\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Sites
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="A\SHB\Entity\SitesRepository")
*/
class Sites
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var ArrayCollection $siteLog
*
* @ORM\OneToMany(targetEntity="Logs", mappedBy="log", cascade={"persist"})
* @ORM\OrderBy({"siteid" = "ASC"})
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="log_id", referencedColumnName="siteid")
* })
*/
private $siteLog;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Constructor
*/
public function __construct()
{
$this->siteLog = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add siteLog
*
* @param \A\SHB\Entity\SiteLog $siteLog
* @return Sites
*/
public function addSiteLog(\A\SHB\Entity\SiteLog $siteLog)
{
$this->siteLog[] = $siteLog;
return $this;
}
/**
* Remove siteLog
*
* @param \A\SHB\Entity\SiteLog $siteLog
*/
public function removeSiteLog(\A\SHB\Entity\SiteLog $siteLog)
{
$this->siteLog->removeElement($siteLog);
}
/**
* Get siteLog
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getSiteLog()
{
return $this->siteLog;
}
}
记录实体:
<?php
namespace A\SHB\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Logs
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="A\SHB\Entity\LogsRepository")
*/
class Logs
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var integer
*
* @ORM\Column(name="siteid", type="integer")
*/
private $siteid;
/**
* @var integer
*
* @ORM\Column(name="dateline", type="integer")
*/
private $dateline;
/**
* @var Log
*
* @ORM\ManyToOne(targetEntity="Sites")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="site_id", referencedColumnName="id")
* })
*/
private $log;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set siteid
*
* @param integer $siteid
* @return Logs
*/
public function setSiteid($siteid)
{
$this->siteid = $siteid;
return $this;
}
/**
* Get siteid
*
* @return integer
*/
public function getSiteid()
{
return $this->siteid;
}
/**
* Set dateline
*
* @param integer $dateline
* @return Logs
*/
public function setDateline($dateline)
{
$this->dateline = $dateline;
return $this;
}
/**
* Get dateline
*
* @return integer
*/
public function getDateline()
{
return $this->dateline;
}
/**
* Set log
*
* @param \A\SHB\Entity\Log $log
* @return Logs
*/
public function setLog(\A\SHB\Entity\Log $log = null)
{
$this->log = $log;
return $this;
}
/**
* Get log
*
* @return \A\SHB\Entity\Log
*/
public function getLog()
{
return $this->log;
}
}
控制器 :
public function indexAction()
{
$sites = $this->getDoctrine()->getRepository('ASHB:Sites')->findAll();
foreach ($sites as $site)
{
$host = $site->getForum();
// Do something ....
$log = new Logs();
$log->setSiteid($site->getId());
$log->setDateline($temp['dateline']);
$em = $this->getDoctrine()->getManager();
$em->persist($log);
$em->flush();
}
return $this->render('ASHB:Default:index.html.twig', array('sites' => $output, 'counters' => $counters));
}
现在,当我运行此代码时,出现以下错误:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'siteid' cannot be null"
如果我 var_dump $log
, before $em->persist($log);
, siteid 就在那里。我不确定出了什么问题以及为什么将 siteid 设置为 null。
更新 1: 我尝试进行以下更改,但仍然出现相同的错误:
/**
* @var Log
*
* @ORM\ManyToOne(targetEntity="Sites", inversedBy="siteLog")
*/
private $log;