表格集合内的表格集合Symfony

时间:2017-08-31 11:42:11

标签: php symfony twig symfony-forms

我有三个实体Post CarousselCarousselImg。 哪个Carousel实体与post_id的Post实体相关 CarousselImg实体与caroussel_id的Caroussel实体相关。 多元一对多与一对多的关系是由学说产生的。

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

  

该物业" carousselImgs"在课堂上#34; AppBundle \ Entity \ Caroussel"可以用方法定义" addCarousselImg()"," removeCarousselImg()"但新值必须是\ Traversable的数组或实例," AppBundle \ Entity \ CarousselImg"给出。

这是我在Post Type

中的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 Type中的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;
    }
}
&#13;
&#13;
&#13;

&#13;
&#13;
<?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;
    }
}
&#13;
&#13;
&#13;

&#13;
&#13;
<?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;
    }
    
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

尝试将new CarousselImgType()替换为CarousselImgType::classadd方法的第二个参数需要具有字段的类型,而不是对象。

修改:您正在向CarousselImg而不是Caroussel的集合提供CarousselImg。您应该在Caroussel类型中执行类似的操作:

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