symfony2 学说:多对多关系和学说中的子查询


symfony2 doctrine: many to many relation and SUBQUERY in doctrine

我有两个表:档案

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| author      | varchar(100) | NO   |     | NULL    |                |
| title       | varchar(100) | NO   |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

和收藏

+------------+---------+------+-----+---------+-------+
| Field      | Type    | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+-------+
| user_id    | int(11) | NO   | PRI | NULL    |       |
| archive_id | int(11) | NO   | PRI | NULL    |       |
+------------+---------+------+-----+---------+-------+

这些表与 ManyToMany 关系相关联,当然我还有一个用户表。集合表是运行 php 应用程序/控制台生成的 doctrine:schema:update,并且有实体定义:

用户实体

/**
* @ORM'OneToMany(targetEntity="My'ApplicationBundle'Entity'Archive", mappedBy="user")
**/
protected $archives;
/**
* @ORM'ManyToMany(targetEntity="My'ApplicationBundle'Entity'Archive", inversedBy="users")
* @ORM'JoinTable(name="collection")
**/
private $collection;

存档实体

/**
* @ORM'ManyToOne(targetEntity="My'UserBundle'Entity'User", inversedBy="archives")
* @ORM'JoinColumn(name="user_id", referencedColumnName="id")
**/
protected $user;
/**
* @ORM'ManyToMany(targetEntity="My'UserBundle'Entity'User", mappedBy="collection")
**/
private $users;

当我在存档表中搜索某些内容时,es:

select a.id, a.author, a.title, IF((select c.archive_id from collection c where c.archive_id = a.id and c.user_id = 1),1,0) as present from archive a;

我还会有一列指示用户 (es: id: 1) 是否在他的集合中拥有此存档,所以我的结果集应该是这样的

+----+---------------+--------------+---------+
| id | author        | title        | present |
+----+---------------+--------------+---------+
|  8 | test author 7 | test title 7 |       1 |
|  9 | test author 8 | title 8      |       0 |
| 10 | test 8 pdf    | title 9 pdf  |       1 |
+----+---------------+--------------+---------+

如何使用原则 DQB/DQL 翻译上述查询?

非常感谢

使用属性present在 2 个实体之间添加一个实体ArchiveContact,然后查询此新实体。

Doctrine文档中此类实体的示例,其中实体order而不是user,实体product而不是archive,属性offeredPrice而不是present

在经典的订单产品商店示例中,有 包含对订单和产品的引用的订单项,以及 其他数据,例如购买的产品数量以及可能 甚至是当前的价格。

<?php
use Doctrine'Common'Collections'ArrayCollection;
/** @Entity */
class Order
{
    /** @Id @Column(type="integer") @GeneratedValue */
    private $id;
    /** @ManyToOne(targetEntity="Customer") */
    private $customer;
    /** @OneToMany(targetEntity="OrderItem", mappedBy="order") */
    private $items;
    /** @Column(type="boolean") */
    private $payed = false;
    /** @Column(type="boolean") */
    private $shipped = false;
    /** @Column(type="datetime") */
    private $created;
    public function __construct(Customer $customer)
    {
        $this->customer = $customer;
        $this->items = new ArrayCollection();
        $this->created = new 'DateTime("now");
    }
}
/** @Entity */
class Product
{
    /** @Id @Column(type="integer") @GeneratedValue */
    private $id;
    /** @Column(type="string") */
    private $name;
    /** @Column(type="decimal") */
    private $currentPrice;
    public function getCurrentPrice()
    {
        return $this->currentPrice;
    }
}
/** @Entity */
class OrderItem
{
    /** @Id @ManyToOne(targetEntity="Order") */
    private $order;
    /** @Id @ManyToOne(targetEntity="Product") */
    private $product;
    /** @Column(type="integer") */
    private $amount = 1;
    /** @Column(type="decimal") */
    private $offeredPrice;
    public function __construct(Order $order, Product $product, $amount = 1)
    {
        $this->order = $order;
        $this->product = $product;
        $this->offeredPrice = $product->getCurrentPrice();
    }
}