这是一个实体(已编辑:完整文件内容)
// Eve\MoonBundle\Entity\MoonMaterial.php
namespace Eve\MoonBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
//use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Table(name="_moon_material")
* @ORM\Entity()
*/
class MoonMaterial
{
public function __construct()
{
//$this->invTypes_byTypeID = new ArrayCollection();
}
// relations start
/**
* @ORM\OneToOne(targetEntity="Eve\DumpBundle\Entity\invTypes")
* @ORM\JoinColumn(name="typeID", referencedColumnName="typeID")
*/
private $invTypes_byTypeID;
public function get_invTypes_byTypeID()
{
return $this->invTypes_byTypeID;
}
// relations end
/**
* @ORM\Column(name="userID", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
*/
private $userID;
/**
* @ORM\Column(name="moonID", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
*/
private $moonID;
/**
* @ORM\Column(name="typeID", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
*/
private $typeID;
/**
* Set userID
*
* @param integer $userID
* @return MoonMaterial
*/
public function setUserID($userID)
{
$this->userID = $userID;
return $this;
}
/**
* Get userID
*
* @return integer
*/
public function getUserID()
{
return $this->userID;
}
/**
* Set moonID
*
* @param integer $moonID
* @return MoonMaterial
*/
public function setMoonID($moonID)
{
$this->moonID = $moonID;
return $this;
}
/**
* Get moonID
*
* @return integer
*/
public function getMoonID()
{
return $this->moonID;
}
/**
* Set typeID
*
* @param integer $typeID
* @return MoonMaterial
*/
public function setTypeID($typeID)
{
$this->typeID = $typeID;
return $this;
}
/**
* Get typeID
*
* @return integer
*/
public function getTypeID()
{
return $this->typeID;
}
}
控制器中的代码(已编辑)
// Eve\MoonBundle\Controller\IndexController.php
namespace Eve\MoonBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Eve\MoonBundle\Entity\MoonMaterial;
class IndexController extends Controller
{
public function defaultAction($moonName)
{
// ...
$dc = $this->getDoctrine();
$form = $this
->createFormBuilder()
->add('typeID', 'choice', $choiceSettings)
->getForm();
if ($this->getRequest()->isMethod('post'))
{
$form->bind($this->getRequest());
{
$data = $form->getData();
$typeID = $data['typeID'];
if (is_int($typeID))
{
$em = $dc->getEntityManager();
$mm = $em->getRepository('EveMoonBundle:MoonMaterial');
$result = $mm->findOneBy(array(
'userID' => $this->getUser()->getID(),
'typeID' => $typeID,
'moonID' => $moon->getItemID()));
if ($result)
{
$em->remove($result);
$em->flush();
}
$moonMaterial = new MoonMaterial();
$moonMaterial
->setUserID($this->getUser()->getID())
->setTypeID($typeID)
->setMoonID($moon->getItemID());
$em->persist($moonMaterial);
$em->flush();
}
}
}
$twig = 'EveMoonBundle:Index:default.html.twig';
return $this->render($twig, array(
'formAddMoonMaterial' => $form->createView()));
}
}
当我尝试这个时,我得到错误
An exception occurred while executing 'INSERT INTO _moon_material (userID, moonID, typeID) VALUES (?, ?, ?)' with params {"1":38,"2":40001583,"3":null}:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'typeID' cannot be null
但是当我评论OneToOne(上面的代码)关系时它正常工作,所以主要的麻烦在于描述OneToOne关系...请帮忙处理它
我想要什么,我想要重写条目,如果存在于表“_moon_material”或只是写它,如果不是
ps:我只需要“读取”(按ID加入名称)
答案 0 :(得分:7)
如果要允许设置空值
@ORM\JoinColumn(name="typeID", referencedColumnName="typeID")
到
@ORM\JoinColumn(name="typeID", referencedColumnName="typeID", nullable=true)
你应该让列'typeID'也可以为空。 (见How do I modify a MySQL column to allow NULL?)
如果您不想拥有NULL,请执行相反的操作。
答案 1 :(得分:1)
我不知道为什么,我不能做单向关系,所以我通过向invTypes表注释添加一些代码来双向解决它
答案 2 :(得分:0)
我会在您的实体中检查方法setTypeID(),这可能是因为您未按预期设置属性,因此为null。
答案 3 :(得分:0)
$invTypes_byTypeID
和$typeID
属性都映射到同一typeID
列。你不能用学说来做到这一点。
我建议您删除$typeID
属性,而不是使用'type id',而是使用类型实例并将其设置为$invTypes_byTypeID
属性。
我想这意味着要改变你的形式......