mfrascati
mfrascati

Reputation: 81

CakePHP 2.2 find returns empty data even if query matches results

I have a fresh baked (with bake console) project in which I have 2 models behaving differently when I call model->find function.

UsersController

public function index() {
    $this->User->recursive = 0;
    $this->set('users', $this->paginate());
}

WordsController

public function index() {
    $this->Word->recursive = 0;
    $this->set('words', $this->paginate());
}

Query (DebugKit)

SELECT `Word`.`*` FROM `words` AS `Word` WHERE `Word`.`iniziale` = 'A' AND `Word`.`pubblicata` = '1' ORDER BY `Word`.`parola` ASC LIMIT 10
Affected 10 
Num rows 10

In both cases the query inspected has affected rows, but the WordsController doesn't return any results if I debug the paginate() result, while the Users one gives correctly.

Word model has no relations, and I tried to change the model name to Term, obtaining the same result.

I tried also to downgrade CakePHP core to 2.1.4. Nothing.

Are there any possible causes to this problem? Is Word some kind of reserved keyword? How can this be debugged?

Upvotes: 1

Views: 2287

Answers (3)

Arvind K.
Arvind K.

Reputation: 1294

Quoting from a comment in the question above..

Check your model and AppModel and see whether there is any afterFind(). The afterFind call must return the $results dataset at the end.

function afterFind($results, $primary = false)  {
    //some code here..
    return $results; //I had this commented out for some reason
    //and it generated the same issue.
}

Upvotes: 0

gdm
gdm

Reputation: 7930

The correct answer follows that accepted. You should set the encoding in your DB configuration: In my conf I had:

public $default = array(
     'datasource' => 'Database/Mysql',
                'persistent' => false,
                'host' => 'localhost',
                'login' => 'giuseppe',
                'password' => 'pass',
                'database' => 'db'
                'prefix' => '',
                //'encoding' => 'utf8',

    );

Do you see the encoding key? Try to uncomment or set it to the correct encoding, and voila!

Upvotes: 2

mfrascati
mfrascati

Reputation: 81

I figured it out.

I had in the words table (UTF8), some texts containing special characters like àòèéìù. Cake removed all the result containing these characters. I tried replacing "è" with "e" and magically the record was available in Cake!

Hope my 5 hours of headache will help someone else!

Upvotes: 5

Related Questions