警告(2):非法偏移类型[CORE \ Cake \ Model \ Model.php,第2934行]

时间:2014-05-20 16:34:32

标签: php cakephp

我正在学习CakePHP,我正在设计一个用户登录/注册系统。我得到了错误:

Warning (2): Illegal offset type [CORE\Cake\Model\Model.php, line 2934]

Warning (2): Illegal offset type [CORE\Cake\Model\Model.php, line 2912]

当我从localhost向用户发送激活电子邮件时 - 使用Xampp。

UsersController.php

 public function register() {
        if ($this->request->is('post')) {

            $this->User->create();

            if ($this->User->save($this->request->data)) {
                $this->__sendActivationEmail($this->User->getLastInsertID());
                $this->Session->setFlash(__('The user has been created. Check your email for an activation link.'), 'alert', array('class' => 'alert-success'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be created. Please, try again.'), 'alert', array(
                    'class' => 'alert-danger'
                ));
            }
        }
    }

    function __sendActivationEmail($user_id) {
        App::uses('CakeEmail', 'Network/Email');
        $user = $this->User->find(array('User.id' => $user_id), array('User.email', 'User.username', 'User.id'), null, false);
        if ($user === false) {
            debug(__METHOD__ . " failed to retrieve User data for user.id: {$user_id}");
            return false;
        }

        // Set data for the "view" of the Email
        $activate_url = 'http://' . env('SERVER_NAME') . '/users/activate/' . $user['User']['id'] . '/' . $this->User->getActivationHash();
        $name = $this->data['User']['username'];

        $email = new CakeEmail('gmail');
        $email->from('blabla@gmail.com');
        $email->to($this->data['User']['email']);
        $email->subject(env('SERVER_NAME') . ' Please confirm your email address');
        $email->template('user_confirm');
        $email->emailFormat('text');
        $email->viewVars(array('activate_url' => $activate_url, 'name' => $name));
        return $email->send();
    }

user_confirm.ctp

Hey there <?= $username ?>, we will have you up and running in no time, but first we just need you to confirm your user account by clicking the link below:
<?= $activate_url ?>

user.php的

 function getActivationHash() {
        if (!isset($this->id)) {
            return false;
        }
        return substr(Security::hash(Configure::read('Security.salt') . $this->field('created') . date('Ymd')), 0, 8);
    }

我看过this question,但我仍然无法弄清问题是什么。我该如何解决?

Model.php

第2912行

if ($this->findMethods[$type] === true) {
            return $this->{'_find' . ucfirst($type)}('after', $query, $results);
        }
    }

第2934行

if ($this->findMethods[$type] === true) {
            $query = $this->{'_find' . ucfirst($type)}('before', $query);
        }

NB:CakePHP v2.4.9

1 个答案:

答案 0 :(得分:1)

你正在使用Cake 1.3风格的Model::find()来电,这是行不通的。 Cake 2.x对传递的参数更严格,只有两种,类型和参数:

find(string $type = 'first', array $params = array())

http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find

所以2.x等价物看起来像这样:

$user = $this->User->find('first', array(
    'recursive' => -1,
    'fields' => array(
        'User.email', 'User.username', 'User.id'
    ),
    'conditions' => array(
        'User.id' => $user_id
    )
));