0

I want to show in an indexSuccess.php categories titles and the elements of that categories. These are my two tables:

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 }

So far I did this in the action:

 public function executeIndex(sfWebRequest $request)
  {


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

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

and i did this in the 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; ?>

What am I doing wrong here? thank you

Eva Dias
  • 1,709
  • 9
  • 36
  • 67

1 Answers1

0

Well you're not using relation between the two tables when fetching data. And querying like you did takes from database all Categories and all Certificats (not all Certificats for a category).

If you want to show only one category in a page with all his elements you should change your queries to fetch just one category (see ->fetchOne()) specifying which category you need, then use its id in the where clause for the second query. Or just join them to use just one query.

If you want to show all categories, and their elements, in a single page I'd add a foreignAlias to the relation SgpsCategories so from SgpsCategories you can have all his elements:

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 }

So your action:

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

And your template:

<?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; ?>
dlondero
  • 2,539
  • 1
  • 24
  • 33