【问题标题】:Symfony2 and Doctrine, Column cannot be null with OneToOne relationshipsSymfony2 和 Doctrine,具有 OneToOne 关系的列不能为空
【发布时间】:2013-01-06 02:53:03
【问题描述】:

这是一个实体(已编辑:完整文件内容

// 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 连接名称)

【问题讨论】:

  • 请发布 invTypes 和 MoonMaterial 的完整类。错误显然不在你想的地方
  • 看看 EDITED 代码,我改了
  • 那你怎么看?麻烦在哪里?有什么想法吗?

标签: symfony orm doctrine one-to-one


【解决方案1】:

如果你想允许设置空值

@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,则相反。

【讨论】:

  • 正如我所说,我需要该关系(invTypes 表)只是为了通过“id”获取“名称”,在任何情况下都不能删除\添加\编辑我不明白你的代码会帮助我
【解决方案2】:

我会检查您实体中的 setTypeID() 方法,这很可能没有按照您的预期设置属性,因此为 null。

【讨论】:

    【解决方案3】:

    $invTypes_byTypeID$typeID 属性映射到相同的 typeID 列。你不能用教义来做到这一点。

    我建议您删除 $typeID 属性,而不是使用“类型 ID”,而是使用类型实例并将其设置为 $invTypes_byTypeID 属性。

    我猜这意味着改变你的形式等等......

    【讨论】:

    • 所以我必须在 MoonMaterial 中更改 $typeID (name),就这样?
    • 我将所有 typeID 名称更改为 userTypeID 并且....SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'userTypeID' cannot be null 上仍然有错误
    【解决方案4】:

    我不知道为什么,我不能做单向关系,所以我通过在 invTypes 表注释中添加一些代码来解决它的双向问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多