当表单具有实体类型时,Symfony3 多对多关系
Symfony3 many-to-many realtion when form has entitytype
所以我正在为我的网站构建消息系统,我有 Message 实体,它与 Inbox[= 具有多对多关系30=],它应该存储 message_id 和 receiver_id。这是因为用户应该能够向 许多 用户
发送消息
我已经完成了所有关系,但是,当提交表单时我得到错误:
Expected value of type "Doctrine\Common\Collections\Collection|array" for association field "DEERCMS\MessageBundle\Entity\Message#$receiver", got "PTB\UserBundle\Entity\User" instead.
这是表格,它有 '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;
}
}
我自己解决了。所以基本上目标实体是错误的,应该在消息和用户之间定义关系,这是它的样子
/**
* @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;
所以我正在为我的网站构建消息系统,我有 Message 实体,它与 Inbox[= 具有多对多关系30=],它应该存储 message_id 和 receiver_id。这是因为用户应该能够向 许多 用户
发送消息我已经完成了所有关系,但是,当提交表单时我得到错误:
Expected value of type "Doctrine\Common\Collections\Collection|array" for association field "DEERCMS\MessageBundle\Entity\Message#$receiver", got "PTB\UserBundle\Entity\User" instead.
这是表格,它有 '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;
}
}
我自己解决了。所以基本上目标实体是错误的,应该在消息和用户之间定义关系,这是它的样子
/**
* @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;