0
votes

I am having a weird problem. I have a model named Courses. This model has an association with another model from another database. It is a pretty simple configuration, nothing too elaborate.

When I run this:

$courses = $this->Course->find('all');

Nothing is returned. In debug mode the SQL it shows is correct and it does run correctly when manually running the SQL, but the results are not saved to the variable $courses.

I have also tried this:

debug($this->Course->find('all'));

Again - the SQL is shown correctly in the SQL section of the debug, but no records are displayed in the debug session.

Query - $this->Model->find('all')

Now for the weird part -- if I change 'all' to 'first' the first record is displayed and everything works as expected.

$courses = $this->Course->find('first');

Query - $this->Model->find('first');

I tried moving this statement to other controllers and the results are the same. The sql is generated and run properly but the results are not returned through Cake and saved in the variable. I do not think it is a variable issue as it will not be returned properly if I wrap the find in a debug statement. If it were a variable problem then it would work when I remove the variable form the equation, but it does not work then either.

I am on CakePHP 2.5.5. I tried to update to 2.6.1 but the results were the same.

I tried to insert the SQL in a variable and run it as a query. The SQL running against the database works, but when I run it using the model it does not work. Same results as with the find all.

$sql = "SELECT " . 
" `Course`.`id`, `Course`.`title`, `Course`.`descr`, `Course`.`created`, `Course`.`modified`, `Course`.`hours`, `Course`.`price`, " . 
" `Course`.`accounting_code_id`, `Course`.`serf_course_id`, `Course`.`comp_code`, `Course`.`course_type`, `Course`.`clinic`, `Course`.`pharmacy`, " .
" `Course`.`healthcare_professional`, `Course`.`scholarship`, `Course`.`pharmacy_tech`, `Course`.`uan`, `Course`.`available`, `Course`.`acpe`, `AccountingCode`.`id`, " .
" `AccountingCode`.`code`, `AccountingCode`.`code_description`, `AccountingCode`.`buyer_percentage`, `AccountingCode`.`created`, `AccountingCode`.`modified`," .
" `AccountingCode`.`deleted`, `AccountingCode`.`created_user_id`, `AccountingCode`.`modified_user_id`, `AccountingCode`.`deleted_user_id`, " .  
" `AccountingCode`.`deleted_record` FROM `cips`.`courses` AS `Course` LEFT JOIN `cips`.`accounting_codes` AS `AccountingCode` ON (`Course`.`accounting_code_id` = " . 
" `AccountingCode`.`id`) WHERE 1 = 1";

    $this->Course->recursive = 0;

    // $allCourses = $this->Course->find('all');
    $allCourses = $this->Course->query($sql);

    debug($allCourses, true, true);

Any ideas?

1
debug($this->Course->find('all)); has a typo in it. Is that the code you were using?caitlin
Nope -- that was a typo in the post.iPhone Guy
Copy the sql query directly from the code and try it in mysql. See if that returns a result first.Colonel Mustard
When I copy the sql query in to mysql it works as expected. the correct results are returned. for some reason the results are not saved in the variable.iPhone Guy
What does debug($courses); show you?Calamity Jane

1 Answers

0
votes

Ok - figured this one out with the help of a colleague. In the database config I had to specify the encoding to utf8. Once I changed the config the find worked as expected.

    public $default = array(
    'datasource' => 'Database/Mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'llll',
    'password' => 'pppp',
    'database' => 'dddd',
    'unix_socket' => '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock',
    'encoding'=>'utf8',
);