-2

我正在尝试使用 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;
4

2 回答 2

0

OneToMany 不需要 JoinColumn。所以它应该看起来像,根据文档

class Sites
{
    /**
     * @ORM\OneToMany(targetEntity="Logs", mappedBy="log")
     */
    private $site_log;
}

class Logs
{
    /**
     * @ORM\ManyToOne(targetEntity="Sites", inversedBy="site_log")
     * @ORM\JoinColumn(name="site_id", referencedColumnName="id")
     */
    private $log;
}

为了简单起见,orderBy 和 cascade 被忽略了。

于 2013-04-24T16:53:07.967 回答
0

你的结构有问题,

从日志实体中删除它

 /**
 * @var integer
 *
 * @ORM\Column(name="siteid", type="integer")
 */
private $siteid;

然后更换这部分

  /**
 * @var Log
 *
 * @ORM\ManyToOne(targetEntity="Sites")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="site_id", referencedColumnName="id")
 * })
 */
private $log;

  /**
 * @var Log
 *
 * @ORM\ManyToOne(targetEntity="Sites" inversedBy="logs")
 */
private $site;

并在您的网站实体中将其替换为

 /**
 * @var ArrayCollection $siteLog
 *
 * @ORM\OneToMany(targetEntity="Logs", mappedBy="logs", cascade={"persist"})
 * @ORM\OrderBy({"siteid" = "ASC"})
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="log_id", referencedColumnName="siteid")
 * })
 */
private $logs;

 /**
 * @var ArrayCollection $siteLog
 *
 * @ORM\OneToMany(targetEntity="Logs", mappedBy="site", cascade={"persist"})
 * @ORM\OrderBy({"siteid" = "ASC"})
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="log_id", referencedColumnName="id")
 * })
 */
private $logs;

然后为这些新字段使用适当的 setter 和 getter 意味着将对象传递给 setter 函数,对于它们被映射的类(如果你不知道这部分写在评论中,我也会这样做)

于 2013-08-31T22:01:32.197 回答