我敢打赌,有一个非常简单的解决方案,但我就是想不通。如果您需要任何额外的信息,请指出。
我有 3 张桌子:
User
Job - ManyToMany User
JobReport - ManyToMany Job , ManyToMany User
用户和作业都存在于数据库中。所以php调用是:
$report = new JobReportEntity();
//get user
$userWhoReports = $this->getEntityManager()->getRepository("Entity\User")->find(1);
//get job
$queryBuilder = $this->em->createQueryBuilder()
->select(array('p', 'u'))
->from('Entity\Job', 'p')
->leftJoin('p.user', 'u')
->where('p.id = :id')
->setParameter('id', 1);
$job = $queryBuilder->getQuery()->getResult();
$job = $job[0];
$report->setUser($userWhoReports);
$report->setJob($job);
$report->setReportDate(new \Datetime('now'));
$report->setReason("reason");
$this->getEntityManager()->persist($report);
$this->getEntityManager()->flush();
一切似乎都奏效了。以下是实体:
用户实体:
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* @Entity
* @Table(name="user", uniqueConstraints={
* @UniqueConstraint(name="unique_name",columns={"username"}),
* })
*/
class User
{
/**
* @Id
* @Column(type="integer");
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = (int) $id;
}
public function getUsername()
{
return $this->username;
}
public function setUsername($username)
{
$this->username = $username;
}
}
工作实体:
<?php
namespace Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Job
*
* @Entity
* @Table(name="job")
*/
class Job
{
/**
* @Id
* @Column(type="integer");
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
//..
/**
* @Column(type="integer")
*/
protected $user_id;
/**
* @ManyToOne(targetEntity="User")
* @JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
/**
* @ManyToMany(targetEntity="JobTag")
* @JoinTable(name="job_tags",
* joinColumns={@JoinColumn(name="job_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="job_tag_id", referencedColumnName="id")}
* )
*/
protected $tags;
public function __construct() {
$this->tags = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getTags()
{
return $this->tags;
}
public function addTag(JobTag $tag)
{
//$tag->addJob($this);
$this->tags[] = $tag;
}
public function removeTag(JobTag $tag)
{
foreach($this->tags as $k => $t)
{
if ($t->getId() == $tag->getId())
{
//$t->removeJob($this);
unset($this->tags[$k]);
return;
}
}
}
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = (int) $id;
}
public function getUser()
{
return $this->user;
}
public function setUser($user)
{
$this->user = $user;
}
}
工作报告实体:
<?php
namespace Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Job
*
* @Entity
* @Table(name="job_report", uniqueConstraints={
* @UniqueConstraint(name="multiple_reports_from_the_same_user",columns={"job_id", "user_id"}),
* })
*/
class JobReport
{
/**
* @Id
* @Column(type="integer");
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Column(type="integer")
*/
protected $user_id;
/**
* @ManyToMany(targetEntity="User")
* @JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
/**
* @Column(type="integer")
*/
protected $job_id;
/**
* @ManyToMany(targetEntity="Job")
* @JoinColumn(name="job_id", referencedColumnName="id")
*/
protected $job;
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = (int) $id;
}
public function getUser()
{
return $this->user;
}
public function setUser($user)
{
$this->user = $user;
}
public function getJob()
{
return $this->job;
}
public function setJob($job)
{
$this->job = $job;
}
}
一切看起来都很正常?我唯一不确定它是否有效的是工作标签。工作和工作标签有一个连接表。使用 @joinTable 制作的 ManyToMany。
这里是:
use Doctrine\ORM\Mapping as ORM;
/**
* Job
*
* @Entity
* @Table(name="job_tag", uniqueConstraints={
* @UniqueConstraint(name="name",columns={"name"}),
* })
*/
class JobTag
{
/**
* @Id
* @Column(type="integer");
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
//..
/**
* @Column(type="integer")
*/
protected $user_id;
/**
* @ManyToOne(targetEntity="User")
* @JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = (int) $id;
}
public function getUser()
{
return $this->user;
}
public function setUser($user)
{
$this->user = $user;
}
}
这是问题所在:
可捕获的致命错误:传递给 Doctrine\Common\Collections\ArrayCollection::__construct() 的参数 1 必须是数组类型,给定对象,在 /vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php 中调用547 并在第 47 行的 /vendor/doctrine/collections/lib/Doctrine/Common/Collections/ArrayCollection.php 中定义
我将如何调试这个?
并且会非常有帮助:)
非常感谢,
艾恩-卡塔林 B.