当form具有entitytype

时间:2016-10-24 21:01:21

标签: php symfony doctrine dql

所以我在我的网站上建立了一个消息系统,我已经消息实体与收件箱实现了多对多的实体,哪个应该存储message_id和receiver_id。它的beacouse用户应该能够向许多用户发送消息

我已完成所有关系,但是,当提交表单时,我收到错误:

  

类型" Doctrine \ Common \ Collections \ Collection | array"的预期值用于关联字段" DEERCMS \ MessageBundle \ Entity \ Message#$ receiver",得到" PTB \ UserBundle \ Entity \ User"代替。

此处的形式,'multiple' => true正如我所说,用户应该能够向其他许多用户发送消息

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
            ->add('receiver', EntityType::class, array(
                'class' => 'PTBUserBundle:User',
                'choice_label' => 'email',
                 'multiple' => true,
                 'expanded' => false,    
            ))
            ->add('title')
            ->add('message', TextareaType::class);
}

我的实体 的消息

/**
* Message
*
* @ORM\Table()
* @ORM\Entity()
*/

class Message
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     */
    private $sender;

    /**
     * @ORM\ManyToMany(targetEntity="DEERCMS\MessageBundle\Entity\Inbox")
     * @ORM\JoinTable(name="message_receivers",
     *      joinColumns={@ORM\JoinColumn(name="message_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="receiver_id", referencedColumnName="id", unique=true)}
     *      )
     */
    private $receiver;
    /**
     * @ORM\Column type="string")
     */
    private $title;

    /**
     * @ORM\Column(type="string", length=4000)
     */
    private $message;

    /**
     * @ORM\Column(type="date")
     */
    private $date;

    /**
     * @ORM\Column(type="boolean")
     */
    private $displayed;


    public function __construct() 
    {
        $this->date = new \DateTime();
        $this->receiver = new \Doctrine\Common\Collectionsn\ArrayCollection();
    }

    //...     

    /**
     * Set sender
     *
     * @param integer $sender
     *
     * @return Message
     */
    public function setSender($sender)
    {
        $this->sender = $sender;

        return $this;
    }

    /**
     * Get sender
     *
     * @return int
     */
    public function getSender()
    {
        return $this->sender;
    }

    /**
     * Set receiver
     *
     * @param integer $receiver
     *
     * @return Message
     */
    public function setReceiver($receiver)
    {
        $this->receiver = $receiver;

        return $this;
    }

    /**
     * Get receiver
     *
     * @return int
     */
    public function getReceiver()
    {
        return $this->receiver;
    }



    /**
     * Add receiver
     *
     * @param \DEERCMS\MessageBundle\Entity\Inbox $receiver
     *
     * @return Message
     */
    public function addReceiver(\DEERCMS\MessageBundle\Entity\Inbox $receiver)
    {
        $this->receiver[] = $receiver;

        return $this;
    }

    /**
     * Remove receiver
     *
     * @param \DEERCMS\MessageBundle\Entity\Inbox $receiver
     */
    public function removeReceiver(\DEERCMS\MessageBundle\Entity\Inbox $receiver)
    {
        $this->receiver->removeElement($receiver);
    }
}

收件箱

/**
 * Inbox
 *
 * @ORM\Table(name="inbox")
 * @ORM\Entity(repositoryClass="DEERCMS\MessageBundle\Repository\InboxRepository")
 */
class Inbox
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


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

1 个答案:

答案 0 :(得分:1)

我自己解决了。所以基本上目标实体是错误的,应该在消息和用户之间定义关系,这是它的样子

 /**
 * @ORM\ManyToMany(targetEntity="PTB\UserBundle\Entity\User")
 * @ORM\JoinTable(name="message_receivers",
 *      joinColumns={@ORM\JoinColumn(name="message_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id", unique=true)}
 *      )
 */
private $user;