Cakephp 2 Containable does not work in custom library

91 views Asked by At

It does work in a controller:

$topic_word = ClassRegistry::init('TopicsTopicWord');
$result = $topic_word->find('all', [
    'contain'    => ['TopicWord'],
    'conditions' => [
        'TopicsTopicWord.topic_id' => '1777125',
        'TopicWord.status >=' => 1,
    ]
]);
var_dump($result); die();

But the same code gives me this error when I call it from command line: cake feed make

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'TopicWord.status' in 'where clause'

in my app/Model/TopicsTopicWord.php

class TopicsTopicWord extends AppModel
{
    public $recursive = -1;

    public $actsAs    = ['Containable'];
    public $belongsTo = ['TopicWord'];
...
}

in my app/Model/TopicsTopicWord.php

 class TopicWord extends AppModel
 {
     public $recursive = -1;
...

Cake's version: 2.2.1

PHP's version: 5.4.23

+++ UPDATE +++

Thanks to @ndm's comment I've checked the log file and the working sql that is in the controller does contain:

LEFT JOIN
    `db`.`topic_words` AS `TopicWord` ON
        (`TopicsTopicWord`.`topic_word_id` = `TopicWord`.`id`)

But the other ones does not have the LEFT JOIN ... sql part:

SELECT
    TopicsTopicWord.id, TopicsTopicWord.topic_id, TopicsTopicWord.topic_word_id 
FROM
    db.topics_topic_words AS TopicsTopicWord
WHERE
    TopicsTopicWord.topic_id = 1777125 AND TopicWord.status >= 1

+++ UPDATE #2 +++

Thank you very much @ndm. You are right, it does return "AppModel":

$topic_word = ClassRegistry::init('TopicsTopicWord');
var_dump(get_class($topic_word)); die();
...
/Users/xxx/mdpr-vagrant/projects/xxx/app/Lib/FeedFormatter/LineFormatter.php:151:
string(8) "AppModel"

and the same code in the controller returns:

.../app/Controller/TopicsController.php:711:string 'TopicsTopicWord' (length=15)

Looks like I'm not the only one who get this behaviour: https://github.com/cakephp/cakephp/issues/9133

0

There are 0 answers