Doctrine:引用的对象始终为null OneToMany / ManyToOne级联

时间:2014-10-12 21:47:44

标签: php symfony doctrine

请告诉我为什么“问题”表中字段'pool_id'的值始终为NULL? (在使用JS的表格中添加fiels'Duestion')

Pool.php

use Acme\ExamBundle\Entity\Question;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Pool
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

     /**
     * @ORM\Column(type="string")
     */
    protected $name;

/**
* @ORM\OneToMany(targetEntity="Question", mappedBy="pools",cascade={"persist"})
*/

    protected $questions;

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

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

       public function getQuestions()
    {
        return $this->questions;
    }


       /**
     * Add question
     *
     * @param \Acme\ExamBundle\Entity\Question $question
     * @return Pool
     */


        public function addQuestion(Question $question)
    {
        /*$question->addPool($this);
        $this->questions->add($question);*/
                if (!$this->questions->contains($question)) {
        $this->questions->add($question);
  }



    }

           /**
     * Remove question
     *
     * @param \Acme\ExamBundle\Entity\Question $question
     */

    public function removeQuestion(Question $question)
    {
        $this->questions->removeElement($question);
    }


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set questions
     *
     * @param \Acme\ExamBundle\Entity\Question $questions
     * @return Pool
     */
    public function setQuestions(Question $questions = null)
    {
        $this->questions = $questions;

        return $this;
    }
}

Question.php

/**
 * @ORM\Entity
 */

class Question
{
     /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="text")
     */
    protected $description;

    /**
     * @ORM\Column(type="string")
     */
    protected $variants;

    /**
     * @ORM\Column(type="string")
     */
    protected $ansver;

 /**
 * @ORM\ManyToOne(targetEntity="Pool", inversedBy="questions")
 * @ORM\JoinColumn(name="pool_Id", referencedColumnName="id")
 */
    protected $pools;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set description
     *
     * @param string $description
     * @return Question
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set variants
     *
     * @param string $variants
     * @return Question
     */
    public function setVariants($variants='ok')
    {
        $this->variants = $variants;

        return $this;
    }

    /**
     * Get variants
     *
     * @return string 
     */
    public function getVariants()
    {
        return $this->variants;
    }

    /**
     * Set ansver
     *
     * @param string $ansver
     * @return Question
     */
    public function setAnsver($ansver)
    {
        $this->ansver = $ansver;

        return $this;
    }

    /**
     * Get ansver
     *
     * @return string 
     */
    public function getAnsver()
    {
        return $this->ansver;
    }


    /**
     * Set pools
     *
     * @param string $pools
     * @return Question
     */
    public function setPools($pools)
    {
        $this->pools = $pools;

        return $this;
    }

    /**
     * Get pools
     *
     * @return string 
     */
    public function getPools()
    {
        return $this->pools;
    }

}

QuestionType

class QuestionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('description');
        $builder->add('variants');
        $builder->add('ansver');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\ExamBundle\Entity\Question',
        ));
    }

        public function getName()
    {
        return 'question';
    }
}

池类型

class PoolType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name');

        $builder->add('questions', 'collection', array(
            'type' => new QuestionType(),
            'allow_add'    => true,
            'allow_delete' => true,
            'by_reference' => false,
            ));
    }

        public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\ExamBundle\Entity\Pool',
        ));
    }

        public function getName()
    {
        return 'pool';
    }
}

感谢您的想法。希望你能帮帮我。

1 个答案:

答案 0 :(得分:0)

class Pool
/**
 * @ORM\OneToMany(targetEntity="Question", mappedBy="pools",cascade={"persist"})
*/
protected $questions;

class Question
/**
 * @ORM\ManyToOne(targetEntity="Pool", inversedBy="questions")
 * @ORM\JoinColumn(name="pool_Id", referencedColumnName="id")
*/
protected $pools;

使用$ pools表示一个池有点令人困惑。您是否理解OneToMany关系意味着问题最多只能属于一个池?

Question :: pool_id为null的原因是您没有调用Question :: setPools。通过调整Pool:addQuestion

来解决这个问题
class Pool
public function addQuestion(Question $question)
{
    /*$question->addPool($this);
    $this->questions->add($question);*/
    if (!$this->questions->contains($question)) {
        $this->questions->add($question);
        $question->setPools($this);  // ADD THIS LINE
    }
}

看起来你几乎已经在那里了。也许你改变了ManyToMany的关系?

另外,将列名从pool_Id更改为pool_id。处理混合大小写的名称时,SQL并不总是一致的。

最后,可能想要将答案改为答案