Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following works perfectly for me in Symfony 2.3 I got it from Reading the docs and going the knpuniversities episode 3 on Symfony2 I hope it helps someone. </p> <p>The Entity<br> <pre><code>use Doctrine\ORM\Mapping as ORM; use Acme\UserBundle\Entity\User; /** * @ORM\Entity * @ORM\Table(name="products") */ class Products { /** * @ORM\Column(type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(type="string", length=100) */ protected $name; /** * @ORM\Column(type="decimal", scale=2) */ protected $price; /** * @ORM\Column(type="text") */ protected $description; /** * Get id * * @return integer */ public function getId() { return $this-&gt;id; } /** * Set name * * @param string $name * @return Products */ public function setName($name) { $this-&gt;name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this-&gt;name; } /** * Set price * * @param float $price * @return Products */ public function setPrice($price) { $this-&gt;price = $price; return $this; } /** * Get price * * @return float */ public function getPrice() { return $this-&gt;price; } /** * Set description * * @param string $description * @return Products */ public function setDescription($description) { $this-&gt;description = $description; return $this; } /** * Get description * * @return string */ public function getDescription() { return $this-&gt;description; } /** * @ORM\ManyToOne(targetEntity="Acme\UserBundle\Entity\User", cascade={"remove"}) * @ORM\JoinColumn(onDelete="CASCADE") */ private $owner; public function getOwner() { return $this-&gt;owner; } public function setOwner(User $owner) { $this-&gt;owner = $owner; } } </code></pre> <p>and in the Controller</p> <pre><code> /** * Creates a new Products entity. * */ public function createAction(Request $request) { $entity = new Products(); $form = $this-&gt;createCreateForm($entity); $form-&gt;handleRequest($request); if ($form-&gt;isValid()) { $em = $this-&gt;getDoctrine()-&gt;getManager(); // Associate User Entity To Product $user = $this-&gt;container-&gt;get('security.context')-&gt;getToken()-&gt;getUser(); $entity-&gt;setOwner($user); $em-&gt;persist($entity); $em-&gt;flush(); return $this-&gt;redirect($this-&gt;generateUrl('products_show', array('id' =&gt; $entity-&gt;getId()))); } return $this-&gt;render('AcmeMainBundle:Products:new.html.twig', array( 'entity' =&gt; $entity, 'form' =&gt; $form-&gt;createView(), )); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload