在持久化和刷新具有关联的对象时,我收到以下错误消息:
Catchable Fatal Error: Argument 1 passed to
Doctrine\Common\Collections\ArrayCollection::__construct() must be of
the type array, object given, called in
.../vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 519 and defined in
.../vendor/doctrine/common/lib/Doctrine/Common/Collections/ArrayCollection.php line 48
我所拥有的是一个以此为基础对象的单表继承:
use Doctrine\ORM\Mapping as ORM;
/**
* ObjectData
*
* @ORM\Table(name="object_data")
* @ORM\Entity(repositoryClass="Edexp\CoreBundle\Entity\ObjectDataRepository")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="entity_name", type="string")
* @ORM\DiscriminatorMap({
* "request" = "Edexp\MessageBundle\Entity\RequestData"
* })
* @ORM\HasLifecycleCallbacks
*/
class ObjectData
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var \DateTime
*
* @ORM\Column(name="created_at", type="datetime")
*/
private $created_at;
/**
* @var User
*
* @ORM\OneToMany(targetEntity="User", mappedBy="data")
*/
private $user;
/**
* @var string
*
* @ORM\Column(name="`key`", type="string", length=255)
*/
private $key;
/**
* @var string
*
* @ORM\Column(name="data", type="blob")
*/
private $data;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set created_at
*
* @param \DateTime $createdAt
* @return ObjectData
*/
public function setCreatedAt($createdAt)
{
$this->created_at = $createdAt;
return $this;
}
/**
* Get created_at
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->created_at;
}
/**
* Set user
*
* @param User $user
* @return ObjectData
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return User
*/
public function getUser()
{
return $this->user;
}
/**
* Set key
*
* @param string $key
* @return ObjectData
*/
public function setKey($key)
{
$this->key = $key;
return $this;
}
/**
* Get key
*
* @return string
*/
public function getKey()
{
return $this->key;
}
/**
* Set data
*
* @param string $data
* @return ObjectData
*/
public function setData($data)
{
$this->data = $data;
return $this;
}
/**
* Get data
*
* @return string
*/
public function getData()
{
return $this->data;
}
/**
* @ORM\PrePersist
*/
public function prepareForPersist()
{
$this->created_at = new \DateTime();
}
}
以及一个从 ObjectData 继承的附加对象:
use Doctrine\ORM\Mapping as ORM;
use MyProject\CoreBundle\Entity\ObjectData;
/**
* RequestData
*
* @ORM\Entity()
*/
class RequestData extends ObjectData
{
/**
* var Request $request
*
* @ORM\ManyToOne(targetEntity="Request", inversedBy="data")
* @ORM\JoinColumn(name="object_id", referencedColumnName="id")
*/
private $request;
/**
* Set request
*
* @param Request $request
* @return RequestData
*/
public function setRequest($request)
{
$this->request = $request;
return $this;
}
/**
* Get request
*
* @return Request
*/
public function getRequest()
{
return $this->request;
}
}
这是利用 RequestData 实体的对象:
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Request
*
* @ORM\Table(name="requests")
* @ORM\Entity(repositoryClass="RequestRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Request
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var Message $message
*
* @ORM\ManyToOne(targetEntity="Message", inversedBy="requests")
*/
private $message;
/**
* @var RequestType $type
*
* @ORM\ManyToOne(targetEntity="RequestType")
* @ORM\JoinColumn(name="type_id", referencedColumnName="id")
*/
private $type;
/**
* @var ArrayCollection $data
*
* @ORM\OneToMany(
* targetEntity="RequestData",
* mappedBy="request",
* cascade={"persist","remove"}
* )
*/
private $data;
/**
* @var datetime $created_at
*
* @ORM\Column(name="created_at", type="datetime")
*/
private $created_at;
/**
* @var text $comment
*
* @ORM\Column(name="comment", type="text")
*/
private $comment;
/**
* @var ArrayCollection $responses
*
* @ORM\OneToMany(
* targetEntity="Response",
* mappedBy="request",
* cascade={"persist","remove"}
* )
*/
private $responses;
public function __construct()
{
$this->data = new ArrayCollection();
$this->responses = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set message
*
* @param Message $message
*/
public function setMessage($message)
{
$this->message = $message;
}
/**
* Get message
*
* @return Message
*/
public function getMessage()
{
return $this->message;
}
/**
* Set type
*
* @param RequestType $type
*/
public function setType($type)
{
$this->type = $type;
}
/**
* Get type
*
* @return RequestType
*/
public function getType()
{
return $this->type;
}
/**
* Set created_at
*
* @param datetime $createdAt
*/
public function setCreatedAt($createdAt)
{
$this->created_at = $createdAt;
}
/**
* Get created_at
*
* @return datetime
*/
public function getCreatedAt()
{
return $this->created_at;
}
/**
* Set comment
*
* @param text $comment
*/
public function setComment($comment)
{
$this->comment = $comment;
}
/**
* Get comment
*
* @return text
*/
public function getComment()
{
return $this->comment;
}
/**
* Add response
*
* @param Response $response
*/
public function addResponse($response)
{
foreach ( $this->responses as $r ) {
if ( $r->getUser() == $response->getUser() )
return;
}
$this->responses->add($response);
}
/**
* Get responses
*
* @return ArrayCollection
*/
public function getResponses()
{
return $this->responses;
}
public function hasResponse($user)
{
foreach ( $this->responses as $r ) {
if ( $r->getUser() == $user )
return $r;
}
return null;
}
public function addData($data)
{
$data->setRequest($this);
$this->data->add($data);
}
public function getData()
{
return $this->data;
}
/**
* @ORM\PrePersist
*/
public function prePersist()
{
$this->created_at = new \DateTime();
}
}
最后是导致错误的代码:
use MyProject\MessageBundle\Entity\Request as MyRequest;
$data = new RequestData();
$data->setUser($user);
$data->setKey('user_id');
$data->setData(6);
$req = new MyRequest();
$req->setMessage($message);
$req->setComment('bla');
$req->setType($doctrine->getRepository('MessageBundle:RequestType')->find(1));
$req->addData($data);
$em->persist($req);
$em->flush();
关于这里可能有什么问题的任何建议?
干杯