Reputation: 357
i'm using Symfony 2.7 and Doctrine in the latest version. I tried the many-to-one bidirectional example from their:
Now I can add Products with my Controller, but it doesn't update my features.
Here is the code for the product entity:
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/** @ORM\Entity
*
* @ORM\Table(name="product")
*
*/
class Product
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="Feature", mappedBy="product")
**/
private $features;
public function __construct() {
$this->features = new ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function addFeature(Feature $feature)
{
return $this->features[] = $feature;
}
public function getFeatures()
{
return $this->features;
}
}
and the code for feature entity:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/** @ORM\Entity
*
* @ORM\Table(name="feature")
*
*/
class Feature
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="features")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
**/
private $product;
public function getId()
{
return $this->id;
}
public function getProduct()
{
return $this->product;
}
public function setProduct($product)
{
$this->product = $product;
}
}
In my controller i just create a new product and a new feature and add the feature to my product-entity:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Product;
use AppBundle\Entity\Feature;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends Controller
{
/**
* @Route("/")
*/
public function indexAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$feature = new Feature();
$product = new Product();
$product->addFeature($feature);
$em->persist($feature);
$em->persist($product);
$em->flush();
return new Response('Created product id '.$product->getId());
}
}
I only get a new Product and a new feature. The feature doesn't get the relation to the product (the product_id field on database contains "NULL").
Upvotes: 0
Views: 2643
Reputation:
Try changing the addFeature
method of the Product
class into
public function addFeature(Feature $feature)
{
return $this->features->add($feature->setProduct($this));
}
and change the setProduct
method of the Feature
class into
public function setProduct($product)
{
$this->product = $product;
// For chaining
return $this;
}
If these changes do not solve your problem, try changing the $features
property annotation into
/**
* @ORM\OneToMany(targetEntity="Feature", mappedBy="product", cascade={"persist", "remove"})
**/
private $features;
The cascade
option tells Doctrine to automatically manage the sub-entity.
Upvotes: 1