Symfony在关系级联中持续失败

时间:2014-01-15 09:19:56

标签: symfony doctrine entity-relationship

你好,当我试图坚持我的实体时,我有这个错误:

An exception occurred while executing 'INSERT INTO item_details (item_id, item_type,    name, start_date, end_date, description, pre_requisites, picture, acquired_knowledges,   status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params [null, null, "ertret",   "09\/01\/2014", "17\/01\/2014", "ter", "trtre", {}, "trtre", null]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'item_id' cannot be null

课程实体代码:

<?php

namespace Mooc\AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Course
 *
 * @ORM\Table(name="course")
 * @ORM\Entity
 */
class Course
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\Column(name="language_id", type="integer", nullable=false)
     */
    private $languageId;

    /**
     * @var integer
     *
     * @ORM\Column(name="category_id", type="integer", nullable=false)
     */
    private $categoryId;

    /**
     * @ORM\OneToMany(targetEntity="ItemDetails", mappedBy="course",cascade={"all"})
     **/

    private $details;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->details = new \Doctrine\Common\Collections\ArrayCollection();
    }

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

    /**
     * Set languageId
     *
     * @param integer $languageId
     * @return Course
     */
    public function setLanguageId($languageId)
    {
        $this->languageId = $languageId;

        return $this;
    }

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

    /**
     * Set categoryId
     *
     * @param integer $categoryId
     * @return Course
     */
    public function setCategoryId($categoryId)
    {
        $this->categoryId = $categoryId;

        return $this;
    }

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

    /**
     * Add details
     *
     * @param \Mooc\TeacherBundle\Entity\ItemDetails $details
     * @return Course
     */
    public function addDetail(\Mooc\AdminBundle\Entity\ItemDetails $details)
    {
        $this->details[] = $details;

        return $this;
    }

    /**
     * Add details
     *
     * @param \Mooc\TeacherBundle\Entity\ItemDetails $details
     * @return Course
     */
    public function setDetails(\Mooc\AdminBundle\Entity\ItemDetails $details)
    {
        $this->details[] = $details;

        return $this;
    }    

    /**
     * Remove details
     *
     * @param \Mooc\TeacherBundle\Entity\ItemDetails $details
     */
    public function removeDetail(\Mooc\AdminBundle\Entity\ItemDetails $details)
    {
        $this->details->removeElement($details);
    }

    /**
     * Get details
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getDetails()
    {
        return $this->details;
    }
}

项目详细信息实体代码:     

namespace Mooc\AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * ItemDetails
 *
 * @ORM\Table(name="item_details")
 * @ORM\Entity
 */
