我有一个用于创建文档的表格。一方面,我可以添加名称和描述,在旁边我可以选择一个或多个机构创建的文档属于谁。
每个机构被分配一个specific市场(一共有7个市场,所以一个市场可以有多个代理机构,但一个代理机构只能属于一个市场!)
我想要实现的是“prePersist”功能,它会自动将正确的市场(取决于所选机构的数量)添加到文档中。
我的文档实体有两个实体(市场和机构)以及相应的 getter 和 setter:
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Market", inversedBy="uploadProfiles", cascade={"persist"})
* @ORM\JoinTable(name="document_uploadprofile_markets",
* joinColumns={@ORM\JoinColumn(name="uploadprofile_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="market_id", referencedColumnName="id")})
**/
private $markets;
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Agency", inversedBy="uploadProfiles", cascade={"persist"})
* @ORM\JoinTable(name="document_uploadprofile_agencies",
* joinColumns={@ORM\JoinColumn(name="uploadprofile_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="iata8", referencedColumnName="iata8")})
**/
private $agencies;
public function __construct()
{
$this->agencies = new \Doctrine\Common\Collections\ArrayCollection();
$this->markets = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add market
*
* @param \AppBundle\Entity\Market $market
*
* @return UploadProfile
*/
public function addMarket(\AppBundle\Entity\Market $market)
{
$this->markets[] = $market;
return $this;
}
/**
* Remove market
*
* @param \AppBundle\Entity\Market $market
*/
public function removeMarket(\AppBundle\Entity\Market $market)
{
$this->markets->removeElement($market);
}
/**
* Get markets
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getMarkets()
{
return $this->markets;
}
/**
* Add agency
*
* @param \AppBundle\Entity\Agency $agency
*
* @return UploadProfile
*/
public function addAgency(\AppBundle\Entity\Agency $agency)
{
$this->agencies[] = $agency;
return $this;
}
/**
* Remove agency
*
* @param \AppBundle\Entity\Agency $agency
*/
public function removeAgency(\AppBundle\Entity\Agency $agency)
{
$this->agencies->removeElement($agency);
}
/**
* Get agencies
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getAgencies()
{
return $this->agencies;
}
我知道我可以将 prePersist 函数添加到我的文档实体中,并尝试编写我想要实现的目标,但我认为这不起作用,因为我需要类似的东西:
foreach($document->getAgencies() as $agency) {
$document->setMarket($em->getRepository('AppBundle:Agency')->getMarket($agency));
}
我什至不确定 foreach 循环是否正确,因为(到目前为止)结果始终为空。我已经在这里向该主题提出了一个问题:Symfony 在 CreateController 中使用 Arraycollection 的 setter
我还尝试编写一个自己的存储库函数,以从我的代理实体获取所有不同的市场,但到目前为止这也不起作用。
另一个想法是我的表单类中的 POST_SUBMIT 事件侦听器,但到目前为止这对我来说也没有意义。
有任何想法吗?如果需要更多代码,请告诉我!
edit我编辑并更改了上面的代码,以便在我的市场和文档之间建立多种关系。然后我尝试的是,将 prePersist 函数添加到我的文档实体中,它实际上工作得很好,同时仍然具有 OneToMany 关系(它总是覆盖以前的市场,但现在并不重要)
我现在正在尝试编辑该函数,以便可以将多个市场添加到文档中。
我有两个想法,但都没有实现:
if(count($this->getAgencies()) > 0){
foreach($this->getAgencies() as $agency) {
$this->addMarket($agency->getMarket());
}
}
--> 市场永远是空的
or
if(count($this->getAgencies()) > 0){
$upId = rtrim($this->getId(),"_up");
$query = $em->createQuery("SELECT DISTINCT (a.market) FROM UserBundle\Entity\User u JOIN u.agencies a WHERE u.id = $userId");
$marketIds = $query->getResult();
$em = $this->getDoctrine ()->getManager ();
$repository = $this->getDoctrine()
->getRepository('AppBundle:Market');
$markets = $repository->findOneById($marketIds);
$this->addMarket($markets);
}
}
update
这是我的文档实体中的 prepersist 函数,然后是 getMarkets() 函数,这是在评论之一中建议的。我将名称更改为 addMarkets 而不是 getMarkets
/**
* @ORM\PrePersist
*/
public function prePersist() {
if(count($this->getAgencies()) > 0){
foreach($this->getAgencies() as $agency) {
$this->addMarkets($agency->getMarket());
}
}
}
public function addMarkets(\AppBundle\Entity\Market $market)
{
$markets = array();
foreach($this->agencies as $agency) {
$market = $agency->getMarket();
$id = $market->getId();
// Skip duplicates
if (isset($markets['id'])) {
continue;
}
$markets[$id] = $market;
}
return $markets;
}
另一种方法
所以我再次编辑它,现在我的函数看起来像这样
$markets = $this->getMarkets();
if(count($this->getAgencies()) > 0){
foreach($this->getAgencies() as $agency) {
if(!$this->markets->contains($markets)) {
$this->addMarket($agency->getMarket());
}
return;
dump($markets);
}
}
我认为这可能有助于消除我的重复项,但事实并非如此..知道为什么吗?