Reputation: 2047
I have 3 tables:
Certificates, Request and Requestc
Request hasMany Requestc.
Requestc use the certificates_requests table and has id
request_id
and certificate_id
coloumns.
I'm trying to retrive data from Request
table and im trying to retrive the names of certificates associated with that Request.
I'm trying this without sucess:
$options['joins'] = array (
array( 'table' => 'certificates_requests',
'alias' => 'Requestc',
'type' => 'left',
'conditions' => array('Certificate.id = Requestc.certificate_id')
)
);
$certidoes = $this->Request->find('all', $options);
My models:
class Certificate extends AppModel {
public $name = 'Certificate';}
class Request extends AppModel {
public $name = 'Request';
public $hasMany = array(
'Requestc' => array(
'foreignKey' => 'request_id'
)
); }
class Requestc extends AppModel {
public $name = 'Requestc';
public $belongsTo = 'Request';
public $useTable = 'certificates_requests'; }
Upvotes: 0
Views: 719
Reputation: 2509
That looks like a HABTM relationship to me.
Containable behavior will solve your problem. Read More Here
<?php
// Request.php // Model
var $actsAs = array('Containable');
var $hasAndBelongsToMany = array(
'Certificate' => array(
'className' => 'Certificate',
'joinTable' => 'certificates_requests',
'foreignKey' => 'request_id',
'associationForeignKey' => 'certificate_id'
)
);
// Certificate.php // Model
var $actsAs = array('Containable');
var $hasAndBelongsToMany = array(
'Request' => array(
'className' => 'Request',
'joinTable' => 'certificates_requests',
'foreignKey' => 'certificate_id',
'associationForeignKey' => 'request_id'
)
);
// RequestsController.php
$this->Request->contain(array(
'Certificate' => array(
'fields' => array('id', 'name')
)
));
$request = $this->Request->read(null, <requestIdHere>);
// $request = array(
// 'Request' => array(
// '<requestData>',
// ...
// ),
// 'Certificate' => array(
// [0] => array(
// 'id' => <idGoesHere>,
// 'name' => "<nameGoesHere>'
// ),
// [1] => array(
// 'id' => <idGoesHere>,
// 'name' => "<nameGoesHere>'
// ),
// [2] => array(
// 'id' => <idGoesHere>,
// 'name' => "<nameGoesHere>'
// ),
// )
// )
?>
Let me know if this solves your problem. :)
-Drew
Upvotes: 1