form symfony 2 many many self reference entity -


i create form collection of self reference entity.

i need form create new product ,this form have select field (child) existing products.

i have product entity , entity include child field (child product too).

product entity :

/**  * @var integer  *  * @orm\column(name="id", type="bigint", length=20)  * @orm\id  * @orm\generatedvalue(strategy="auto")  */ protected $id;  /**  * @var string  *  * @orm\column(name="title", type="string", length=255)  */ protected $title;  /**  * @var string  *  * @orm\column(name="manufacturer_reference", type="string", length=255, nullable=true)  */ protected $manufacturer_reference;  /**  * @var string  *  * @orm\column(name="resume", type="text", nullable=true)  */ protected $resume;  /**  * @var boolean  *  * @orm\column(name="is_salable", type="boolean", options={"default" = 1})  */ protected $is_salable = 1;  /**  * @var boolean  *  * @orm\column(name="is_active", type="boolean", options={"default" = 1})  */ protected $is_active = 1;  /**  * @orm\manytoone(targetentity="\hexanet\common\catalogbundle\entity\productcategory")  * @orm\joincolumn(name="product_category_id", referencedcolumnname="id", nullable=true)  */ protected $product_category;  /**  * @orm\manytoone(targetentity="\hexanet\common\catalogbundle\entity\manufacturer")  * @orm\joincolumn(name="manufacturer_id", referencedcolumnname="id", nullable=true)  */ protected $manufacturer;  /**  * @orm\manytomany(targetentity="\hexanet\common\catalogbundle\entity\product", mappedby="parents" )  */ protected $children;  /**  * @orm\manytomany(targetentity="\hexanet\common\catalogbundle\entity\product")  * @orm\jointable(name="product_to_product",  *     joincolumns={@orm\joincolumn(name="child_product_id", referencedcolumnname="id")},  *     inversejoincolumns={@orm\joincolumn(name="parent_product_id", referencedcolumnname="id")}  * )  */ protected $parents;  /**  * @orm\onetomany(targetentity="\hexanet\common\catalogbundle\entity\productprice", mappedby="product" )  */ protected $product_prices;  /**  * @orm\onetomany(targetentity="\hexanet\common\catalogbundle\entity\productpricepurchase", mappedby="product")  */ protected $product_prices_purchase;  /**  * @orm\onetomany(targetentity="\hexanet\common\catalogbundle\entity\productpricecustom", mappedby="product")  */ protected $product_prices_custom;  /**  * id  *  * @return integer  */ public function getid() {     return $this->id; }  /**  * set title  *  * @param string $title  * @return product  */ public function settitle($title) {     $this->title = $title;      return $this; }  /**  * title  *  * @return string  */ public function gettitle() {     return $this->title; }  /**  * set product category  *  * @param \hexanet\common\catalogbundle\entity\productcategory $product_category  * @return product  */ public function setproductcategory(\hexanet\common\catalogbundle\entity\productcategory $product_category = null) {     $this->product_category = $product_category;      return $this; }  /**  * product category  *  * @return \hexanet\common\catalogbundle\entity\productcategory  */ public function getproductcategory() {     return $this->product_category; }  /**  * set resume  *  * @param string $resume  * @return product  */ public function setresume($resume) {     $this->resume = $resume;      return $this; }  /**  * resume  *  * @return string  */ public function getresume() {     return $this->resume; }  /**  * set manufacturer reference  *  * @param string $title  * @return product  */ public function setmanufacturerreference($ref) {     $this->manufacturer_reference = $ref;      return $this; }  /**  * manufacturer reference  *  * @return string  */ public function getmanufacturerreference() {     return $this->manufacturer_reference; }  /**  * set salable  *  * @param boolean $active  * @return product  */ public function setissalable($salable) {     $this->is_salable = $salable;      return $this; }  /**  * salable  *  * @return boolean  */ public function getissalable() {     return $this->is_salable; }  /**  * set active  *  * @param boolean $active  * @return product  */ public function setisactive($active) {     $this->is_active = $active;      return $this; }  /**  * active  *  * @return boolean  */ public function getisactive() {     return $this->is_active; }  /**  * set manufacturer  *  * @param $manufacturer  * @return product  */ public function setmanufacturer($manufacturer) {     $this->manufacturer = $manufacturer;      return $this; }  /**  * manufacturer  *  */ public function getmanufacturer() {     return $this->manufacturer; }  /**  * constructor  */ public function __construct() {     $this->parents = new \doctrine\common\collections\arraycollection();     $this->children = new \doctrine\common\collections\arraycollection();     $this->product_prices = new \doctrine\common\collections\arraycollection();     $this->product_prices_purchase = new \doctrine\common\collections\arraycollection();     $this->product_prices_custom = new \doctrine\common\collections\arraycollection(); }  /**  * add child  *  * @param \hexanet\common\catalogbundle\entity\product $product  * @return product  */ public function addchild(\hexanet\common\catalogbundle\entity\product $product) {     die(var_dump($product));     $this->children[] = $product;      return $this; }  /**  * remove child  *  * @param \hexanet\common\catalogbundle\entity\product $product  */ public function removechild(\hexanet\common\catalogbundle\entity\product $product) {     $this->children->removeelement($product); }  /**  * children  *  * @return \doctrine\common\collections\collection  */ public function getchildren() {     return $this->children; }  /**  * add parent  *  * @param \hexanet\common\catalogbundle\entity\product $product  * @return product  */ public function addparent(\hexanet\common\catalogbundle\entity\product $product) {     $this->parents[] = $product;      return $this; }  /**  * remove parent  *  * @param \hexanet\common\catalogbundle\entity\product $price  */ public function removeparent(\hexanet\common\catalogbundle\entity\product $product) {     $this->parents->removeelement($product); }  /**  * parents  *  * @return \doctrine\common\collections\collection  */ public function getparents() {     return $this->parents; }  /**  * add product price  *  * @param \hexanet\common\catalogbundle\entity\productprice $price  * @return product  */ public function addproductprice(\hexanet\common\catalogbundle\entity\productprice $price) {     $this->product_prices[] = $price;      return $this; }  /**  * remove product price  *  * @param \hexanet\common\catalogbundle\entity\productprice $price  */ public function removeproductprice(\hexanet\common\catalogbundle\entity\productprice $price) {     $this->product_prices->removeelement($price); }  /**  * product prices  *  * @return \doctrine\common\collections\collection  */ public function getproductprices() {     return $this->product_prices; }  /**  * add product price purchase  *  * @param \hexanet\common\catalogbundle\entity\productpricepurchase $price  * @return product  */ public function addproductpricepurchase(\hexanet\common\catalogbundle\entity\productpricepurchase $price) {     $this->product_prices_purchase[] = $price;      return $this; }  /**  * remove product price purchase  *  * @param \hexanet\common\catalogbundle\entity\productpricepurchase $price  */ public function removeproductpricepurchase(\hexanet\common\catalogbundle\entity\productpricepurchase $price) {     $this->product_prices_purchase->removeelement($price); }  /**  * product prices purchase  *  * @return \doctrine\common\collections\collection  */ public function getproductpricespurchase() {     return $this->product_prices_purchase; }  /**  * add product price custom  *  * @param \hexanet\common\catalogbundle\entity\productpricecustom $price  * @return product  */ public function addproductpricecustom(\hexanet\common\catalogbundle\entity\productpricecustom $price) {     $this->product_prices_custom[] = $price;      return $this; }  /**  * remove product price custom  *  * @param \hexanet\common\catalogbundle\entity\productpricecustom $price  */ public function removeproductpricecustom(\hexanet\common\catalogbundle\entity\productpricecustom $price) {     $this->product_prices_custom->removeelement($price); }  /**  * product prices custom  *  * @return \doctrine\common\collections\collection  */ public function getproductpricescustom() {     return $this->product_prices_custom; }} 

