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: { }