Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.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
Sql 条令自定义实体存储库(QueryBuilder)_Sql_Symfony_Doctrine Orm_Query Builder_Api Platform.com - Fatal编程技术网

Sql 条令自定义实体存储库(QueryBuilder)

Sql 条令自定义实体存储库(QueryBuilder),sql,symfony,doctrine-orm,query-builder,api-platform.com,Sql,Symfony,Doctrine Orm,Query Builder,Api Platform.com,如何为房间实体创建自定义存储库QueryBuilder,witch将返回两个日期之间的所有可用房间。我在RoomRepository.php中有两个实体Room.php和Reservation.php我创建了函数findAvailableRooms($from,$to)我是否创建了查询生成器,返回在所选期间没有预订的所有房间?TnxRoom.php <?php namespace App\Entity; use ApiPlatform\Core\Annotation\ApiResour

如何为房间实体创建自定义存储库QueryBuilder,witch将返回两个日期之间的所有可用房间。我在RoomRepository.php中有两个实体Room.php和Reservation.php

我创建了
函数findAvailableRooms($from,$to)
我是否创建了查询生成器,返回在所选期间没有预订的所有房间?Tnx

Room.php

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\AvailableRoom;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Bridge\Doctrine\IdGenerator\UuidV4Generator;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Uid\Uuid;
use App\Repository\RoomRepository;
use Doctrine\ORM\Mapping as ORM;
use App\Controller\AvailableRoomController;

/**
 * @ApiResource(
 *     normalizationContext={"groups"={"room:read"}},
 *     denormalizationContext={"groups"={"room:write"}},
 *     attributes={
 *         "pagination_client_items_per_page"=true
 *     },
 *     collectionOperations={
 *         "get",
 *         "post",
 *         "get_available"={
 *             "method"="GET",
 *             "path"="/rooms/available/{from}/{to}",
 *             "controller"=AvailableRoomController::class
 *         }
 *     }
 * )
 * @ORM\Entity(repositoryClass=RoomRepository::class)
 */
class Room
{
    /**
     * @ORM\Id
     * @ORM\Column(type="uuid", unique=true)
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class=UuidV4Generator::class)
     * @Groups({"room:read"})
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"room:read", "room:write", "reservation:read"})
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=255)
     * @Gedmo\Slug(fields={"name"})
     * @Groups({"room:read", "room:write"})
     */
    private $slug;

    /**
     * @ORM\Column(type="text", nullable=true)
     * @Groups({"room:read", "room:write"})
     */
    private $description;

    /**
     * @ORM\Column(type="datetime")
     * @Gedmo\Timestampable(on="create")
     * @Groups({"room:read", "room:write"})
     */
    private $createdAt;

    /**
     * @ORM\Column(type="datetime")
     * @Gedmo\Timestampable(on="update")
     * @Groups({"room:read", "room:write"})
     */
    private $updatedAt;

    /**
     * @ORM\OneToMany(targetEntity=Reservation::class, mappedBy="room")
     * @Groups({"room:read", "room:write"})
     */
    private $reservations;

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

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

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

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

        return $this;
    }

    public function getSlug(): ?string
    {
        return $this->slug;
    }

    public function setSlug(string $slug): self
    {
        $this->slug = $slug;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(?string $description): self
    {
        $this->description = $description;

        return $this;
    }

    public function getCreatedAt(): ?\DateTimeInterface
    {
        return $this->createdAt;
    }

    public function setCreatedAt(?\DateTimeInterface $createdAt): self
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    public function getUpdatedAt(): ?\DateTimeInterface
    {
        return $this->updatedAt;
    }

    public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    /**
     * @return Collection|Reservation[]
     */
    public function getReservations(): Collection
    {
        return $this->reservations;
    }

    public function addReservation(Reservation $reservation): self
    {
        if (!$this->reservations->contains($reservation)) {
            $this->reservations[] = $reservation;
            $reservation->setRoom($this);
        }

        return $this;
    }

    public function removeReservation(Reservation $reservation): self
    {
        if ($this->reservations->removeElement($reservation)) {
            // set the owning side to null (unless already changed)
            if ($reservation->getRoom() === $this) {
                $reservation->setRoom(null);
            }
        }

        return $this;
    }
}

放置在
ReservationRepository
中的类似以下方法将获取与指定日期范围重叠的
房间
的ID:

  /**
   * @param \DateTimeInterface $start
   * @param \DateTimeInterface $end
   *
   * @return array
   */
  public function findRoomIdsReservedInDateRange(\DateTimeInterface $start, \DateTimeInterface $end)
  {
    $result = $this
      ->createQueryBuilder('r')
      ->select('r.room.id')
      ->andWhere('r.dateFrom < :end')
      ->andWhere('r.dateTo > :start')
      ->setParameter('start', $start)
      ->setParameter('end', $end)
      ->orderBy('r.room.id', 'ASC')
      ->getQuery()
      ->getScalarResult()
    ; // [['id' => 1], ['id' => 2], ['id' => 2], ['id' => 5]]

    return array_unique(array_column($result, 'id')); // [1, 2, 5]
  }
  /**
   * @param \DateTimeInterface $start
   * @param \DateTimeInterface $end
   *
   * @return array
   */
  public function findRoomIdsReservedInDateRange(\DateTimeInterface $start, \DateTimeInterface $end)
  {
    $result = $this
      ->createQueryBuilder('r')
      ->select('r.room.id')
      ->andWhere('r.dateFrom < :end')
      ->andWhere('r.dateTo > :start')
      ->setParameter('start', $start)
      ->setParameter('end', $end)
      ->orderBy('r.room.id', 'ASC')
      ->getQuery()
      ->getScalarResult()
    ; // [['id' => 1], ['id' => 2], ['id' => 2], ['id' => 5]]

    return array_unique(array_column($result, 'id')); // [1, 2, 5]
  }
$exclude = $reservationRepository->findRoomIdsReservedInDateRange($start, $end);

return $this
  ->createQueryBuilder('room')
  ->andWhere('room.id not in :array')
  ->setParameter('array', $exclude)
  ->getQuery()
  ->getResult()
  ;