Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony多个重复条目_Php_Many To Many_Symfony4 - Fatal编程技术网

Php Symfony多个重复条目

Php Symfony多个重复条目,php,many-to-many,symfony4,Php,Many To Many,Symfony4,当我想插入关系为ManyToMany的数据时,我遇到了一个问题 当我想用CategoryType创建一个新类别时,我只需要名称的字段。 在我的AdminCategoryController中,我想通过获取我工作的域服务器来强制站点 更新 namespace App\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\OR

当我想插入关系为ManyToMany的数据时,我遇到了一个问题

当我想用CategoryType创建一个新类别时,我只需要名称的字段。 在我的AdminCategoryController中,我想通过获取我工作的域服务器来强制站点

更新
namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
 */
class Category
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Site", inversedBy="categories", cascade={"persist"})
     */
    private $site;

    public function __toString(){
        return $this->name;
    }

    public function __construct(Site $site)
    {
        $this->site = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return Collection|Site[]
     */
    public function getSite(): Collection
    {
        return $this->site;
    }

    public function addSite(Site $site): self
    {
        if (!$this->site->contains($site)) {
            $this->site[] = $site;
        }

        return $this;
    }

    public function removeSite(Site $site): self
    {
        if ($this->site->contains($site)) {
            $this->site->removeElement($site);
        }

        return $this;
    }
}
事实上,我有很多网站和许多领域。所有站点必须只连接一个数据库,并使用其服务器名。 因此,我的VHOST已为此进行了配置

所以,我有两个实体:站点,类别

站点实体

<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\SiteRepository")
 */
class Site
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $domain;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Category", mappedBy="site")
     */
    private $categories;

    public function __construct()
    {
        $this->categories = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getDomain(): ?string
    {
        return $this->domain;
    }

    public function setDomain(string $domain): self
    {
        $this->domain = $domain;

        return $this;
    }

    /**
     * @return Collection|Category[]
     */
    public function getCategories(): Collection
    {
        return $this->categories;
    }

    public function addCategory(Category $category): self
    {
        if (!$this->categories->contains($category)) {
            $category->addSite($this);
            $this->categories[] = $category;
        }

        return $this;
    }

    public function removeCategory(Category $category): self
    {
        if ($this->categories->contains($category)) {
            $this->categories->removeElement($category);
            $category->removeSite($this);
        }

        return $this;
    }
}
namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
 */
class Category
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Site", inversedBy="categories", cascade={"persist"})
     */
    private $site;

    public function __toString(){
        return $this->name;
    }

    public function __construct(Site $site)
    {
        $this->site = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return Collection|Site[]
     */
    public function getSite(): Collection
    {
        return $this->site;
    }

    public function addSite(Site $site): self
    {
        if (!$this->site->contains($site)) {
            $this->site[] = $site;
        }

        return $this;
    }

    public function removeSite(Site $site): self
    {
        if ($this->site->contains($site)) {
            $this->site->removeElement($site);
        }

        return $this;
    }
}
我通过会话在SiteService中获取站点信息:
namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
 */
class Category
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Site", inversedBy="categories", cascade={"persist"})
     */
    private $site;

    public function __toString(){
        return $this->name;
    }

    public function __construct(Site $site)
    {
        $this->site = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return Collection|Site[]
     */
    public function getSite(): Collection
    {
        return $this->site;
    }

    public function addSite(Site $site): self
    {
        if (!$this->site->contains($site)) {
            $this->site[] = $site;
        }

        return $this;
    }

    public function removeSite(Site $site): self
    {
        if ($this->site->contains($site)) {
            $this->site->removeElement($site);
        }

        return $this;
    }
}
use App\Repository\SiteRepository;

use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

class SiteService{

    public $infos;

    private $session;
    private $requestStack;

    private $siteRepository;

    public function __construct(SessionInterface $session, RequestStack $requestStack, SiteRepository $siteRepository){
        $this->session = $session;
        $this->requestStack = $requestStack;
        $this->siteRepository = $siteRepository;

        $this->getInfos();
    }

    public function getInfos(){
        $this->infos = $this->session->get('site');
        if(empty($this->infos)){
            $request = $this->requestStack->getCurrentRequest();
            $server_name = $request->server->get('SERVER_NAME');
            $this->infos = $this->siteRepository->findOneBy(['domain'=>$server_name]);
            if($this->infos == NULL){
                $this->infos = $this->siteRepository->findOneBy([]);
            }
            $this->session->set('site', $this->infos);
            $this->infos = $this->session->get('site');
        }
        return $this->infos;
    }
}
这是我的类别控制器,用于在站点中添加新类别

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
 */
class Category
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Site", inversedBy="categories", cascade={"persist"})
     */
    private $site;

    public function __toString(){
        return $this->name;
    }

    public function __construct(Site $site)
    {
        $this->site = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return Collection|Site[]
     */
    public function getSite(): Collection
    {
        return $this->site;
    }

    public function addSite(Site $site): self
    {
        if (!$this->site->contains($site)) {
            $this->site[] = $site;
        }

        return $this;
    }

    public function removeSite(Site $site): self
    {
        if ($this->site->contains($site)) {
            $this->site->removeElement($site);
        }

        return $this;
    }
}
 /**
 * @Route("/new", name="admin.category.new")
 * @param  Request $request
 * @return Response
 */
