Jump to content

Muliple hasMany relationships


flaab

Recommended Posts

Hi =)

 

I'm trying out CakePhp and i've readed all the manual.

 

The hasMany relationships are pretty cool and they are easy to use.

 

But...even though I've seen plenty of examples I haven't figured out the way to implement multiple hasMany relationships.

 

Here's a simple hasMany relationship.

class user extends AppModel
{
var $name = "User";
var $validate = array();

/**
 * RELACIONES
 * 1) hasMany Challenges
 * 2) hasMany Files
 * 3) hasMany Rounds (Como ganador)
 */

 var $hasMany = array('Challenge') =>

 			array('className') => 'Challenge',
 				'conditions'	=> 'Challenge.active = 1',
 				'order'		=> 'Challenge.created DESC',
 				'limit'		=> '',
 				'foreignKey'	=> 'user_id',
 				'dependent'	=> 'true',
 				'exclusive'	=> 'false',
 				'finderSql'	=> ''
 			)

 		);

}

 

But...What if my User has more hasMany relationships with other tables? Like...besides putting challenges on the web, it has many articles written? How can I define both?

 

Thanks.

Link to comment
Share on other sites

I'm almost new to CakePHP but I have a project which includes a dealer-model containing the following line:

 

var $hasMany = array('Product','Advert');

 

It does what I thought it would... let me access multiple Products and multiple Adverts associated with a single user:

 

 

 

$this->set('myProducts', $this->Dealer->Product->findAll('user_id = 4',array('id','header','user_id'),null,5));
$this->set('myAdverts', $this->Dealer->Advert->findAll('user_id = 4',array('id','header','user_id'),null,5));

 

Maybe it can help  you :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.