当我尝试通过doctrine使用symfony将值存储到DB时,doctrine会为字段生成NULL,尽管它已设置且我不知道为什么。
实体: https://github.com/Invis00/pub/blob/master/OldbCompetitionstage.php
测试控制器:
public function compTest(){
$em = $this->getDoctrine()->getManager();
$object = new OldbCompetitionstage();
$object->setName("name");
$object->setNr(2);
$object->setOldbCompetitionid(2);
// Echo tells 2
echo $object->getOldbCompetitionid();
$em->persist($object);
$em->flush();
return $this->render('base.html.twig', array( "current" => "pubmin")
);
}
表:
compstageID int(11)
name varchar(100)
nr int(11)
oldb_competitionID int(11)
startDate datetime
ipub_compstageID int(11)
Symfony Profiler告诉:
INSERT INTO oldb_competitionstage (name, nr, oldb_competitionID, startDate, ipub_compstageID) VALUES (?, ?, ?, ?, ?)
Parameters: { 1: name, 2: 2, 3: null, 4: null, 5: null }
但为什么oldb_competitionid为NULL而不是2? echo告诉我它是2。
似乎竞争的映射信息在某种程度上是造成这种行为的原因,但没有告知错误,我也没有看到它的问题。
答案 0 :(得分:1)
您的问题来自班上的$oldbCompetitionid
。它没有任何理由存在,并且因为您指定的列名与ManyToOne ORM列定义中使用的列名相同,所以它会导致NULL
。
你既不能将joinColumn定义为你的类的成员也不能 将其值设置为一个简单的列,有或没有ORM
删除它并检查类之间的关系定义。
代码中有问题的部分:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="oldb_competitionstage")
*/
class OldbCompetitionstage
{
/**
* @var integer
*
* @ORM\Column(name="compstageID", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $compstageid;
/**
* @var integer
*
* @ORM\Column(name="oldb_competitionID", type="integer", nullable=true)
*/
private $oldbCompetitionid;
/**
* Mappings
*/
/**
* @ORM\ManyToOne(targetEntity="OldbCompetition", inversedBy="compstages")
* @ORM\JoinColumn(name="oldb_competitionID", referencedColumnName="competitionID")
*/
private $oldbCompetition;
您应该在OldbCompetition
班级中找到类似的内容:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="oldb_competitionstage")
*/
class OldbCompetition
{
/**
* @var integer
*
* @ORM\Column(name="competitionID", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $compid;
/**
* @ORM\ManyToOne(targetEntity="OldbCompetition", mappedBy="oldbCompetition")
*/
private $compstages;