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
https://forums.phpfreaks.com/topic/57048-muliple-hasmany-relationships/
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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.