Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-10-2008, 09:26 AM
Junior Member
 
Join Date: May 2008
Posts: 2
Default Linking two tables with many2many relation

Hi all
I try to link two tables, Blogs and Membres, with an Blogsmembres intersection table :

Here are the classes I use :

PHP Code:
class Blogs extends Zend_Db_Table_Abstract
{
    protected 
$_name 'blogs';
    protected 
$_primary 'id';
    protected 
$_dependentTables = array('Blogsmembres');

PHP Code:
class Membres extends Zend_Db_Table_Abstract
{
    protected 
$_name 'membres';
    protected 
$_primary 'id';
    protected 
$_dependentTables = array('Blogsmembres');

PHP Code:
class Blogsmembres extends Zend_Db_Table_Abstract
{
    protected 
$_name 'blogs_membres';
    protected 
$_primary = array('membre_id','blog_id');
    protected 
$_referenceMap    = array(
     
'Membres' => array( 
            
'columns'           => 'membre_id',
            
'refTableClass'     => 'Membres',
            
'refColumns'        => 'id'
        
),   
     
'Blogs' => array( 
            
'columns'           => 'blog_id'
            
'refTableClass'     => 'Blogs'
            
'refColumns'        => 'id'
        
));

Here is the controller I use :
PHP Code:
public function indexAction() {
        
$this->view->titre"Accueil";
        
        
//Module Blogs
        
$blog = new Blogs();
        
$selection=$blog->select()->setIntegrityCheck(false);
        
$selection->from($blog);
        
$selection->joinLeft("blogs_membres","blogs.id=blogs_membres.blog_id");
        
$selection->joinLeft("membres","membres.id=blogs_membres.blog_id");
        
$selection->group('blogs.id');
        
$this->view->sites=$blog->fetchAll($selection);
    } 
Here is the view I use :
HTML Code:
<table border="1" cellspacing="0" cellpadding="2">
	<tr><td>Nom</td><td>Auteur</td><td>Actions</td>
<?php foreach($this->sites as $site) :
$site_authors=$site->findMembresViaBlogsmembres();
foreach($site_authors as $site_author)
{
$authors[$site_author->membre_id]=$site_author->pseudo;
}
?>
<tr>
	<td><a href="<?php echo $this->escape($site->url_site);?>"><?php echo $this->escape($site->titre);?></a></td>
	<td><?php echo implode(",",$authors);?></td>
	<td>
	  <a href="<?php echo $this->baseUrl; ?>/blogs/modifier/id/<?php 
          echo $site->id;?>">Modifier</a>
  <a href="<?php echo $this->baseUrl; ?>/blogs/supprimer/id/<?php 
      echo $membre->id;?>">Supprimer</a>
	</td>
</tr>
<?php endforeach; ?>
</table>
Is existing any dirty-less method than that ? Am I forced to use some PHP code in my view ?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-11-2008, 01:09 PM
Junior Member
 
Join Date: Apr 2008
Posts: 6
Default

I would move some of that controller logic into your model, and call a simpler (human-readable) function in your controller. Then your view won't have any logic in it either.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-11-2008, 01:28 PM
Junior Member
 
Join Date: May 2008
Posts: 2
Default

So you think I should simply call a controller-side defined function in my view ?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 11:31 AM.