Bonjour,
J'aimerai faire ce SQL en utilisant les fonction cakePHP
SELECT m.id, u.name, ms.sector_id,ms.status FROM mailboxes as m
left join `msuses` as ms on m.id=ms.mailbox_id
left join users as u on ms.user_id=u.id
WHERE m.id = 1
La table mailboxes
public $hasMany = array(
'Msus' => array(
'className' => 'Msus',
'foreignKey' => 'mailbox_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
La table User
public $hasMany = array(
'Msus' => array(
'className' => 'Msus',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
);
La table Msuses
public $belongsTo = array(
'Mailbox' => array(
'className' => 'Mailbox',
'foreignKey' => 'mailbox_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Sector' => array(
'className' => 'Sector',
'foreignKey' => 'sector_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
avec cette requetes, j'arrive au résultat souhaité seulement si j'utilise le comportement "Containable" cakePHP, fait une multitude de requête
$this->set('mailbox', $this->Mailbox->find('first', array(
'conditions' => array('Mailbox.id' => $id),
'contain' => array('Msus' => array(
'order' => array('Msus.sector_id'),
'User'=>array('fields'=>'full_name')
)
)
))
);
Alors que je devrai réussir à obtenir mon résultat en un seul SQL comme celui écrit au début...
Est ce possible de le faire en un seul SQL avec cakePHP ?
Merci pour votre aide
$this->Msus->read(null,$id);
ou
$this->Mailbox->query("SELECT m.id, u.name, ms.sector_id,ms.status FROM mailboxes as m
left join `msuses` as ms on m.id=ms.mailbox_id
left join users as u on ms.user_id=u.id
WHERE m.id = 1");
Essai avec ce code :
$options'joins'] = array(
array('table' => 'msuses',
'alias' => 'msuses',
'type' => 'left',
'conditions' => array(
'mailboxes.id = msuses.mailbox_id'
)
),
array('table' => 'users',
'alias' => 'users',
'type' => 'left',
'conditions' => array(
'msuses.user_id= users.id'
)
)
);
$options'conditions'] = array(
'msuses.id' => '1'
);
$Mailboxe = $this->Mailboxe->find('all', $options);
Dans la doc, voilà la partie qui t'intéresse je pense lien