【问题标题】:Collection of Forms inside Collection of Forms SymfonySymfony 表单集合中的表单集合
【发布时间】:2017-08-31 11:42:10
【问题描述】:

我有三个实体Post CarousselCarousselImg。 其中Carousel 实体与post_id 的Post 实体相关 CarousselImg 实体与 caroussel_id 的 Carousel 实体相关。 多对一和一对多的关系是由教义产生的。

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

“AppBundle\Entity\Caroussel”类中的属性“carousselImgs”可以使用“addCarousselImg()”、“removeCarousselImg()”方法定义,但新值必须是数组或\Traversable的实例,“ AppBundle\Entity\CarousselImg" 给出。

这是我在帖子类型中的轮播部分

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

<?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;
    }
    
}

【问题讨论】:

    标签: php symfony twig symfony-forms


    【解决方案1】:

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

    编辑:您将CarousselImg 提供给您的Caroussel,而不是CarousselImg 的集合。你应该在 Caroussel 类型中做类似的事情:

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

    【讨论】:

    • 你好,谢谢你的回答,但我没有工作同样的错误
    • 能否请教一下 CarousselTypeAppBundle\Entity\Caroussel 类的完整代码?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 2015-09-02
    • 1970-01-01
    • 2015-01-14
    • 2016-11-29
    • 1970-01-01
    相关资源
    最近更新 更多