我有一个表单来创建文档。一方面,我可以添加名称和说明,并在旁边,我可以选择一个或多个代理创建的文档所属。 每个机构被分配到一个特定的市场(总共有7个市场,所以一个市场可以有几个代理商,但一个代理商只属于一个市场!) 我想实现的是一个“prePersist”功能这会自动将正确的市场(取决于所选机构的数量)添加到文档中。Symfony将数据添加到预先保留的对象上
我的文档实体有两个实体(市场和机构)与根据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 use setter for Arraycollection in CreateController
我也试着写一个自己的仓库函数来从我的代理实体获取所有不同的市场,但到目前为止这也不起作用。
另一个想法是在我的表单类中的POST_SUBMIT事件监听器,但到目前为止,我也没有任何意义。
任何想法?如果需要更多代码,请告诉我!
编辑 我编辑,并以有我的市场和文件之间的多对多关系改变了我上面的代码。然后我尝试的是,prePersist函数添加到我的文档实体,它实际上工作正常,但仍然有OneToMany关系(它只是总是覆盖以前的市场,但现在并不重要) 我现在试图编辑该功能,以便可以将多个市场添加到文档中。 两个想法我有,但他们都没有发挥出来:
if(count($this->getAgencies()) > 0){
foreach($this->getAgencies() as $agency) {
$this->addMarket($agency->getMarket());
}
}
- >市场总是空
或
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);
}
}
更新
这里我的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);
}
}
我想,这可能努力消除我的重复,但它不..为什么?
嗯,这是我有什么? – sonja
你的意思是说你认为你的结构就像我的答案一样? – svgrafov
是的我在代理和市场之间有一对多和多对多的关系。上面显示的代码在我的文档中,所以它是文档和代理+文档和市场之间的关系 – sonja