for form have :

class producttype extends abstracttype{  public function buildform(formbuilderinterface $builder, array $options) {     $builder         ->add('title')         ->add('manufacturer_reference')         ->add('resume')         ->add('product_category', 'entity', array(         'class' => 'hexanetcatalogbundle:productcategory',         'property' => 'title',         ))         ->add('children', 'collection', array(         'type' => new productchildrentype,         'allow_add' => true)); }  public function setdefaultoptions(optionsresolverinterface $resolver) {     $resolver->setdefaults(array(         'data_class' => 'hexanet\common\catalogbundle\entity\product'     )); }  public function getname() {     return 'hexanet_common_catalogbundle_producttype'; }} 

the problem there, dont know how create productchildrentype builder :

class productchildrentype extends abstracttype{ public function buildform(formbuilderinterface $builder, array $options) {     $builder         ->add('product', 'entity', array(         'class' => 'hexanetcatalogbundle:product',         'property' => 'title',         )); }  public function setdefaultoptions(optionsresolverinterface $resolver) {     $resolver->setdefaults(array(         'data_class' => 'hexanet\common\catalogbundle\entity\product'     )); }  public function getname() {     return 'hexanet_common_catalogbundle_productchildrentype'; }} 

->add('product', 'entity',...) have error :

neither property "product" nor method "getproduct()" nor method "isproduct()" exists in class "hexanet\common\catalogbundle\entity\product".

