dtxq82489 2014-05-20 16:34
浏览 31
已采纳

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

I am learning CakePHP and I am designing a user login/registration system. I get the errors:

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

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

when I send an activation email to a user from localhost - using 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);
    }

I have looked at this question but I still can't figure out what the problem is. How do I solve it?

Model.php

line 2912

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

line 2934

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

NB: CakePHP v2.4.9

  • 写回答

1条回答 默认 最新

  • douke6857 2014-05-20 17:40
    关注

    You are using a Cake 1.3 styled Model::find() call, that won't work. Cake 2.x is more strict about the arguments being passed, and there are only two, the type and the parameters:

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

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

    So the 2.x equivalent would look something like this:

    $user = $this->User->find('first', array(
        'recursive' => -1,
        'fields' => array(
            'User.email', 'User.username', 'User.id'
        ),
        'conditions' => array(
            'User.id' => $user_id
        )
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