I’d like to have a second table with data in my controller for using it in the view.
At this point I have a controller named ‘PortfoliosController.php’. In this controller I have a public function called Index, here I’d like to have a join in it.
So far I have the following code (only I still can’t access the data from the join table):
public function index() {
$this->Portfolio->recursive = -1;
$options = $this->Portfolio->find('all', array('joins' => array(
array(
'table' => 'students',
'alias' => 'Student',
'type' => 'LEFT',
'foreignKey' => true,
'conditions'=> array('Student.userid = Portfolio.userid')
)
)));
$this->set('portfolios', $this->Portfolio->find('all', $options));
}
Is anyone here seeing a problem, or have a answer that might help!?
The best way to get the data from joining tables, would be to set up a relationship between the two tables in your portfolio model.
So in this case it sounds like a hasmany relationship would be used, because a portfolio could contain many results from the second table? This meant that when you do a find ‘all’, not only would it find all the the data from that table, but also the data from the second table due to its relationship.
more info about setting up the relationships here http://book.cakephp.org/1.3/view/1040/Relationship-Types