从Symfony准则DB中获取数据

问题描述 投票:0回答:1

我刚刚开始我的第一个symfony项目,我有点困惑。

我想在数据库中查询没有商品的货架。我想出了这样的sql查询,似乎工作:

select distinct she.id shelf_id,rac.id rack_id , row.id row_id, war.id warehouse_id from shelfs she
left join products pro on she.id = pro.warehouse_id
left join racks rac on she.rack_id=rac.id
left join rows row on rac.row_id = row.id
left join warehouses war on row.warehouse_id = war.id
where she.id not in (select shelf_id from products);

但是我不知道如何将其转换为dql。

我试图在存储库中编写这样的功能:

    {

        $entityManager = $this->getEntityManager();


        $query = $entityManager->createQuery(
            'SELECT distinct she
            FROM App\Entity\Shelfs she
            JOIN App\Entity\Racks rac
            JOIN App\Entity\Rows row
            JOIN App\Entity\Warehouses war
            WHERE she NOT IN (SELECT prod FROM App\Entity\Products prod)
            ');
        return $query->getResult();

    }

这是部分工作的,起初它显示了sql查询的确切值。例如3货架ID,上面没有商品。但是,当我将产品添加到其中一个架子时,它仍然显示3是空的。

那是我的控制器:

public function index()
    {
        $repository = $this->getDoctrine()->getRepository(Products::class);
        $product = $repository->find(76); // that's for testing and it is refreshing

        $warehousesRepository = $this->getDoctrine()
            ->getRepository(Warehouses::class);

        $freespace = $warehousesRepository->findempty(); // that is not refreshing


        return $this->render('base.html.twig', [
            'selected_view' => 'pages/findPlace.html.twig',
            'product' => $product,
            'freespace' => $freespace

        ]);
    }

这是我的产品实体:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

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

    /**
     * @ORM\Column(type="integer")
     */
    private $barcode;

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

    /**
     * @ORM\Column(type="float")
     */
    private $price;

    /**
     * @ORM\Column(type="integer")
     */
    private $quantity;

    /**
     * @ORM\Column(type="integer")
     * @ORM\ManyToOne(targetEntity="Warehouses")
     * @ORM\JoinColumn(name="warehouse_id", referencedColumnName="id")
     */
    private $warehouse_id;

    /**
     * @ORM\Column(type="integer")
     * @ORM\ManyToOne(targetEntity="Rows")
     * @ORM\JoinColumn(name="row_id", referencedColumnName="id")
     */
    private $row_id;

    /**
     * @ORM\Column(type="integer")
     * @ORM\ManyToOne(targetEntity="Racks")
     * @ORM\JoinColumn(name="rack_id", referencedColumnName="id")
     */
    private $rack_id;

    /**
     * @ORM\Column(type="integer")
     * @ORM\ManyToOne(targetEntity="Shelfs")
     * @ORM\JoinColumn(name="shelf_id", referencedColumnName="id")
     */
    private $shelf_id;

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

    public function getBarcode(): ?int
    {
        return $this->barcode;
    }

    public function setBarcode(int $barcode): self
    {
        $this->barcode = $barcode;

        return $this;
    }

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

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

        return $this;
    }

    public function getPrice(): ?float
    {
        return $this->price;
    }

    public function setPrice(float $price): self
    {
        $this->price = $price;

        return $this;
    }

    public function getQuantity(): ?int
    {
        return $this->quantity;
    }

    public function setQuantity(int $quantity): self
    {
        $this->quantity = $quantity;

        return $this;
    }

    public function getWarehouseId(): ?int
    {
        return $this->warehouse_id;
    }

    public function setWarehouseId(int $warehouse_id): self
    {
        $this->warehouse_id = $warehouse_id;

        return $this;
    }

    public function getRowId(): ?int
    {
        return $this->row_id;
    }

    public function setRowId(int $row_id): self
    {
        $this->row_id = $row_id;

        return $this;
    }

    public function getRackId(): ?int
    {
        return $this->rack_id;
    }

    public function setRackId(int $rack_id): self
    {
        $this->rack_id = $rack_id;

        return $this;
    }

    public function getShelfId(): ?int
    {
        return $this->shelf_id;
    }

    public function setShelfId(int $shelf_id): self
    {
        $this->shelf_id = $shelf_id;

        return $this;
    }
}

我通过添加创建实体后尝试建立关系:

* @ORM\ManyToOne(targetEntity="Warehouses")
* @ORM\JoinColumn(name="warehouse_id", referencedColumnName="id")

但是那没有用,所以我想我需要重做实体并在向导中指定关系。

我已经使用sql建立了关系:

ALTER TABLE shelfs ADD FOREIGN KEY (rack_id) REFERENCES racks(id);
        ALTER TABLE racks ADD FOREIGN KEY (row_id) REFERENCES rows(id);
        ALTER TABLE rows ADD FOREIGN KEY (warehouse_id) REFERENCES warehouses(id);
        ALTER TABLE products ADD FOREIGN KEY (warehouse_id) REFERENCES warehouses(id);
        ALTER TABLE products ADD FOREIGN KEY (rack_id) REFERENCES racks(id);
        ALTER TABLE products ADD FOREIGN KEY (row_id) REFERENCES rows(id);
        ALTER TABLE products ADD FOREIGN KEY (shelf_id) REFERENCES shelfs(id);
        ALTER TABLE order_items ADD FOREIGN KEY (product_id) REFERENCES products(id);
        ALTER TABLE order_items ADD FOREIGN KEY (order_id) REFERENCES orders(id);
        ALTER TABLE orders ADD FOREIGN KEY (user_id) REFERENCES users(id);

它应该看起来像这样:

DB Diagram

我觉得我在某个地方的过程中迷路了。

我不知道发生了什么,也无法在symfony文档中找到任何相关的内容。

php sql symfony doctrine dql
1个回答
0
投票
    $sel = $this->createQueryBuilder('prod') // FROM App\Entity\Products prod
        ->select('prod')
        // ->from('App\Entity\Products', 'prod'); // FROM App\Entity\Products prod
    ;


    $res = $this->createQueryBuilder('she') // FROM App\Entity\Shelfs she
        ->select('DISTINCT she')
        // ->from('App\Entity\Shelfs', 'she')  // FROM App\Entity\Shelfs she
        ->leftJoin('she.rac', 'rac')
        ->leftJoin('she.row', 'row')
        ->leftJoin('she.war', 'war')
        ->andWhere('she NOT IN :(sel)')->setParameter('sel', $sel->getQuery()->getResult());
        ;

    return $res->getQuery()->getResult();
© www.soinside.com 2019 - 2024. All rights reserved.