symfony - Many to Many Relationship Setter -


i have many many relationship between entities: purchaseorder , supplier. when want add supplier order in symfony project, error message:

property "suppliers" not public in class "acme\appbundle\entity\purchaseorder". maybe should create method "setsuppliers()"?

when make setsuppliers() function myself in purchaseorder entity:

public function setsuppliers(\acme\appbundle\entity\supplier $suppliers ) {     $this->suppliers = $suppliers;      return $this; } 

i error message:

catchable fatal error: argument 1 passed doctrine\common\collections\arraycollection::__construct() must of type array, object given, called in /var/www/symfony/vendor/doctrine/orm/lib/doctrine/orm/unitofwork.php on line 519 , defined in /var/www/symfony/vendor/doctrine/collections/lib/doctrine/common/collections/arraycollection.php line 47

any ideas?

/**  * @route("order/{id}/supplieradd", name="order_supplieradd")  * @secure(roles="role_admin")  */ public function newsupplieraction(request $request, $id) {     $purchaseorder = $this->getdoctrine()     ->getrepository('acmeappbundle:purchaseorder')     ->find($id);      if (!$purchaseorder) {         throw $this->createnotfoundexception(                 'no order found id '.$id         );     }      $form = $this->createform(new addsuppliertype(), $purchaseorder);      // process form on post     if ($request->ismethod('post')) {         $form->bind($request);         if ($form->isvalid()) {                           $em = $this->getdoctrine()->getmanager();              $em->persist($purchaseorder);             $em->flush();              return new response('added supplier order id '.$articleorder->getid());         }     }      return $this->render('acmeappbundle:basicdata:newsupplier.html.twig', array(             'form' => $form->createview(),             'id' => $id,     )); } 

and addsuppliertype.php

public function buildform(formbuilderinterface $builder, array $options) {     $builder->add('suppliers', 'entity', array(          'class' => 'acmeappbundle:supplier',          'property' => 'name',     )); } 

some parts of purchaseorder , supplier entities:

 class purchaseorder{   ...          /**          * @orm\manytomany(targetentity="supplier", mappedby="purchaseorders")               */     private $suppliers;      public function __construct()     {         $this->suppliers = new arraycollection();     }  /**  * add suppliers  *  * @param \acme\appbundle\entity\supplier $suppliers  * @return purchaseorder  */ public function addsupplier(\acme\appbundle\entity\supplier $suppliers) {     $this->suppliers[] = $suppliers;      return $this; }  /**  * remove suppliers  *  * @param \acme\appbundle\entity\supplier $suppliers  */ public function removesupplier(\acme\appbundle\entity\supplier $suppliers) {     $this->suppliers->removeelement($suppliers); }  /**  * suppliers  *  * @return \doctrine\common\collections\collection   */ public function getsuppliers() {     return $this->suppliers; } }  class supplier{     ...      /**      * @orm\manytomany(targetentity="purchaseorder", inversedby="suppliers")      * @orm\jointable(name="suppliers_purchaseorders")      */     private $purchaseorders; } 

new add remove set methods:

 /**      * add supplier      *      * @param \acme\appbundle\entity\supplier $supplier      * @return purchaseorder      */ public function addsupplier(\acme\appbundle\entity\supplier $supplier) {     $this->suppliers->add($supplier);      return $this; }  /**  * remove supplier  *  * @param \acme\appbundle\entity\supplier $supplier  */ public function removesupplier(\acme\appbundle\entity\supplier $supplier) {     $this->suppliers->removeelement($supplier); }  public function setsuppliers($supplier) {      if ( is_array($supplier) ) {         $this->suppliers = $supplier ;     } else {         $this->suppliers->clear() ;         $this->suppliers->add($supplier) ;     } } 

problems:

  1. grammatically incorrect method name , argument:

    public function addsupplier(\acme\appbundle\entity\supplier $suppliers) 

    method says addsupplier (singular) accepting suppliers (plural)

  2. you need refactoring on method be:

    public function addsupplier(supplier $supplier) {     $this->suppliers->add($supplier) ; } 

    also:

    public function removesupplier(supplier $supplier) {     $this->suppliers->removeelement($supplier) ; } 

    getter , setter method work if answer on: set multiple='false' in form in many many relation symfony2

symfony find add...() , remove...() methods itself. if relation "suppliers", find addsupplier. or if relation "categories", find addcategory() , removecategory().


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -