Я настраиваю однонаправленное отношение "один ко многим" с таблицей соединений, как описано здесь: http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#one-to- многие однонаправленный-с-Join стола
У меня есть объект под названием TestEntity и объект, называемый testOptionalProperty. Когда я пытаюсь сохранить объект Entity, я получаю ошибку
Catchable Fatal Error: аргумент 1 передан в Doctrine\Common\Collections\ArrayCollection :: __ construct() должен быть из массива типов, заданного объекта, вызванного в /Users/dave/Desktop/doctrine-fun/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php в строке 555 и определено
Вот мои сущности:
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;
}
}
Метод контроллера:
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();
}
И мое картографирование доктрин:
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: { }
Является простым, ArrayCollection
__construct
получает только параметр Array
as, потому что вы пытаетесь сохранить отношения ManyToMany
, необходимые для инкапсуляции объекта в массив $array[] = $object;
в противном случае измените связь между объектами в OneToOne
.
Btw, попробуйте запустить app/console doctrine:generate:entities VendorAppBundle:TestEntity
для генерации геттеров и сеттеров на основе отношений