I have the One-to-Many bidirectional relationship below.
After generating the crud actions with a symfony2 task, when I try to save the Products associated to a Category in the new/edit Category form, the products are not saved...
namespace PruebaFrontendBundleEntity;
use GedmoMappingAnnotation as Gedmo;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* @ORMEntity
* @ORMTable(name="category")
*/
class Category
{
/**
* @var integer $id
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORMOneToMany(targetEntity="Product", mappedBy="category")
*/
protected $products;
/**
* @ORMColumn(name="name")
*/
protected $name;
public function __construct()
{
$this->products = new ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
public function __toString()
{
return $this->name;
}
public function getProducts()
{
return $this->products;
}
public function setProducts($products)
{
die("fasdf"); //here is not entering
$this->products[] = $products;
}
public function addProduct($product)
{
die("rwerwe"); //here is not entering
$this->products[] = $product;
}
}
namespace PruebaFrontendBundleEntity;
use GedmoMappingAnnotation as Gedmo;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* @ORMEntity
* @ORMTable(name="product")
*/
class Product
{
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMManyToOne(targetEntity="Category", inversedBy="products")
* @ORMJoinColumn(name="category_id", referencedColumnName="id")
*/
protected $category;
/**
* @ORMColumn(type="string", length=100)
*/
protected $name;
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
public function getCategory()
{
return $this->category;
}
public function setCategory($category)
{
$this->category = $category;
}
public function __toString()
{
return $this->name;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…