簡體   English   中英

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 app / console 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翻譯以上查詢? 非常感謝

在您的2個實體之間添加一個實體ArchiveContact ,其中present一個屬性,並對該新實體進行查詢。

此類實體的實施例從學說文檔 ,與實體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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM