我有一个表格,我想在控制器中填写 1 个字段(fromID)(我曾经使用 setFromId 方法),但我收到错误。有我的代码:
public function newAction()
{
$entity = new PrivateMessage();
$user = $this->container->get('security.context')->getToken()->getUser();
$entity->setFromId($user);
$form = $this->createForm(new PrivateMessageType(), $entity);
$form->setData($entity);
return $this->render('AcmeStoreBundle:PrivateMessage:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
我错过了什么吗?@edit:我忘了补充说,当字段 fromID 在表单中可用时它正在工作。但我不想让用户改变它。
这是 PrivateMessage 实体的一部分:
class PrivateMessage
{/** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id;
/**
*
* @ORM\ManyToOne(targetEntity="User", inversedBy="pm_to")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="to_id", referencedColumnName="id", nullable=false)
* })
*/
private $toID;
/**
*
* @ORM\ManyToOne(targetEntity="User", inversedBy="pm_from")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="from_id", referencedColumnName="id", nullable=false)
* })
*/
private $fromID;
function __construct()
{
$this->setOpened(false);
$this->setRecipientDelete(false);
$this->setSenderDelete(false);
}
/**
* @ORM\PrePersist
*/
public function setFromIdValue()
{
$this->fromID= $this->getFromId();
}
/**
* Set fromID
*
* @param \Acme\Bundle\Entity\User $fromId
* @return PrivateMessage
*/
public function setFromId(\Acme\StoreBundle\Entity\User $fromId = null)
{
$this->fromID = $fromId;
return $this;
}
/**
* Get fromID
*
* @return \Acme\StoreBundle\Entity\User
*/
public function getFromId()
{
return $this->fromID;
}