0
votes

I have a form and i would like to fill 1 field (fromID) in controller (i used to do it setFromId method), but I receives error. There is my code:

 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(),
    ));
}

do i missed something? @edit: i forgot to add that it's working when field fromID is avaliable in form. but i don't want to let user change it.

And this is part of PrivateMessage entity:

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;
}
1
can you paste the entity class? - Moylin
There is an error "An exception occurred while executing 'INSERT INTO PrivateMessage (time_sent, subject, message, opened, recipientDelete, senderDelete, to_id, from_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' with params ["2013-08-09 22:12:09", "gd", "hgdh", 0, 0, 0, 1, null]: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'from_id' cannot be null " - user2637282
The error occurs when the object is being persisted. You have not shown the code for that. The fact that you are using 'Id' in your property names suggest that you are not understanding the use of objects in Doctrine 2. When the form is posted you will need something to convert the id back into an object. Consider showing the posting code. - Cerad

1 Answers

0
votes

There is better place than controller to set (even override) your form fields - event listener. You should write listener for prePersist doctrine event and set/update field. Please look at example from docs: http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html#configuring-the-listener-subscriber