2011-09-07 44 views
0

我想在indexSuccess.php類別中顯示標題和該類別的元素。這些是我的兩個表:獲取只有該類別的類別標題和元素 - symfony

SgpsCategories: 
    columns: 
    description: { type: string(255), notnull: true } 

SgpsCertificats: 
    actAs: { Timestampable: ~ } 
    columns: 
    categories_id: { type: integer, notnull: true } 
    titre: { type: string(255), notnull: true } 
    type: { type: string(255), notnull: true } 
    taille: { type: string(50), notnull: true } 
    chemin: { type: string(255), notnull: true } 
    relations: 
    SgpsCategories: { onDelete: CASCADE, local: categories_id, foreign: id } 

到目前爲止,我這樣做的動作:

public function executeIndex(sfWebRequest $request) 
    { 


    $this->categories = Doctrine_core::getTable('SgpsCategories') 
     ->createQuery('b') 
     ->execute(); 

    $this->sgps_certificatss = Doctrine_Core::getTable('SgpsCertificats') 
     ->createQuery('a') 
     ->execute(); 
    } 

,我這樣做是在indexSuccess.php的:

<?php foreach ($categories as $categorie): ?> 

    <div id="titulo-certificado"><?php echo $categorie->getDescription(); ?></div> 

    <?php foreach ($sgps_certificatss as $sgps_certificats): ?> 

     <?php echo $sgps_certificats->getTitre()."<br>";?> 

    <?php endforeach; ?> 
<?php endforeach; ?> 

我在做什麼這裏錯了嗎?謝謝

回答

0

那麼你在獲取數據時沒有使用兩個表之間的關係。查詢和你一樣從數據庫中獲取全部類別和全部認證(並非所有類別的認證)。

如果你想在頁面中只顯示一個類別的所有元素,你應該改變你的查詢來獲取一個類別(請參閱 - > fetchOne()),指定你需要的類別,然後在其中使用它的id第二個查詢的子句。或者只是加入他們只使用一個查詢。

如果要顯示所有類別,他們的元素,在一個網頁中我會添加一個foreignAlias的關係SgpsCategories所以從SgpsCategories你可以有自己的所有元素:

SgpsCategories: 
    columns: 
    description: { type: string(255), notnull: true } 

SgpsCertificats: 
    actAs: { Timestampable: ~ } 
    columns: 
    categories_id: { type: integer, notnull: true } 
    titre: { type: string(255), notnull: true } 
    type: { type: string(255), notnull: true } 
    taille: { type: string(50), notnull: true } 
    chemin: { type: string(255), notnull: true } 
    relations: 
    SgpsCategories: { onDelete: CASCADE, local: categories_id, foreign: id, foreignAlias: Certificats } 

所以,你的行動:

public function executeIndex(sfWebRequest $request) 
{ 
    $this->categories = Doctrine_core::getTable('SgpsCategories') 
     ->createQuery('b') 
     ->execute(); 
} 

而且你的模板:

<?php foreach ($categories as $categorie): ?> 

    <div id="titulo-certificado"><?php echo $categorie->getDescription(); ?></div> 

    <?php foreach ($categorie->getCertificats() as $sgps_certificats): ?> 

     <?php echo $sgps_certificats->getTitre()."<br>";?> 

    <?php endforeach; ?> 
<?php endforeach; ?>