-2

I want make a query like that

SELECT `User`.`id`, `User`.`nome`, `User`.`email`, `User`.`slug`,
`Photo`.`id`, `Photo`.`title`, `Photo`.`user_id`,
`Scheda`.`id`, `Scheda`.`user_id`, `Scheda`.`size`, `Scheda`.`weight`,
`Size`.`id`, `Size`.`value`, `Weight`.`id`, `Weight`.`value`
FROM `database`.`users` AS `User`
LEFT JOIN `database`.`photos` AS `Photo`
    ON (`Photo`.`user_id` = `User`.`id`)
LEFT JOIN `database`.`schedas` AS `Scheda`
    ON (`Scheda`.`user_id` = `User`.`id`)
LEFT JOIN `database`.`sizes` AS `Size`
    ON (`Size`.`id` = `Scheda`.`size`)
LEFT JOIN `database`.`weights` AS `Weight`
    ON (`Weight`.`id` = `Scheda`.`weight`)
WHERE `slug` = 'alessandro' LIMIT 1 

How can I do into the controller?

Thanks Alessandro

Oldskool
  • 34,211
  • 7
  • 53
  • 66

1 Answers1

0

Solved

I created Model for Size and Weight and then I inserted into the UserController the code:

$this->loadModel('Size');      
$size=$this->Size->find('all', array(
 'conditions' => array('Scheda.user_id = '.$data['User']['id']),
    'fields'=>array('Size.value')
));
$this->set('size',$size);

The only question is...Why into view I must call the value with 0?

echo $size['0']['Size']['value']

echo $size['Size']['value'] //didn't work.

Alessandro

  • two tips 1) don't use string conditions - they offer you no protection from sql injection (compare the code in the answer to `'conditions' => array('Scheda.user_id' => $data['User']['id'])` and consider what happens if that value is "1 OR 1=1") 2) don't ask questions in answers (it's because you're finding _all_ - i.e. more than one result is an expected result). +1 for answering your own question. Actually - I retract that since it looks like you've answered a different question to the one you've asked. – AD7six Aug 03 '14 at 15:30