thx help

i have similar case store, can add products on admin area, can offer them on checkout... partner @ work , me solved problem yesterday, if you're still interested, here go.... using symfony 2.6.x , haven't tested on older versions of symfony yet.

->add('myextras', 'collection', array(             'type' => 'entity',             'options' => array(                 'class'       => 'storebundle:productos',                 'placeholder' => '-- select product --',                 'property' => 'name',                 'query_builder' => function (entityrepository $er) use( $options ) {                     return $er->createquerybuilder('p')                         ->where('p.asociable = :asociable')                         ->andwhere('p.id != :selfid')                         ->setparameters( array('adjuntable' => '1', 'selfid' => $options['selfid'] ));                 },                 'label' => 'extra product'             ),             'by_reference' => false,             'allow_add'    => true,             'allow_delete' => true         )) 

instead of using collection of form type "children", used collection of type "entity", , used querybuilder control conditions needed right options show.

using stopped having messages, you're getting... , saving , removing relation, when add children, had tell children set parent... , removing same, first tell children remove parent parent's list, , remove children children list... (in code easier see)

in entity have collections myextras (childrens) , imextraof (parents), when adding children, have tell children i'm receiving counterpart ->addimextraof (i father function) ... add product our list. , removing, same, call first ->removeimextraof , if don't way, relation not saved.

the entity :

public function addmyextra(productos $extra) {     $extra->addimextraof($this);     if( !$this->myextras->contains($extra) ) {         $this->myextras[] = $extra;     }       return $this; }  public function removemyextra(productos $extra) {     $extra->removeimextraof($this);     $this->myextras->removeelement($extra); } 

the orm mapping (yml): (myextras = children, imextraof = parents )

manytomany:             myextras:         targetentity: productos         cascade: [ persist ]         mappedby: imextraof         inversedby: null           jointable:             name: productos_has_productos             joincolumns:                 -                     name: extra_id                     referencedcolumnname: id             inversejoincolumns:                 -                     name: base_id                     referencedcolumnname: id         orderby: null     imextraof:         targetentity: productos         cascade: [ persist ]         mappedby: null         inversedby: myextras           jointable:             name: productos_has_productos             joincolumns:                 -                     name: base_id                     referencedcolumnname: id             inversejoincolumns:                 -                     name: extra_id                     referencedcolumnname: id         orderby: null 

hope helps someone.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -