如何通过原则2创建子属性表(基本上像联接表,但另一侧没有实体)。我是否需要为此创建一个单独的实体(我知道如果我对一个联接表进行ManyToMany操作,它只会在数据库中创建联接表,而不会创建一个实体)。
即-我的工单可以有很多分配的工作日期。我希望能够用这些日期填写表格,
______________________
| Orders |
|____________________|
id
1
2
3
______________________
| Order_Dates |
|____________________|
order_id | Date
1 | 2019-01-01
1 | 2019-05-01
2 | 2019-01-01
2 | 2019-02-01
2 | 2019-03-01
3 | 2019-01-02
3 | 2019-01-05
到目前为止,基本上我已经拥有
class Orders
{
public function __construct()
{
$this->dates = new ArrayCollection();
}
/**
* @ORM\Id()
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
/**
* One Order has Many Dates
*/
private $dates;
public function getDates() : ArrayCollection
{
return $this->dates;
}
public function addDate(\DateTime $date): self
{
$this->dates->add($date);
return $this;
}
public function setDates(ArrayCollection $dates): self
{
$this->dates = $dates;
return $this;
}
}
我假设我需要一个@OneToMany之类的东西,但是我期望子表是FILLED FROM Order Entity,而不是映射。我在doctrine association documentation中看到的所有内容似乎都希望有某种映射(也许我读错了吗?)
谢谢!
现在是我的控制者:
订单
/**
* @ORM\OneToMany(targetEntity="OrderDates", mappedBy="order", cascade={"persist", "remove"}, orphanRemoval=TRUE)
*/
private $dates;
...
public function getDates() : PersistentCollection
{
return $this->dates;
}
public function addDate(\DateTime $date): self
{
$orderDate = new OrderDates();
$orderDate->setDate($date);
$orderDate->setOrder($this);
$this->dates[] = $orderDate;
return $this;
}
public function setDates(array $dates): self
{
foreach($dates as $date)
{
$this->addDate(new \DateTime($date));
}
return $this;
}
订购日期
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\OrderDatesRepository")
*/
class OrderDates
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="datetime")
*/
private $date;
/**
* @ORM\ManyToOne(targetEntity="Orders", inversedBy="dates")
*/
private $order;
public function getId(): ?int
{
return $this->id;
}
public function getDate(): ?\DateTimeInterface
{
return $this->date;
}
public function setDate(\DateTimeInterface $date): self
{
$this->date = $date;
return $this;
}
public function getOrder(): Orders
{
return $this->order;
}
public function setOrder(Orders $order): self
{
$this->order = $order;
return $this;
}
}
答案 0 :(得分:0)
您必须为Order_Dates创建一个实体,如果要为订单日期创建一个额外的数据库表,请使用OneToMany批注。 Doctrine不支持由Doctrine处理的实体与另一个驱动程序处理的实体之间的关联。主义甚至不支持由不同实体管理器处理的实体之间的关联。
总结起来:您必须创建一个OrderDates实体并以传统方式设置关联。
编辑:您应该这样设置关联:
class Orders {
// id and other fields here...
/**
* @ORM\OneToMany(targetEntity="OrderDates", mappedBy="order", cascade={"persist", "remove"}, orphanRemoval=TRUE)
*/
private $dates;
public function addDate(\DateTime $date): self
{
$orderDate = new OrderDates();
$orderDate->setDate($date);
$orderDate->setOrder($this);
$this->dates[] = $orderDate;
return $this;
}
}
class OrderDates {
// id here...
/**
* @ORM\ManyToOne(targetEntity="Order", inversedBy="dates")
*/
private $order;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="datetime")
*/
private $date;
// setters and getters here
}