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
        )
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路