CakePHP找到所有未设置值的变量。所有其他查找功能都有效

时间:2012-03-05 13:49:28

标签: php sql cakephp

在我的控制器中,我有两个函数可以从db中获取状态= 4的所有记录。在一个函数中,它可以正常工作。我从工作函数中复制了find all语句:

$this->set('completed', $this->Topic->find('all', array('fields' => array(
                    'Topic.creator','Topic.link','Topic.id', 'Topic.topic_name', 'Topic.info', 'Topic.priority', 'Topic.user_id',
                    'Topic.completed','Topic.created', 'Topic.status','User.name','User.id','Topic.category','Topic.tags'),'conditions' => array(
                    'Topic.status' => 4))));

但是在新函数中,$completed数组似乎不存在。 debug语句只是一个空行。如果我使用debug($this->Topic->getDataSource()->getLog());调试sql日志,这是返回的数组:

Array
(
[log] => Array
    (
        [0] => Array
            (
                [query] => SELECT `Topic`.`creator`, `Topic`.`link`, `Topic`.`id`, `Topic`.`topic_name`, `Topic`.`info`, `Topic`.`priority`, `Topic`.`user_id`, `Topic`.`completed`, `Topic`.`created`, `Topic`.`status`, `User`.`name`, `User`.`id`, `Topic`.`category`, `Topic`.`tags` FROM `topics` AS `Topic` LEFT JOIN `users` AS `User` ON (`Topic`.`user_id` = `User`.`id`) LEFT JOIN `events` AS `Event` ON (`Event`.`topic_id` = `Topic`.`id`)  WHERE `Topic`.`status` = 4
                [affected] => 9
                [numRows] => 9
                [took] => 0
            )

    )

[count] => 1
[time] => 0
)

日志中的SQL语句有效。如果我插入mySQL会产生结果。受影响的numRows字段显示正确的记录数。但是生成的数据没有设置为变量。任何想法将不胜感激。我的老板和我都很难过。以下是完整的功能:

         public function dashboard(){
            $this->set('completed', $this->Topic->find('all', array('fields' => array(
                    'Topic.creator','Topic.link','Topic.id', 'Topic.topic_name', 'Topic.info', 'Topic.priority', 'Topic.user_id',
                    'Topic.completed','Topic.created', 'Topic.status','User.name','User.id','Topic.category','Topic.tags'),'conditions' => array(
                    'Topic.status' => 4))));
            $this->set('total_inprogress_release', $this->Topic->find('count', array('conditions' => array(
                    'OR' => array('status <>' => 4,'status <>' => 0),
                    'priority' => 4))));
    $this->set('upcoming_events', $this->Topic->Event->find('all'));
            $this->Topic->virtualFields['count'] = 'COUNT(*)';
            $this->set('graph_data', $this->Topic->find('chart', array('fields' => array('status_txt', 'priority_txt', 'count'), 'group' => array('status', 'priority'),
                    'chart' => array(
                            'xaxisTitle' => 'Status',
                            'yaxisLabels' => array('Release', 'Company', 'News'),
                            'xaxisLabels' => array('Open','In Progress','Completed'),
                            'yaxis' => 'Topic.priority_txt',
                            'xaxis' => 'Topic.status_txt',
                            'data' => 'Topic.count'
            ))));
    }

这是非工作功能:

        public function completed(){
            $foo = $this->Topic->find('all', array('fields' => array(
                    'Topic.creator','Topic.link','Topic.id', 'Topic.topic_name', 'Topic.info', 'Topic.priority', 'Topic.user_id',
                    'Topic.completed','Topic.created', 'Topic.status','User.name','User.id','Topic.category','Topic.tags'),'conditions' => array(
                    'Topic.status' => 4)));
            debug($foo);
            debug($this->Topic->getDataSource()->getLog());


    }

2 个答案:

答案 0 :(得分:1)

经过几个小时的跟踪数据,直到我们弄清楚它丢失的位置,我们发现问题是数据库中的一些数据。它被编码,导致核心蛋糕文件中的while语句在遇到所述数据时重置。解决方案是改变Config/database.php:     class DATABASE_CONFIG {

    public $default = array(
            'datasource' => 'Database/Mysql',
            'persistent' => false,
            'host' => 'localhost',
            'login' => 'login',
            'password' => 'Password',
            'database' => 'database',
            'prefix' => '',
            'encoding' => 'utf8',//this line allows the database to read utf8 data.
    );

}

答案 1 :(得分:0)

我认为这种关系可能会引发问题。在已完成的函数中尝试此操作,并pr($foo)结果:

$foo = $this->Topic->find('all', array(
    'fields' => array(
        'Topic.creator',
        'Topic.link',
        'Topic.id', 
        'Topic.topic_name', 
        'Topic.info', 
        'Topic.priority', 
        'Topic.user_id',
        'Topic.completed',
        'Topic.created', 
        'Topic.status',
        'User.name',
        'User.id',
        'Topic.category',
        'Topic.tags'
    ),
    'conditions' => array(
        'Topic.status' => 4,
    ),
    'joins' => array(
        array(
            'table' => 'users',
            'alias' => 'User',
            'type' => 'LEFT',
            'conditions' => array(
                'Topic.user_id = User.id'
            ),
        ),
    ),
    'recursive' => -1,
));

我怀疑你会得到你想要的结果。