0
votes

I'm setting up a One-to-Many unidirectional relationship with a join table, as described here: http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#one-to-many-unidirectional-with-join-table

I have an entity called TestEntity and an entity called testOptionalProperty. When I try to persist my Entity object, I get the error

Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given, called in /Users/dave/Desktop/doctrine-fun/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 555 and defined

Here are my entities:

TestEntity.php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * TestEntity
 */
class TestEntity
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $name;

    private $testOptionalProperty;

    /**
     * @return mixed
     */
    public function getTestOptionalProperty()
    {
        return $this->testOptionalProperty;
    }

    /**
     * @param mixed $testOptionalProperty
     */
    public function setTestOptionalProperty($testOptionalProperty)
    {
        $this->testOptionalProperty = $testOptionalProperty;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return TestEntity
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }
}

TestOptionalProperty.php

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * TestOptionalProperty
 */
class TestOptionalProperty
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var integer
     */
    private $value;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set value
     *
     * @param integer $value
     * @return TestOptionalProperty
     */
    public function setValue($value)
    {
        $this->value = $value;

        return $this;
    }

    /**
     * Get value
     *
     * @return integer 
     */
    public function getValue()
    {
        return $this->value;
    }
}

Controller Method:

public function testAction()
{
    $entity = new TestEntity();
    $entity->setName('Testing');
    $entity->setTestOptionalProperty(new TestOptionalProperty());
    $entity->getTestOptionalProperty()->setValue(100);
    $em = $this->getDoctrine()->getManager();
    $em->persist($entity);
    $em->flush();
}

And my doctrine mapping:

AppBundle\Entity\TestEntity:
    type: entity
    table: null
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: 255
    lifecycleCallbacks: {  }
    manyToMany:
            testOptionalProperty:
                targetEntity: AppBundle\Entity\TestOptionalProperty
                cascade: ["persist"]
                joinTable:
                    name: entity_property
                    joinColumns:
                        entity_id:
                            referencedColumnName: id
                            unique: true
                    inverseJoinColumns:
                        property_id:
                            referencedColumnName: id


AppBundle\Entity\TestOptionalProperty:
    type: entity
    table: null
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        value:
            type: integer
    lifecycleCallbacks: {  }
1
Your mappings are seriously messed up. Consult the docs: doctrine-orm.readthedocs.org/en/latest/reference/…. OneToMany relations will not have join tables. - Cerad
In the link I provided at the top of my question, I link to the doctrine documentation explaining the exact mapping that I am trying to use. It is a one to many unidirectional with a join table - David Legatt
OneToMany relations do not have join tables. Your orm file shows a ManyToMany relation. If you really want a ManyToMany relation with additional relations on the join table then make an entity specifically for the join table and break the ManyToMany into two ManyToOne relations. - Cerad
Please read the link I provided at the top of my question. I am setting my entities up exactly like the doctorine documentation describes. - David Legatt
So why isn't testOptionalProperty an array Like the documentation shows and what the error message indicates? - Cerad

1 Answers

0
votes

Is simple, the ArrayCollection __construct get only an Array as parameter, because you try to persist an ManyToMany relationship you need to encapsulate the object in an array $array[] = $object; otherwise change the relationship between the entities in a OneToOne.

Btw, try to run app/console doctrine:generate:entities VendorAppBundle:TestEntity for generating relation-based getters and setters