php - Doctrine2 / PostgreSQL - Selection error with a nullable relation -
in zf2 application, have 2 doctrine2 entities : product
, partner
because in cases product doesn't need partner, there nullable manytoone relation between product , partner.
it works if partner provided in form (dropdown). if not, have error :
an exception occurred while executing 'select t0.id id1, t0.title title2 partners t0 t0.id = ?' params [""]: sqlstate[22p02]: invalid text representation: 7 error: invalid input syntax integer: ""`
the entities :
/** * @orm\entity * @orm\table(name="products") */ class product extends abstractentity { /* ... */ /** * @orm\manytoone(targetentity="partner", inversedby="products", cascade={"persist", "merge"}) * @orm\joincolumn(name="partner_id", referencedcolumnname="id", nullable=true) */ protected $partner; /* ... */ /** * @return partner */ public function getpartner() { return $this->partner; } /** * @param partner $partner * * @return $this */ public function setpartner(partner $partner) { $this->partner = $partner; return $this; } /* ... */ } /** * @orm\entity * @orm\table(name="partners") */ class partner extends abstractentity { /* ... */ /** * @orm\onetomany(targetentity="product", mappedby="partner", cascade={"persist", "merge", "remove"}) **/ protected $products; /* ... */ public function __construct() { $this->products = new arraycollection(); } /* ... */ /** * @param product $product * * @return $this */ public function addproduct(product $product) { $this->products[] = $product; $product->setpartner($this); return $this; } /** * @param $products * * @return $this */ public function addproducts($products) { foreach ($products $product) { $this->addproduct($product); } return $this; } /** * @param null $products * * @return $this */ public function removeproducts($products = null) { if (!$products) { $products = $this->getproducts(); } foreach ($products $product) { if (is_object($product)) { $this->products->removeelement($product); } else $this->products->remove($product); } return $this; } /** * @return mixed */ public function getproducts() { return $this->products; } /** * @param mixed $products * * @return $this */ public function setproducts($products) { $this->products = $products; return $this; } /* ... */ }
any appreciated.
thanks @hkulekci answer.
i updated input filter :
public function getinputfilterspecification() { return [ 'partner' => ['required' => true], /* ... */ ]; }
to :
public function getinputfilterspecification() { return [ 'partner' => [ 'required' => true, 'continue_if_empty' => true, 'filters' => [ ['name' => 'null'] ] ], /* ... */ ]; }
Comments
Post a Comment