【问题标题】:symfony2 doctrine: many to many relation and SUBQUERY in doctrinesymfony2 学说:学说中的多对多关系和子查询
【发布时间】:2015-03-16 16:40:40
【问题描述】:

我有两张桌子: 存档

+-------------+--------------+------+-----+---------+----------------+
| 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    |       |
+------------+---------+------+-----+---------+-------+

这些表是通过多对多关系链接的,当然我也有一个用户表。运行 php app/console 学说: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,并在此新实体上进行查询。
  • 嗨,Veve,你能举个例子吗?我用实体定义更新了我的问题。谢谢
  • 看看我下面的答案,它包含一个例子;)
  • 我无法根据我的需要调整这个示例。我将使用学说的 createNativeQuery。无论如何感谢您的帮助。

标签: php sql symfony doctrine-orm


【解决方案1】:

在您的 2 个实体之间添加一个实体 ArchiveContact,并使用属性 present,并查询这个新实体。

此类实体from Doctrine documentation 的示例,其中实体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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 2014-09-29
    • 2017-01-22
    • 1970-01-01
    相关资源
    最近更新 更多