2017-08-31 53 views
0

我有三个实体Post CarousselCarousselImg。 其中Carousel实体与邮政实体有关post_idCarousselImg实体与卡洛斯实体有关,caroussel_id。 多对一和一对多的关系是由教义产生的。收集形式Symfony收集表格

问题是当我尝试添加包含caroussel [包含carousselimg]的帖子。

在类“的appbundle \实体\ Caroussel”的属性“carousselImgs”可以与方法“addCarousselImg()”,“removeCarousselImg()”,但新的值必须是一个数组或的实例来限定\ Traversable,“AppBundle \ Entity \ CarousselImg”给出。

这里是柱式我caroussel部分

->add('caroussels',CollectionType::class, array(
       'entry_type' => CarousselType::class, 
       'allow_add' => true, 
       'delete_empty'=>true, 
       'by_reference' => false, 
       'prototype' => true, 
       'entry_options' => array(
        'attr'  => array('class' => 'caroussels-box') 
       ), 
      ) 
     ) 

这里是Caroussel型我carousselImg部分

$builder->add('title')->add('carousselImgs', new CarousselImgType()); 

感谢您的帮助

<?php 
 

 
namespace AppBundle\Entity; 
 

 
use Doctrine\ORM\Mapping as ORM; 
 

 
/** 
 
* CarousselImg 
 
* 
 
* @ORM\Table(name="caroussel_img") 
 
* @ORM\Entity(repositoryClass="AppBundle\Repository\CarousselImgRepository") 
 
*/ 
 
class CarousselImg 
 
{ 
 
    /** 
 
    * @var int 
 
    * 
 
    * @ORM\Column(name="id", type="integer") 
 
    * @ORM\Id 
 
    * @ORM\GeneratedValue(strategy="AUTO") 
 
    */ 
 
    private $id; 
 

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

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

 

 
    /** 
 
    * @ORM\ManyToOne(targetEntity="Caroussel", inversedBy="carousselImgs") 
 
    * @ORM\JoinColumn(name="caroussel_id", referencedColumnName="id") 
 
    */ 
 

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

 
    /** 
 
    * Set imgUrl 
 
    * 
 
    * @param string $imgUrl 
 
    * @return CarousselImg 
 
    */ 
 
    public function setImgUrl($imgUrl) 
 
    { 
 
     $this->imgUrl = $imgUrl; 
 

 
     return $this; 
 
    } 
 

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

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

 
     return $this; 
 
    } 
 

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

 
    /** 
 
    * Set caroussel 
 
    * 
 
    * @param \AppBundle\Entity\Caroussel $caroussel 
 
    * @return CarousselImg 
 
    */ 
 
    public function setCaroussel(\AppBundle\Entity\Caroussel $caroussel = null) 
 
    { 
 
     $this->caroussel = $caroussel; 
 

 
     return $this; 
 
    } 
 

 
    /** 
 
    * Get caroussel 
 
    * 
 
    * @return \AppBundle\Entity\Caroussel 
 
    */ 
 
    public function getCaroussel() 
 
    { 
 
     return $this->caroussel; 
 
    } 
 
}

<?php 
 

 
namespace AppBundle\Entity; 
 

 
use Doctrine\ORM\Mapping as ORM; 
 
use Doctrine\Common\Collections\ArrayCollection; 
 
/** 
 
* Caroussel 
 
* 
 
* @ORM\Table(name="caroussel") 
 
* @ORM\Entity(repositoryClass="AppBundle\Repository\CarousselRepository") 
 
*/ 
 
class Caroussel 
 
{ 
 
    /** 
 
    * @var int 
 
    * 
 
    * @ORM\Column(name="id", type="integer") 
 
    * @ORM\Id 
 
    * @ORM\GeneratedValue(strategy="AUTO") 
 
    */ 
 
    private $id; 
 

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

 
    /** 
 
    * @ORM\ManyToOne(targetEntity="Post", inversedBy="caroussels") 
 
    * @ORM\JoinColumn(name="post_id", referencedColumnName="id") 
 
    */ 
 

 
    private $post; 
 
    /** 
 
    * @ORM\OneToMany(targetEntity="CarousselImg", mappedBy="caroussel", cascade={"persist"}) 
 
    */ 
 
    private $carousselImgs; 
 

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

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

 
    /** 
 
    * Set title 
 
    * 
 
    * @param string $title 
 
    * @return Caroussel 
 
    */ 
 
    public function setTitle($title) 
 
    { 
 
     $this->title = $title; 
 

 
     return $this; 
 
    } 
 

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

 
    /** 
 
    * Set post 
 
    * 
 
    * @param \AppBundle\Entity\Post $post 
 
    * @return Caroussel 
 
    */ 
 
    public function setPost(\AppBundle\Entity\Post $post = null) 
 
