我正在与联接表设置一对多单向关系,如下所述:http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#one-to-many-unidirectional-with-join-table
我有一个名为TestEntity的实体和一个名为testOptionalProperty的实体。当我尝试保留我的Entity对象时,我收到错误
捕获致命错误:参数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: { }
答案 0 :(得分:0)
很简单,ArrayCollection
__construct
只获取Array
作为参数,因为您尝试持久化ManyToMany
关系,您需要将对象封装在数组中{ {1}}否则会更改$array[] = $object;
中实体之间的关系。
顺便说一下,尝试运行OneToOne
来生成基于关系的getter和setter