我打赌这有一个非常简单的解决方案,但我无法理解。如果您需要任何额外信息,请指出。
我有3张桌子:
User
Job - ManyToMany User
JobReport - ManyToMany Job , ManyToMany User
用户和作业都存在于db中。所以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();
一切似乎都有效。以下是实体:
UserEntity:
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;
}
}
JobEntity:
<?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;
}
}
JobReportEntity:
<?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;
}
}
一切似乎都很正常?我不确定它唯一有效的是工作标记。作业和作业标签有一个连接表。 ManyToMany用@joinTable制作。
这是:
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中定义
我该如何调试?
并且会给予很多帮助:)
非常感谢,
Ioan-Catalin B。
答案 0 :(得分:0)
如果我没弄错的话,@ ManyToMany注释只能用于@JoinTable。
/**
* Territory
*
* @ORM\Table()
*/
class Territory
{
/**
* @ORM\ManyToMany(targetEntity="House", inversedBy="territories")
* @ORM\JoinTable(name="territory_house",
* joinColumns={@ORM\JoinColumn(name="territory_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="house_id", referencedColumnName="id")}
* )
*/
private $houses;
}
/**
* House
*
* @ORM\Table(name="house")
*/
class House
{
/**
* @ORM\ManyToMany(targetEntity="Territory", mappedBy="houses")
*/
private $territories;
}