class ItemDetails
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\Column(name="item_id", type="integer", nullable=false)
     */
    private $itemId;

    /**
     * @var integer
     *
     * @ORM\Column(name="item_type", type="integer", nullable=false)
     */
    private $itemType;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255, nullable=false)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="start_date", type="string", length=60, nullable=false)
     */
    private $startDate;

    /**
     * @var string
     *
     * @ORM\Column(name="end_date", type="string", length=60, nullable=false)
     */
    private $endDate;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="text", nullable=false)
     */
    private $description;

    /**
     * @var string
     *
     * @ORM\Column(name="pre_requisites", type="text", nullable=false)
     */
    private $preRequisites;

    /**
     * @var string
     *
     * @ORM\Column(name="picture", type="string", length=255, nullable=false)
     */
    private $picture;

    /**
     * @var string
     *
     * @ORM\Column(name="acquired_knowledges", type="text", nullable=false)
     */
    private $acquiredKnowledges;

    /**
     * @var integer
     *
     * @ORM\Column(name="status", type="integer", nullable=false)
     */
    private $status;

    /**
     * @ORM\ManyToOne(targetEntity="Course", inversedBy="details")
     * @ORM\JoinColumn(name="item_id", referencedColumnName="id")
     **/

    private $course;

    /**
     * @ORM\OneToMany(targetEntity="Video", mappedBy="itemDetails",cascade={"all"})
     **/

    private $video;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->video = new \Doctrine\Common\Collections\ArrayCollection();
    }
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set itemId
     *
     * @param integer $itemId
     * @return ItemDetails
     */
    public function setItemId($itemId)
    {
        $this->itemId = $itemId;

        return $this;
    }

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

    /**
     * Set itemType
     *
     * @param integer $itemType
     * @return ItemDetails
     */
    public function setItemType($itemType)
    {
        $this->itemType = $itemType;

        return $this;
    }

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

    /**
     * Set name
     *
     * @param string $name
     * @return ItemDetails
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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

    /**
     * Set startDate
     *
     * @param string $startDate
     * @return ItemDetails
     */
    public function setStartDate($startDate)
    {
        $this->startDate = $startDate;

        return $this;
    }

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

    /**
     * Set endDate
     *
     * @param string $endDate
     * @return ItemDetails
     */
    public function setEndDate($endDate)
    {
        $this->endDate = $endDate;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set preRequisites
     *
     * @param string $preRequisites
     * @return ItemDetails
     */
    public function setPreRequisites($preRequisites)
    {
        $this->preRequisites = $preRequisites;

        return $this;
    }

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

    /**
     * Set picture
     *
     * @param string $picture
     * @return ItemDetails
     */
    public function setPicture($picture)
    {
        $this->picture = $picture;

        return $this;
    }

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

    /**
     * Set acquiredKnowledges
     *
     * @param string $acquiredKnowledges
     * @return ItemDetails
     */
    public function setAcquiredKnowledges($acquiredKnowledges)
    {
        $this->acquiredKnowledges = $acquiredKnowledges;

        return $this;
    }

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

    /**
     * Set status
     *
     * @param integer $status
     * @return ItemDetails
     */
    public function setStatus($status)
    {
        $this->status = $status;

        return $this;
    }

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



    /**
     * Set course
     *
     * @param \Mooc\AdminBundle\Entity\Course $course
     * @return ItemDetails
     */
    public function setCourse(\Mooc\AdminBundle\Entity\Course $course = null)
    {
        $this->course = $course;

        return $this;
    }

    /**
     * Get course
     *
     * @return \Mooc\AdminBundle\Entity\Course 
     */
    public function getCourse()
    {
        return $this->course;
    }

    /**
     * Add details
     *
     * @param \Mooc\TeacherBundle\Entity\ItemDetails $video
     * @return Course
     */
    public function addVideo(\Mooc\AdminBundle\Entity\Video $video)
    {
        $this->details[] = $video;

        return $this;
    }

    /**
     * Add details
     *
     * @param \Mooc\TeacherBundle\Entity\ItemDetails $video
     * @return Course
     */
    public function setVideo(\Mooc\AdminBundle\Entity\Video $video)
    {
        $this->video[] = $video;

        return $this;
    }    

    /**
     * Remove details
     *
     * @param \Mooc\TeacherBundle\Entity\ItemDetails $video
     */
    public function removeVideo(\Mooc\AdminBundle\Entity\Video $video)
    {
        $this->video->removeElement($video);
    }

    /**
     * Get details
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getVideo()
    {
        return $this->video;
    }
}

视频实体代码:     

namespace Mooc\AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Video
 *
 * @ORM\Table(name="video")
 * @ORM\Entity
 */
class Video
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\Column(name="course_id", type="integer", nullable=false)
     */
    private $courseId;

    /**
     * @var string
     *
     * @ORM\Column(name="video", type="string", length=255, nullable=false)
     */
    private $video;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="text", nullable=false)
     */
    private $description;

    /**
     * @ORM\ManyToOne(targetEntity="ItemDetails")
     * @ORM\JoinColumn(name="course_id", referencedColumnName="id")
     **/

    private $itemDetails;

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

    /**
     * Set courseId
     *
     * @param integer $courseId
     * @return Video
     */
    public function setCourseId($courseId)
    {
        $this->courseId = $courseId;

        return $this;
    }

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

    /**
     * Set video
     *
     * @param string $video
     * @return Video
     */
    public function setVideo($video)
    {
        $this->video = $video;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set itemDetails
     *
     * @param \Mooc\AdminBundle\Entity\ItemDetails $itemDetails
     * @return Video
     */
    public function setItemDetails(\Mooc\AdminBundle\Entity\ItemDetails $itemDetails = null)
    {
        $this->itemDetails = $itemDetails;

        return $this;
    }

    /**
     * Get itemDetails
     *
     * @return \Mooc\AdminBundle\Entity\ItemDetails 
     */
    public function getItemDetails()
    {
        return $this->itemDetails;
    }
}

课程表格代码:     

namespace Mooc\AdminBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Doctrine\ORM\EntityRepository;

class CourseType extends AbstractType
{
    private $languageId;

    public function __construct($languageId) {

        $this->languageId=$languageId;

    }
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $session = new Session();

        $languageId=$session->get('languageId');              
        $builder
            ->add('languageId', 'entity', array(
                'class' => 'MoocAdminBundle:Language',
                'property' => 'name',
                'preferred_choices' => array((object)$languageId),
            ))
            ->add('categoryId', 'entity', array(
                'class' => 'MoocAdminBundle:CourseCategory',
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('u')
                        ->where('u.languageId ='.$this->languageId);
                },
            ))
            ->add('details', new ItemDetailsType())
            ->add('submit', 'submit', array('attr' => array('class' => 'btn btn-success btn-large')));
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Mooc\AdminBundle\Entity\Course'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'mooc_adminbundle_course';
    }
}

ItemDetails表格:     

namespace Mooc\AdminBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ItemDetailsType extends AbstractType
{


        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('startDate')
            ->add('endDate')
            ->add('description')
            ->add('preRequisites')
            ->add('picture','file')
            ->add('acquiredKnowledges')
            ->add('status')
            ->add('video', new VideoType())
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Mooc\AdminBundle\Entity\ItemDetails'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'mooc_adminbundle_itemdetails';
    }
}

视频表格

<?php

namespace Mooc\AdminBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class VideoType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('courseId')
            ->add('video','file')
            ->add('description','textarea')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Mooc\AdminBundle\Entity\Video'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'mooc_adminbundle_video';
    }
}

控制器中的代码:

public function courseaddAction() {

    $locale=$this->get('session')->get('_locale');

    $language=$this->get('doctrine.orm.entity_manager')->getRepository('MoocAdminBundle:Language')->findBy(array('locale'=>$locale));
    $languageId=$language[0]->getId();

    $form=$this->createForm(new CourseType($languageId));         

    if ($this->getRequest()->isMethod("POST")) {
        $form->HandleRequest($this->getRequest());    
        //if ($form->isValid()) {                
            $course = $form->getData();
            $this->get('mooc.course')->addCourse($course);                
        //}

    }

    return $this->render('MoocTeacherBundle:Default:courseadd.html.twig', array('form'=>$form->createView(),'languageId'=>$languageId));
}

服务代码:

 public function addCourse($course) {
    $details=$course->getDetails();

    $this->em->persist($course);
    /*
    foreach($course->getDetails() as $details) {
        $details->setItemType('course');
        $this->em->persist($details);

         foreach($details->getVideo() as $video) {                
            $this->em->persist($video);
        }        
    }  
   */
    $this->em->flush();
}

1 个答案:

答案 0 :(得分:0)

您需要在表格中设置ItemId:

$builder
        ->add('name')
        ->add('startDate')
        ->add('itemId') //add this line
        ->add('endDate')
        ->add('description')
        ->add('preRequisites')
        ->add('picture','file')
        ->add('acquiredKnowledges')
        ->add('status')
        ->add('video', new VideoType())
    ;

或将可空值更改为true

    /**
     * @var integer
     *
     * @ORM\Column(name="item_id", type="integer", nullable=true) //here nullable to true
     */
    private $itemId;