2015-05-26 34 views
0

我有一個實體Article20000Information,用以下幾個領域:iddescriptionmanufacturer_idsupplier_id如何根據ID拉取信息?

我有另一個實體,Organisation。它有一個公司名單(兩個製造商&供應商),每個公司都有一個id

我也有一個呈現Article20000Information數據列表的頁面。目前,它只是顯示在表格中的數據:

| id | Description | Manufacturer | Supplier | Price | 
|----|-------------|--------------|----------|--------| 
| 1 | thing1 |  2  | 5  | 34 | 
| 2 | thing2 |  5  | 2  | 23 | 
| 3 | thing3 |  3  | 4  | 25 | 

我需要的是對製造商和供應商列中顯示從organisationname值的基礎上,顯示的ID。

什麼是最好的方式去做這件事?

+0

[join](http://en.wikipedia.org/wiki/Join_%28SQL%29) –

+0

您是否使用'Doctrine'作爲ORM? –

+0

@AlexandruOlaru是 – benoliver999

回答

1

明白了!

我需要多個別名,我猜想,但我也需要給他們AS,以便他們出來不同的列名稱。這又使枝條呈現標籤。

<?php 

namespace Regenerys\QMSBundle\Entity; 

use Doctrine\ORM\EntityRepository; 

class Article20000InformationRepository extends EntityRepository 
{ 
    public function findStuff() 
    { 
     return $this->getEntityManager() 
      ->createQuery(
       'SELECT 
    A.id, 
    A.articleNumber, 
    A.description, 
    B.name as manufacturer, 
    C.name as supplier 
FROM 
    RegenerysQMSBundle:Article20000Information A 
    LEFT OUTER JOIN RegenerysQMSBundle:Organisation B WITH B.id = A.manufacturer 
    LEFT OUTER JOIN RegenerysQMSBundle:Organisation C WITH C.id = A.supplier ' 

      ) 
      ->getResult(); 
    } 
} 

感謝@Alexandru對他的DQL幫助。

0

您需要根據id條件加入兩個表。

select A.id, A.Description, B.ManufacturName, B.supplierName 
from Article20000Information A 
left outer join Organisation B 
ON B.id = A.id 

More info on table joins.

0

如果您使用的原則,正確的方法是創建一個Repository類,並有寫建議您加入代碼由@ K139但DQL

class Article20000InformationRepository extends EntityRepository 
{ 
    public function findAll() 
    { 
     return $this->getEntityManager() 
      ->createQuery(
       'SELECT A.id, A.Description, B.ManufacturName, B.supplierName FROM AppBundle:Article20000Information A 
       LEFT OUTER JOIN AppBundle:Organisation B ON B.id = A.id ' 
      ) 
      ->getResult(); 
    } 
} 

然後在您的控制器中,您將使用它:

$articles = $em->getRepository('AppBundle:Article20000Information')->findAll(); 
+0

好東西,謝謝。 這是我 '選擇A.id,A.articleNumber,A.description,B.name,A.manufacturerCatalogueNumber,B.name,A.supplierCatalogueNumber,B.name,A.secondarySupplierCatalogueNumber,A.countedAs ,A.amountInsideCounted,A.purchasedAs,A.price,A.whereToFindCertificates FROM RegenerysQMSBundle:Article20000Information A LEFT OUTER JOIN RegenerysQMSBundle:Organization B WITH B.id = A.id' 如何獲取它,以便製造商和供應商是從同一個「Organisation」表中提取的? – benoliver999

+0

不,你應該加入他們,如組織 –

+0

對不起,我不清楚。 ID位於同一個表中。現在我從'organisation'獲取'name',並正確顯示在製造商下。我需要爲供應商再次運行同樣的事情 - 根據ID從'organisation'顯示'name'。 – benoliver999

相關問題