2011-09-07 52 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; ?>