Doctrine Self-referencing Pesquisa | Fórum | School of Net

Deseja poder participar de nosso fórum e tirar todas as suas dúvidas?
Clique aqui e assine nosso plano de acesso ilimitado. Saiba mais.

por Elvis

3 meses, 1 semana atrás Elvis

Doctrine Self-referencing Pesquisa

Eu preciso fazer uma pequisa que seja tanto no parent quando no children por exemplo pesquisa por Moveis > Quarda Roupas ``` $params = $request->getQueryParams(); $qb = $this->em->createQueryBuilder(); $query = $qb->select('c') ->from(\App\Domain\Category::class, 'c') ->setMaxResults((int)$params['limit']) ->setFirstResult((int)$params['offset'])->orderBy('c.name'); if (isset($params['name'])) { $arr = explode(' ', urldecode($params['name'])); foreach ($arr as $key => $value) { $query->andWhere("c.name LIKE :name_{$key}"); $query->setParameter("name_{$key}", "%{$value}%"); } } ``` ``` <?php /** * Created by PhpStorm. * User: Elvis * Date: 13/03/2019 * Time: 09:36 */ namespace App\Domain; use Doctrine\ORM\Mapping as ORM; use JMS\Serializer\Annotation as JMS; use \Doctrine\Common\Collections\ArrayCollection; /** * @ORM\Entity(repositoryClass="CategoryRepository") * @ORM\Table(name="categories", indexes={@ORM\Index(columns={"name"}, flags={"fulltext"})}) */ class Category { /** * @ORM\Id * @ORM\Column(type="integer", name="id", nullable=false, unique=true) * @ORM\GeneratedValue(strategy="AUTO") * @JMS\Type("integer") * @JMS\Groups ({"list", "details"}) */ protected $id; /** * @ORM\Column(type="string", name="name", nullable=false) * @JMS\Type("string") * @JMS\Groups ({"list", "details"}) */ protected $name; /** * @ORM\OneToMany(targetEntity="Category", mappedBy="parent") */ private $children; /** * @ORM\ManyToOne(targetEntity="Category", inversedBy="children", cascade={"persist", "remove"}) * @ORM\JoinColumn(name="parent_id", referencedColumnName="id") * @JMS\Type("App\Domain\Category") * @JMS\Groups ({"details"}) */ private $parent; public function __construct() { $this->children = new ArrayCollection(); } /** * @return mixed */ public function getId() { return $this->id; } /** * @param mixed $id * @return Category */ public function setId($id) { $this->id = $id; return $this; } /** * @return mixed */ public function getName() { return $this->name; } /** * @param mixed $name * @return Category */ public function setName($name) { $this->name = $name; return $this; } /** * @return mixed */ public function getChildren() { return $this->children; } /** * @param mixed $children * @return Category */ public function setChildren($children) { $this->children = $children; return $this; } /** * @JMS\PreSerialize * @JMS\Groups({"list"}) */ public function getParent() { $parent = $this->parent; /* if ($parent) { $this->name = $this->parent->getName() . ' > ' . $this->name; } */ return $parent; } /** * @param mixed $parent * @return Category */ public function setParent($parent) { $this->parent = $parent; return $this; } } ```

6 Respostas