本文介绍了Symfony2 和 Doctrine,具有 OneToOne 关系的列不能为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个实体(已完整文件内容)

// EveMoonBundleEntityMoonMaterial.php

namespace EveMoonBundleEntity;

use DoctrineORMMapping as ORM;

//use DoctrineCommonCollectionsArrayCollection;

/**
 * @ORMTable(name="_moon_material")
 * @ORMEntity()
 */
class MoonMaterial
{

    public function __construct()
    {
        //$this->invTypes_byTypeID = new ArrayCollection();
    }

    // relations start

    /**
     * @ORMOneToOne(targetEntity="EveDumpBundleEntityinvTypes")
     * @ORMJoinColumn(name="typeID", referencedColumnName="typeID")
     */
    private $invTypes_byTypeID;

    public function get_invTypes_byTypeID()
    {
        return $this->invTypes_byTypeID;
    }

    // relations end

    /**
     * @ORMColumn(name="userID", type="integer", nullable=false)
     * @ORMId
     * @ORMGeneratedValue(strategy="NONE")
     */
    private $userID;

    /**
     * @ORMColumn(name="moonID", type="integer", nullable=false)
     * @ORMId
     * @ORMGeneratedValue(strategy="NONE")
     */
    private $moonID;

    /**
     * @ORMColumn(name="typeID", type="integer", nullable=false)
     * @ORMId
     * @ORMGeneratedValue(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;
    }

}

控制器中的代码(已编辑)

// EveMoonBundleControllerIndexController.php
namespace EveMoonBundleController;

use SymfonyBundleFrameworkBundleControllerController;
use EveMoonBundleEntityMoonMaterial;

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()));
    }

}

当我尝试这个时,我得到了错误

when i try this, i get error

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 关系......请帮助处理它

but when i comment the OneToOne (code above) relationship it work correctly, so main trouble is in description OneToOne relationship... pls help to deal with it

我想要什么,如果表_moon_material"中存在,我想重写条目,如果不存在则直接写

what i want, i want rewrite entry if exist in table "_moon_material" or simply write it if not

ps:我只需要读取"的关系(通过 id 连接名称)

ps: i need that relation only for "read" (join name by id)

推荐答案

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

i don't know why, i can't do unidirectional relations, so i solved it by bidirectional by adding some code to invTypes table annotation

这篇关于Symfony2 和 Doctrine,具有 OneToOne 关系的列不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-14 00:15