public function new(Request $request):Response{
    $category = new Category();

    $form = $this->createForm(CategoryType::class, $category);
    $form->handleRequest($request);

    $server_name = $request->server->get('SERVER_NAME');
    $site = $this->siteRepository->findBy(['domain'=>$server_name]);

    $siteFromSession = $this->session->get('site');

    if($form->isSubmitted() && $form->isValid()){
        $category->addSite($site);
        $this->em->persist($category);
        $this->em->flush();

        return $this->redirectToRoute('admin.category.edit', ['id'=>$category->getId()]);
    }

    $form = $form->createView();
    $params = compact('category', 'form');
    return $this->render('admin/category/new.html.twig', $params);
}
两个变量转储不相等

垃圾场($site)

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
 */
class Category
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Site", inversedBy="categories", cascade={"persist"})
     */
    private $site;

    public function __toString(){
        return $this->name;
    }

    public function __construct(Site $site)
    {
        $this->site = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return Collection|Site[]
     */
    public function getSite(): Collection
    {
        return $this->site;
    }

    public function addSite(Site $site): self
    {
        if (!$this->site->contains($site)) {
            $this->site[] = $site;
        }

        return $this;
    }

    public function removeSite(Site $site): self
    {
        if ($this->site->contains($site)) {
            $this->site->removeElement($site);
        }

        return $this;
    }
}
Site {#187 ▼
  -id: 1
  -name: "My Website"
Site {#623 ▼
  -id: 1
  -name: "My Website"
转储($siteFromSession)

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
 */
class Category
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Site", inversedBy="categories", cascade={"persist"})
     */
    private $site;

    public function __toString(){
        return $this->name;
    }

    public function __construct(Site $site)
    {
        $this->site = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return Collection|Site[]
     */
    public function getSite(): Collection
    {
        return $this->site;
    }

    public function addSite(Site $site): self
    {
        if (!$this->site->contains($site)) {
            $this->site[] = $site;
        }

        return $this;
    }

    public function removeSite(Site $site): self
    {
        if ($this->site->contains($site)) {
            $this->site->removeElement($site);
        }

        return $this;
    }
}
Site {#187 ▼
  -id: 1
  -name: "My Website"
Site {#623 ▼
  -id: 1
  -name: "My Website"
如果我使用$Category->addSite($site)持久化Category; 这是工作

但当我使用$Category->addSite($siteFromSession)持久化Category时; 我在数据库中看到当前站点在站点表和类别中是重复的。站点是新站点,而不是我插入的站点

但是,我不希望每次添加类别时都执行查询。 我想使用会话来获取网站的信息只有第一次,当我在管理页面登录

谢谢你帮助我,所以!!我已经决定了! 当我想在会话中保存站点信息时,我将其分离,当我想从会话中获取实体时,我将其合并

public function setInfos(){
    $request = $this->requestStack->getCurrentRequest();
    $server_name = $request->server->get('HTTP_HOST');
    $site = $this->siteRepository->findOneBy(['domain'=>$server_name]);
    $this->em->detach($site);
    $this->session->set('site', $site);
}

public function getInfos()
    $site = $this->session->get('site');
    $site = $this->em->merge($site);
}
所以!!我已经决定了! 当我想在会话中保存站点信息时,我将其分离,当我想从会话中获取实体时,我将其合并

public function setInfos(){
    $request = $this->requestStack->getCurrentRequest();
    $server_name = $request->server->get('HTTP_HOST');
    $site = $this->siteRepository->findOneBy(['domain'=>$server_name]);
    $this->em->detach($site);
    $this->session->set('site', $site);
}

public function getInfos()
    $site = $this->session->get('site');
    $site = $this->em->merge($site);
}

为什么在添加类别时不愿意执行db查询?在某些时候,您必须告诉Dority使用现有站点,否则它看起来就像一个新站点。变量
SERVER\u NAME
返回主域,如apache
ServerName
指令中所设置的。您没有提供有关如何设置
VirtualHost
的任何信息,但我想说使用
HTTP\u HOST
可以解决您的问题。对于VHost和ServerName,我已经更新了我的帖子。有了站点信息,我可以切换到我所有的网站,在管理员页面中。因此,我无法想象每次都会在管理员中请求知道我要在哪个站点添加/修改项目。你知道为什么返回的对象不一样吗?站点#187和站点#623但有了好的主键(1),我意识到,在其他论坛中,真正的问题是来自会话的站点信息,他们谈到了从会话中合并目标站点。但我总是犯错误:通过未配置为级联的关系“App\entity\CategorySite\site”找到了一个新实体。但是如果我添加cascade=persist,这个重复的入口站点将id:3,名称:“我的网站”如此!!我已经决定了!当我想在会话中保存站点信息时,我将其分离,当我想从会话中获取实体时,我将其合并为什么在添加类别时不愿意执行db查询?在某些时候,您必须告诉Dority使用现有站点,否则它看起来就像一个新站点。变量
SERVER\u NAME
返回主域,如apache
ServerName
指令中所设置的。您没有提供有关如何设置
VirtualHost
的任何信息,但我想说使用
HTTP\u HOST
可以解决您的问题。对于VHost和ServerName,我已经更新了我的帖子。有了站点信息,我可以切换到我所有的网站,在管理员页面中。因此,我无法想象每次都会在管理员中请求知道我要在哪个站点添加/修改项目。你知道为什么返回的对象不一样吗?站点#187和站点#623但有了好的主键(1),我意识到,在其他论坛中,真正的问题是来自会话的站点信息,他们谈到了从会话中合并目标站点。但我总是犯错误:通过未配置为级联的关系“App\entity\CategorySite\site”找到了一个新实体。但是如果我添加cascade=persist,这个重复的入口站点将id:3,名称:“我的网站”如此!!我已经决定了!当我想在会话中保存站点信息时,我将其分离,当我想从会话中获取实体时,我将其合并