ここにエンティティがあります(編集済み:完全なファイルコンテンツ)
// 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 の関係の説明にあります... pls はそれに対処するのを助けます
私が欲しいのは、テーブル「_moon_material」に存在する場合はエントリを書き換えるか、存在しない場合は単純に書き込むことです
ps: 「読み取り」の場合にのみその関係が必要です (名前を ID で結合)