    { 
 
     $this->post = $post; 
 

 
     return $this; 
 
    } 
 

 
    /** 
 
    * Get post 
 
    * 
 
    * @return \AppBundle\Entity\Post 
 
    */ 
 
    public function getPost() 
 
    { 
 
     return $this->post; 
 
    } 
 

 
    /** 
 
    * Add carousselImgs 
 
    * 
 
    * @param \AppBundle\Entity\CarousselImg $carousselImgs 
 
    * @return Caroussel 
 
    */ 
 
    public function addCarousselImg(\AppBundle\Entity\CarousselImg $carousselImgs) 
 
    { 
 
     $carousselImgs->setCaroussel($this); 
 
     $this->carousselImgs[] = $carousselImgs; 
 

 
     return $this; 
 
    } 
 

 
    /** 
 
    * Remove carousselImgs 
 
    * 
 
    * @param \AppBundle\Entity\CarousselImg $carousselImgs 
 
    */ 
 
    public function removeCarousselImg(\AppBundle\Entity\CarousselImg $carousselImgs) 
 
    { 
 
     $this->carousselImgs->removeElement($carousselImgs); 
 
    } 
 

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

<?php 
 

 
namespace AppBundle\Entity; 
 

 
use Doctrine\ORM\Mapping as ORM; 
 
use Doctrine\Common\Collections\ArrayCollection; 
 

 
/** 
 
* Post 
 
* 
 
* @ORM\Table(name="post") 
 
* @ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository") 
 
*/ 
 
class Post 
 
{ 
 
    /** 
 
    * @var int 
 
    * 
 
    * @ORM\Column(name="id", type="integer") 
 
    * @ORM\Id 
 
    * @ORM\GeneratedValue(strategy="AUTO") 
 
    */ 
 
    private $id; 
 

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

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

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

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

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

 
    /** 
 
    * @var int 
 
    * 
 
    * @ORM\Column(name="activeOrNot", type="integer") 
 
    */ 
 
    private $activeOrNot; 
 

 
    /** 
 
    * @var string 
 
    * 
 
    * @ORM\Column(name="type", type="string", length=255) 
 
    */ 
 
    private $type; 
 
    /** 
 
    * @ORM\ManyToOne(targetEntity="Category", inversedBy="posts") 
 
    * @ORM\JoinColumn(name="category_id", referencedColumnName="id") 
 
    */ 
 
    protected $category; 
 

 
    /** 
 
    * @ORM\ManyToOne(targetEntity="Seo", inversedBy="posts", cascade={"persist", "remove"}) 
 
    * @ORM\JoinColumn(name="seo_id", referencedColumnName="id") 
 
    */ 
 

 
    protected $seo; 
 

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

 
    private $videos; 
 
    /** 
 
    * @ORM\OneToMany(targetEntity="Citation", mappedBy="post", cascade={"persist"}) 
 
    */ 
 
    private $citations; 
 
    /** 
 
    * @ORM\OneToMany(targetEntity="Caroussel", mappedBy="post", cascade={"persist"}) 
 
    */ 
 
    private $caroussels; 
 

 
    public function __construct() 
 
    { 
 
     $this->videos = new ArrayCollection(); 
 
     $this->citations = new ArrayCollection(); 
 
     $this->caroussels = new ArrayCollection(); 
 
    } 
 

 

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

 
    /** 
 
    * Set title 
 
    * 
 
    * @param string $title 
 
    * @return Post 
 
    */ 
 
    public function setTitle($title) 
 
    { 
 
     $this->title = $title; 
 

 
     return $this; 
 
    } 
 

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

 
    /** 
 
    * Set subTitle 
 
    * 
 
    * @param string $subTitle 
 
    * @return Post 
 
    */ 
 
    public function setSubTitle($subTitle) 
 
    { 
 
     $this->subTitle = $subTitle; 
 

 
     return $this; 
 
    } 
 

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

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

 
     return $this; 
 
    } 
 

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

 
    /** 
 
    * Set thumble 
 
    * 
 
    * @param string $thumble 
 
    * @return Post 
 
    */ 
 
    public function setThumble($thumble) 
 
    { 
 
     $this->thumble = $thumble; 
 

 
     return $this; 
 
    } 
 

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

 
    /** 
 
    * Set postFirstImg 
 
    * 
 
    * @param string $postFirstImg 
 
    * @return Post 
 
    */ 
 
    public function setPostFirstImg($postFirstImg) 
 
    { 
 
     $this->postFirstImg = $postFirstImg; 
 

 
     return $this; 
 
    } 
 

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

 
    /** 
 
    * Set activeOrNot 
 
    * 
 
    * @param integer $activeOrNot 
 
    * @return Post 
 
    */ 
 
