Currently my code below works fine but it’s a bit of overkill. In my controller I fetch the categories that have links and all the links in my database.
In my view I loop through all the categories and then when I want to add the links under the category I loop through all the links in my database instead I should only loop through the links that are assigned to the current category but I don’t know how to do this with Zend Framework. Can anybody send me into right direction. Thank’s for your time.
Controller:
public function indexAction()
{
$this->view->title = App_Translate::translate('links_title');
$this->view->headTitle($this->view->title, 'PREPEND');
$linkCat = Doctrine_Query::create()
->distinct()
->from('LinkCategory lc')
->innerJoin('lc.Link l WITH lc.id = l.link_category_id')
->orderBy('lc.id')
->execute();
$links = Doctrine_Query::create()
->from('Link')
->execute();
$this->view->linkCat = $linkCat;
$this->view->links = $links;
}
}
View:
<?php if(!empty($this->linkCat)): ?>
<ul>
<?php foreach($this->linkCat as $linkCat): ?>
<li><h2><?php echo $this->escape($linkCat['title']); ?></h2>
<?php if(!empty($this->links)): ?>
<ul>
<?php foreach($this->links as $link): ?>
<?php if($link['link_category_id'] == $linkCat['id']): ?>
<li><a href="<?php echo $this->escape($link['url']); ?>"><?php echo $this->escape($link['title']); ?></a></li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>No links added</p>
<?php endif; ?>
Your question is not really related to Zend Framework. You are fetching data using Doctrine, not ZF.
In your case, I think you should be able to loop over the links in the specific category using
foreach($linkCat->Link as $link), seeing how you use innerJoin to load the relation.