2015-05-04 23 views
0

我正在爲Symfony2使用APYDataGridBundle。APYDataGridBundle:不顯示別名列數據

我最終的目標是每個用戶在網格中有一行(現在可以),並且有一個帶有id或null的favoris_id列(然後用JS我可以設法顯示聯繫人是否是用戶的一部分最喜歡還是不用ajax來添加或刪除最喜歡的)。

如果我使用註釋來添加列,那麼每個現有收藏夾中的每個用戶都會以一行而不是1行爲登錄用戶的null或id結束。

因此,我操縱查詢來添加一個字段,然後添加一個別名列。生成的查詢正常,列si顯示,但單元格爲空,它們不顯示f.id。

這裏是我的查詢:

$source->manipulateQuery(
      function ($query) use ($alias, $userId, $sourceType) { 
       $query 
        ->addSelect('f.id favorisid') 
        ->leftJoin($alias . '.usersFavoris', 'f', 'WITH', 'f.user = :user') 
        ->andWhere($alias . '.source = :sourceparam') 
        ->setParameter('user', $userId) 
        ->setParameter('sourceparam', $sourceType) 
       ; 
      } 

這裏是我的別名的列:

// Add Column favoris_id 
    $myCol = new TextColumn(
     array( 'id'   => 'favoris_id', 
       'title'   => 'favoris id', 
       'field'   => 'favorisid', // The aliased name 
       'isManualField' => true, // Indicate it is a manual (or aliased) field 
       'isAggregate' => false, // Defaults to false, set true if using aggregate func. like SUM() 
       'source'  => true, // Indicates the grid should retrieve it from the source (the query) 
    )); 
    $grid->addColumn($myCol,8); 

這裏是接觸實體的相關部分:

/** 
* @ORM\OneToMany(targetEntity="Curuba\contactsBundle\Entity\contactsFavoris", mappedBy="contact", orphanRemoval=true, cascade={"remove", "persist"}) 
* @ORM\JoinColumn(nullable=true) 
*/ 
private $usersFavoris; 

可運行的查詢提供由探查器正確:

SELECT c0_.nom AS nom0, c0_.prenom AS prenom1, e1_.nom AS nom2, 
f2_.fonction AS fonction3, u3_.fullname AS fullname4, c0_.updated AS updated5, 
c0_.id AS id6, c0_.fullname AS fullname7, c0_.prefix AS prefix8, 
c0_.communications AS communications9, e1_.cp AS cp10, e1_.adresse AS adresse11, 
f2_.persomail AS persomail12, c4_.nom AS nom13, s5_.nom AS nom14, s6_.nom AS nom15, 
c7_.id AS id16 
FROM contacts c0_ 
LEFT JOIN fonctions f2_ ON c0_.id = f2_.contact_id 
LEFT JOIN entites e1_ ON f2_.entite_id = e1_.id 
LEFT JOIN users u3_ ON c0_.referent_id = u3_.id 
LEFT JOIN categories c4_ ON e1_.categorie_id = c4_.id 
LEFT JOIN souscategories s5_ ON e1_.souscategorie_id = s5_.id 
LEFT JOIN sources s6_ ON e1_.source_id = s6_.id 
LEFT JOIN contactsFavoris c7_ ON c0_.id = c7_.contact_id AND (c7_.user_id = 59) 
WHERE c0_.source_id = 1 
ORDER BY c0_.fullname ASC 
LIMIT 20 

回答

0

我從這個包的貢獻者之一得到了答案。 即使有'飛行'參數,我們也必須使用相同的名稱。 因此,使用favorisid而不是favoris_id解決了問題:

// Add Column favoris_id 
$myCol = new TextColumn(
    array( 'id'   => 'favorisid', 
      'title'   => 'favorisid', 
      'field'   => 'favorisid', // The aliased name 
      'isManualField' => true, // Indicate it is a manual (or aliased) field 
      'isAggregate' => false, // Defaults to false, set true if using aggregate func. like SUM() 
      'source'  => true, // Indicates the grid should retrieve it from the source (the query) 
)); 
$grid->addColumn($myCol,8);