    public function setActiveOrNot($activeOrNot) 
 
    { 
 
     $this->activeOrNot = $activeOrNot; 
 

 
     return $this; 
 
    } 
 

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

 
    /** 
 
    * Set type 
 
    * 
 
    * @param string $type 
 
    * @return Post 
 
    */ 
 
    public function setType($type) 
 
    { 
 
     $this->type = $type; 
 

 
     return $this; 
 
    } 
 

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

 
    /** 
 
    * Set category 
 
    * 
 
    * @param \AppBundle\Entity\Category $category 
 
    * @return Post 
 
    */ 
 
    public function setCategory(\AppBundle\Entity\Category $category = null) 
 
    { 
 
     $this->category = $category; 
 

 
     return $this; 
 
    } 
 

 
    /** 
 
    * Get category 
 
    * 
 
    * @return \AppBundle\Entity\Category 
 
    */ 
 
    public function getCategory() 
 
    { 
 
     return $this->category; 
 
    } 
 

 
    /** 
 
    * Set seo 
 
    * 
 
    * @param \AppBundle\Entity\Seo $seo 
 
    * @return Post 
 
    */ 
 
    public function setSeo(\AppBundle\Entity\Seo $seo = null) 
 
    { 
 
     $this->seo = $seo; 
 

 
     return $this; 
 
    } 
 

 
    /** 
 
    * Get seo 
 
    * 
 
    * @return \AppBundle\Entity\Seo 
 
    */ 
 
    public function getSeo() 
 
    { 
 
     return $this->seo; 
 
    } 
 

 

 
    /** 
 
    * Add videos 
 
    * 
 
    * @param \AppBundle\Entity\Video $videos 
 
    * @return Post 
 
    */ 
 
    public function addVideo(\AppBundle\Entity\Video $videos) 
 
    { 
 
     $videos->setPost($this); 
 
     $this->videos[] = $videos; 
 

 
     return $this; 
 
    } 
 

 
    /** 
 
    * Remove videos 
 
    * 
 
    * @param \AppBundle\Entity\Video $videos 
 
    */ 
 
    public function removeVideo(\AppBundle\Entity\Video $videos) 
 
    { 
 
     $this->videos->removeElement($videos); 
 
    } 
 

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

 
    /** 
 
    * Add citations 
 
    * 
 
    * @param \AppBundle\Entity\Citation $citations 
 
    * @return Post 
 
    */ 
 
    public function addCitation(\AppBundle\Entity\Citation $citations) 
 
    { 
 
     $citations->setPost($this); 
 
     $this->citations[] = $citations; 
 

 
     return $this; 
 
    } 
 

 
    /** 
 
    * Remove citations 
 
    * 
 
    * @param \AppBundle\Entity\Citation $citations 
 
    */ 
 
    public function removeCitation(\AppBundle\Entity\Citation $citations) 
 
    { 
 
     $this->citations->removeElement($citations); 
 
    } 
 

 
    /** 
 
    * Get citations 
 
    * 
 
    * @return \Doctrine\Common\Collections\Collection 
 
    */ 
 
    public function getCitations() 
 
    { 
 
     return $this->citations; 
 
    } 
 
    public function __toString(){ 
 
     return $this->type; 
 
    } 
 

 
    /** 
 
    * Add caroussels 
 
    * 
 
    * @param \AppBundle\Entity\Caroussel $caroussels 
 
    * @return Post 
 
    */ 
 
    public function addCaroussel(\AppBundle\Entity\Caroussel $caroussels, \AppBundle\Entity\CarousselImg $carousselImg) 
 
    { 
 
     $caroussels->setPost($this); 
 
     $carousselImg->setCaroussel($caroussels); 
 
     $this->caroussels[] = $caroussels; 
 

 
     return $this; 
 
    } 
 

 
    /** 
 
    * Remove caroussels 
 
    * 
 
    * @param \AppBundle\Entity\Caroussel $caroussels 
 
    */ 
 
    public function removeCaroussel(\AppBundle\Entity\Caroussel $caroussels) 
 
    { 
 
     $this->caroussels->removeElement($caroussels); 
 
    } 
 

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

回答

0

尝试CarousselImgType::class更换new CarousselImgType()add方法的第二个参数期望具有您的字段的类型,而不是对象。

编辑:你正在给CarousselImg你的Caroussel,而不是CarousselImg的集合。你应该做这样的事情在你的Caroussel类型:

->add('title') 
->add('carousselImgs', CollectionType::class, array(
    'entry_type' => CarousselImgType::class, 
    //... 
) 
+0

您好感谢您的答案,但我因此未工作,同样的错误 –

+0

我们可以有类_CarousselType_和_AppBundle \实体\ Caroussel_请的全部代码? –

+0

ueah我会更新帖子 –