dtr87341 2016-12-29 18:07
浏览 46
已采纳

Symfony ManyToMany具有实体类型

I have a problem with my code. I have this error :

The property "users" in class "Wiki\AdminBundle\Entity\Post" can be defined with the methods "addUser()", "removeUser()" but the new value must be an array or an instance of \Traversable, "Wiki\AdminBundle\Entity\User" given. 

I don't understand what's happened with my code.

This is my FormType :

<?php

namespace Wiki\AdminBundle\Form;

use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Tests\Fixtures\Entity;
use Wiki\AdminBundle\Entity\Post;

class PostType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title')
            ->add('users', EntityType::class, [
                'class' => 'WikiAdminBundle:User',
                'choice_label' => 'username',
            ])
            ->add('status')
            ->add('save', SubmitType::class)
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Post::class,
        ]);
    }
}

And here my Post Entity :

<?php

namespace Wiki\AdminBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Wiki\AdminBundle\Traits\Entity as EntityTraits;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="post")
 * @ORM\Entity(repositoryClass="Wiki\AdminBundle\Entity\Repository\PostRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Post
{
    use EntityTraits\IdTrait;
    use EntityTraits\DateCreated;

    /**
     * @ORM\Column(name="title", type="text")
     */
    private $title;

    /**
     * @ORM\ManyToMany(targetEntity="Wiki\AdminBundle\Entity\User", mappedBy="posts", cascade={"persist"})
     * @ORM\Column(name="author", type="text")
     */
    private $users;

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

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

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

    /**
     * @param mixed $title
     */
    public function setTitle($title)
    {
        $this->title = $title;
    }

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

    /**
     * @param mixed $status
     */
    public function setStatus($status)
    {
        $this->status = $status;
    }

    public function addUser(User $user)
    {
        $this->users->add($user);
        $user->addPost($this);
    }

    public function removeUser(User $user)
    {
        $this->users->removeElement($user);
    }

    /**
     * @return mixed
     */
    public function getUsers()
    {
        return $this->users;
    }
}

Can someone help me to resolve my problem ?

best regards,

  • 写回答

1条回答 默认 最新

  • doubi6898 2016-12-30 05:10
    关注

    It requires array while storing data in datatable but you have passed user object. Add 'multiple' => true in form builder for 'users`.

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title')
            ->add('users', EntityType::class, [
                'class' => 'WikiAdminBundle:User',
                'multiple' => true,
                'choice_label' => 'username',
            ])
            ->add('status')
            ->add('save', SubmitType::class)
        ;
    }
    

    Note: 'multiple' => true will create array type control instead of object.

    Ref Here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化