Wednesday, March 20, 2013

Re: HABTM Inner Join with 3 models

Hello,
I solved the problem now as follows

JobsController.php
...
$drivers = $this->Job->query("SELECT User.id, (CONCAT(vorname, ' ',nachname)) AS name FROM users AS User INNER JOIN users_events AS Event ON User.id = Event.user_id WHERE Event.event_id = $jobid");
...

edit.ctp
$select_value = array();
$select_value[null] = '-- Make a selection --';
foreach($drivers as $driver) {
$key     = $driver['User']['id'];
$value   = $driver[0]['name'];
$select_value[$key]    = $value;
}

$mydata = $this->data;
if ( $mydata['Job']['driver_id'] != null ) {
foreach ( $select_value as $k => $v ) {
if ( $mydata['Job']['driver_id'] == $k ) {
$this_is_selected =  $k; 
}
}
} else {
$this_is_selected = 0;    
}
echo $this->Form->input('driver_id', array(
'options' => $select_value,
'type' => 'select',
'label' => 'Driver'
));

add.ctp
$select_value = array();
$select_value[null] = '-- Make a selection --';
foreach($drivers as $driver) {
$key     = $driver['User']['id'];
$value   = $driver[0]['name'];
$select_value[$key]    = $value;
}

echo $this->Form->input('driver_id', array(
'options' => $select_value,
'type' => 'select',
'label' => 'Driver'
));


Okay it's not CakeLike, but it works flawlessly.

Nice Greetings

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: