简体   繁体   中英

Warning (2): Illegal offset type [CORE\Cake\Model\Model.php, line 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

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
    )
));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM