简体   繁体   English

DQL与JOIN Doctrine2和Symfony2

[英]DQL with JOIN Doctrine2 and Symfony2

Hello I have two tables: products and stocks. 您好我有两张桌子:产品和库存。 They have manyToOne relation and look this way: 他们有很多关系,看起来像这样:

Stock: 股票:

+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+ 
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| stock_name | varchar(255) | NO   |     | NULL    |                | 
| address    | varchar(255) | NO   |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

Products: 产品介绍:

+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| stock_id | int(11)      | YES  | MUL | NULL    |                |
| name     | varchar(255) | NO   |     | NULL    |                |
| delivery | varchar(255) | NO   |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+

Entities 实体

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

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

    /**
     * @ORM\Column(type="string")
     */
    private $delivery;

    /**
     * @ORM\ManyToOne(targetEntity="Stock", inversedBy="products")
     * @ORM\JoinColumn(nullable=true)
     */
    private $stock;

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param mixed $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @return mixed
     */
    public function getDelivery()
    {
        return $this->delivery;
    }

    /**
     * @param mixed $delivery
     */
    public function setDelivery($delivery)
    {
        $this->delivery = $delivery;
    }

    /**
     * @return mixed
     */
    public function getStock()
    {
        return $this->stock;
    }

    /**
     * @param mixed $stock
     */
    public function setStock(Stock $stock)
    {
        $this->stock = $stock;
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }
}

class Stock
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     */
    private $stock_name;

    /**
     * @ORM\Column(type="string")
     */
    private $address;

    /**
     * @ORM\OneToMany(
     *      targetEntity="Products",
     *      mappedBy="stock",
     *      orphanRemoval=true
     * )
     */
    private $products;

    /**
     * @return mixed
     */
    public function getStockName()
    {
        return $this->stock_name;
    }

    /**
     * @param mixed $stock_name
     */
    public function setStockName($stock_name)
    {
        $this->stock_name = $stock_name;
    }

    /**
     * @return mixed
     */
    public function getAddress()
    {
        return $this->address;
    }

    /**
     * @param mixed $address
     */
    public function setAddress($address)
    {
        $this->address = $address;
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return mixed
     */
    public function getProducts()
    {
        return $this->products;
    }

    /**
     * @param mixed $products
     */
    public function setProducts($products)
    {
        $this->products = $products;
    }
}

I need sql that gives me all delivery addresses which associated with stock id 1 and add to this array only one stock address under id 1 from table Stock. 我需要sql,它给我所有与库存ID 1相关联的交货地址,并在表格库中仅将1个库存地址添加到ID 1下。 The array should look like: 该数组应如下所示:

Array ( 
    [0] => Stock Address
    [1] => delivery1
    [2] => delivery2
    [3] => delivery3
)

I try this statement but it gives multiply stock address and array in array: 我尝试这个语句,但它在数组中给出了多个库存地址和数组:

$qb = $this->getEntityManager()->createQuery(
    "SELECT o.delivery , p.address
        FROM AppBundle:Products o 
        JOIN o.stock p
        WHERE o.stock =1
    ");

return $qb->getScalarResult();

and the result of this statement: 以及本声明的结果:

Array ( 
    [0] => Array ( 
        [delivery] => Spoon str, USA 
        [address] => Wall street 1, USA 
    ) 
    [1] => Array ( 
        [delivery] => Lincoln street, USA 
        [address] => Wall street 1, USA 
    )
)

You won't be able to retrieve the array you want directly via one DQL statement. 您将无法通过一个DQL语句直接检索所需的数组。 The reason is that you actually want the four rows that are fetched in your example to be from two different columns. 原因是您实际上希望示例中提取的四行来自两个不同的列。

So you need to process the result somehow. 所以你需要以某种方式处理结果。 For example you could change your snippet to: 例如,您可以将代码段更改为:

$qb = $this->getEntityManager()->createQuery(
"SELECT o.delivery , p.address
    FROM AppBundle:Products o 
    JOIN o.stock p
    WHERE o.stock = 1
");

$result = $qb->getScalarResult();

return array_merge([$result[0]['address']], array_column($result, 'delivery'));

So you take the address as the first element of the array to be returned and add the delivery column only of all results and add them as well. 因此,您将地址作为要返回的数组的第一个元素,并仅添加所有结果的传递列,并添加它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM