0
votes

For the past couple of days I have been trying to create a bidirectionnal ManyToOne-OneToMany relationship in Symfony 3.4 I have two entities. One is Contribution and the other is Source. A Contribution can have several sources. So the relationship should be

Contribution – ManyToOne – Source – OneToMany – Contribution

But I keep getting the following error during $em→flush(); in my controller:

Type error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given, called in /var/www/html/Edebate/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 605

I do not have any set method related to the Array Collection in my Entity Contribution as I could see in other posts here:

Type error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given

Symfony-Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given

And the annotations are ok as mentionned here:

Doctrine OneToMany relationship error

Any help would be appreciate ! :)

Here is my Entity Contribution

use Doctrine\Common\Collections\ArrayCollection;

//annotations

  abstract class Contribution
    {

    /**
    * @ORM\OneToMany(targetEntity="Shaker\DebateBundle\Entity\Source", mappedBy="parent")
    */
    protected $sources;

//Other attributes and methods


    public function __construct() {
       $this->sources = new ArrayCollection();
    }


    /**
     * Add source
     *
     * @param \Shaker\DebateBundle\Entity\Source $source
     *
     * @return Contribution
     */
    public function addSource(\Shaker\DebateBundle\Entity\Source $source)
    {
        $this->sources[] = $source;
        return $this;
    }

    /**
     * Remove source
     *
     * @param \Shaker\DebateBundle\Entity\Source $source
     */
    public function removeSource(\Shaker\DebateBundle\Entity\Source $source)
    {
        $this->sources->removeElement($source);
    }

    /**
     * Get sources
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getSources()
    {
        return $this->sources;
    }
}

And this is in my Entity Source:

/**
* @ORM\ManyToOne(targetEntity="Shaker\DebateBundle\Entity\Contribution", inversedBy="sources")
*/
protected $parent;

   /**
     * Set parent
     *
     * @param \Shaker\DebateBundle\Entity\Contribution $parent
     *
     * @return Contribution
     */
    public function setParent(\Shaker\DebateBundle\Entity\Contribution $parent = null)
    {
        $this->parent = $parent;
        $parent->addSource($this);
        return $this;
    }

    /**
     * Get parent
     *
     * @return \Shaker\JRQBundle\Entity\Contribution
     */
    public function getParent()
    {
        return $this->parent;
    }

And in my Controller, the problem arises with flush:

        $formsourcebook->handleRequest($request);
        $contributionid=$formsourcebook->get('ContributionId')->getData();

        if ($formsourcebook->isValid()) {
            $topicargtarget=$this->getContribution($contributionid);
            $sourcebook->setUser($user);
            $sourcebook->setContribution($topicargtarget);
            $em->persist($sourcebook);
            $em->flush();
        }
2
Has anybody any idea what the error means ? I'm still stuck with this problem and it seems to be a very general error log.Shaker81

2 Answers

0
votes

I don't know your question very well. However, did you try with this syntax in the Source entity?

private $parent;
// ...

public function __construct() {
    $this->parent = new ArrayCollection();
    // or new \Doctrine\Common\Collections\ArrayCollection();
}

I think you're forgetting the constructor in the class.

0
votes

I think you "switched" some logic when working with collections. Here's how I think your "add" method should look like:

public function addSource(\Shaker\DebateBundle\Entity\Source $source)
{
    $this->sources[] = $source;
    $source->setParent($this);

    return $this;
}

And in the other entity:

public function setParent(\Shaker\DebateBundle\Entity\Contribution $parent = null)
{
    $this->parent = $parent;

    return $this;
}

There are missing variables in your controller snippet, together with the form fields definitions, so you shouldn't work that much after submitting the form. Try to directly map as many fields as you can (even via autoguessing), and even if it looks ugly, but works, but then you can beautify later. Just my two cents with several months of delay.