1

In my ZF2 application, I have 2 Doctrine2 entities : Product and Partner

Because in some cases a product doesn't need a partner, there is a nullable ManyToOne relation between Product and Partner.

It works if a partner is provided in the form (dropdown). But if not, I have this error :

An exception occurred while executing 'SELECT t0.id AS id1, t0.title AS title2 FROM partners t0 WHERE t0.id = ?' with params [""]:

SQLSTATE[22P02]: Invalid text representation: 7 ERROR:  invalid input syntax for 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 as $product)
        {
            $this->addProduct($product);
        }

        return $this;
    }

    /**
     * @param null $products
     *
     * @return $this
     */
    public function removeProducts($products = null)
    {
        if (!$products)
        {
            $products = $this->getProducts();
        }

        foreach ($products as $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 help would be much appreciated.

4
  • Did you add ToNull filter to your form? Because form will send empty character and you should change that with null. Commented Jun 28, 2015 at 13:56
  • @hkulekci, no, I didn't add it Commented Jun 29, 2015 at 8:14
  • I dont understand? Do you want add or not? Do you want to get reaction? :) Commented Jun 29, 2015 at 10:47
  • 1
    Can you add this filter, i guess it solves your problem. Commented Jun 29, 2015 at 10:47

2 Answers 2

1

Thanks to @hkulekci for the answer.

I just updated my input filter from that :

public function getInputFilterSpecification()
{
    return [
        'partner' => ['required' => true],
        /* ... */
    ];
}

To that :

public function getInputFilterSpecification()
{
    return [
        'partner' => [
            'required' => true,
            'continue_if_empty' => true,
            'filters' => [
                ['name' => 'Null']
            ]
        ],
        /* ... */
    ];
}
Sign up to request clarification or add additional context in comments.

Comments

0

You will need to allow a null value to be passed to the setPartner method

public function setPartner(Partner $partner = null)
{
    $this->partner = $partner;

    return $this;
}

Also, depending on your form hydrator, you will need to make sure that if no partner is selected the empty value should be converted to null.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.