Symfony在预持久化时将数据添加到对象 [英] Symfony add data to object on pre persist
问题描述
我有一个创建文件的表格。一方面,我可以添加名称和描述,然后可以选择创建的文档所属的一个或多个代理商。
每个代理都分配给一个特定市场(共有7个市场,因此一个市场可以有多个代理,但一个代理仅属于一个市场!)
我想要实现的是一个 prePersist功能,该功能会自动将正确的市场(取决于所选代理商的数量)添加到文档中。
I have a form to create documents. On the one side I can add names and descriptions and next to that I can select one or several agencies to whom the created document belongs. Each of the agencies is assigned to one specific market (there are 7 markets in total, so one market can have several agencies but one agency belongs only to one market!) What I want to achieve is a "prePersist" function which automatically adds the correct market(s) (depending on the number of agencies selected) to the document.
我的文档实体有两个实体(市场和代理商),分别具有相应的获取器和设置器:
My document entity has the two entities (markets and agencies) with the according getters and setters:
/**
* @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函数添加到文档实体中并尝试编写我想要实现的代码,但我认为这行不通,因为我需要这样的东西:
I know that I can add the prePersist function to my document entity and try to code what I want to achieve but I don't think that's working because I need something like that:
foreach($document->getAgencies() as $agency) {
$document->setMarket($em->getRepository('AppBundle:Agency')->getMarket($agency));
}
我什至不确定该foreach循环是否正确,因为(到目前为止)结果始终为null。我已经在这里针对该主题提出了一个问题: Symfony在CreateController中使用Arraycollection的setter方法
I'm not even sure whether that foreach loop is correct since (so far) the result is always null. I've already asked a question to that topic here: Symfony use setter for Arraycollection in CreateController
我还尝试编写自己的存储库功能以从我的代理商实体获取所有不同的市场,但到目前为止那也不起作用。
I've also tried to write an own repository function to get all the distinct markets from my agency entity but so far that's not working either.
另一个想法是我的表单类中的POST_SUBMIT事件侦听器,但到目前为止对我也没有意义。
Another idea was a POST_SUBMIT event listener in my form class but so far that didn't make sense to me either.
有什么想法吗?如果需要更多代码,请告诉我!
Any ideas? If more code is needed, let me know!
编辑
我在上面编辑和更改了我的代码,以便在我的市场和文档之间建立许多联系。然后我尝试的是,将prePersist函数添加到我的文档实体中,并且在仍然保持OneToMany关系的同时,它实际上工作正常(它总是覆盖以前的市场,但是现在不重要了)
现在尝试编辑该功能,以便可以将多个市场添加到文档中。
我有两个想法,但都没有解决:
edit I edited and changed my code above in order to have a manytomany relationship between my markets and documents. What I then tried is, adding the prePersist function to my document entity and it actually worked fine while still having the OneToMany relationship (it was just always overwriting the previous market, but that doesn't matter now) I'm now trying to edit that function so that several markets can be added to the document. Two ideas I had, but they both didn't work out:
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
here is my prepersist function within my document entity and then the getMarkets() function, that has been suggested in one of the comments. I changed the name to addMarkets instead of 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;
}
另一种方法
所以我再次对其进行了编辑,现在我的函数看起来像这样
so I edited it again, now my function looks like that
$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);
}
}
我认为这样做可能会消除重复项,但是
I thought that this might work to eliminate my duplicates but it does not.. any idea why?
推荐答案
如果我正确理解了你说的话,我相信文档
根本不应引用市场
。但是只有引用 Agency
。
If I understand what you said correctly, I believe Documents
should not have a reference to Markets
at all. But only references Agency
.
A Document
与 Agency
的 ManyToMany
关系就是这样。
A Document
will have a ManyToMany
relation with Agency
and that's it.
然后在 Document
中,您可以执行以下操作:
Then in Document
you can do something like:
public function getMarkets()
{
$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;
}
这篇关于Symfony在预持久化时将数据添加到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!