与蛋糕php相关

时间:2012-08-21 05:23:40

标签: cakephp cakephp-2.1

我有以下警告

  

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

     

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

型号代码如下:

`<?
class Register extends AppModel
{
    var $name = 'Register';
    var $useTable = 'registers';
function validateLogin($data)
    {
        $user = $this->find(array('username' => $data['username'], 'password' => $data['password']), array('id', 'username'));
        if(empty($user) == false)
            return $user['Register'];
        return false;
    } 
public $validate = array(
        'first_name' => array(
        'rule' => 'notEmpty',
    'required' => true ,
    'message' => 'this field can not be set empty'
    ),
        'username' => array(
        'rule' => 'notEmpty'
        ),
    'password'=>array(
    'rule'=>'notempty'
    ),
    'cnfrmpassword'=>array(
    'rule'=>'notempty'
    )
    );
}
?>`

视图代码是 的 reg.ctp

`<h2>Registration Form</h2>
<?php echo $this->Html->link('Register',array( 'action' => 'login')); 

echo $this->form->create('Register', array('action' => 'register'));
echo $this->form->input('first_name');
echo $this->form->input('username');
echo $this->form->input('password');
echo $this->form->input('cnfrmpassword', array('type' => 'password'));
echo $this->form->submit();
echo $this->form->end();
?>`

login.ctp

`<h1>Sign in form</h1>
    <?php
    echo $this->form->create('Register',array('action'=>'login'));
    echo $this->form->input('username');
    echo $this->form->input('password');
    echo $this->form->end('Sign in');
    ?>

<?php echo $this->Html->link('Logout', array('controller' => 'Registers', 'action' => 'logout')); ?>
<br/><br/>`

控制器代码

`<?
class RegistersController extends AppController
{
var $name = 'Register';
 var $helpers = array('Html', 'Form');
public function reg() {
      if (!empty($this->data))
         {

              $this->Register->create();
             // $this->Register->save($this->data);
              $this->redirect(array('action' => 'reg'));

      }
  }

public function register() {
        if ($this->request->is('post')) {
            if ($this->Register->save($this->request->data)) {
                $this->Session->setFlash('Your are registered');
                $this->redirect(array('action' => 'reg'));
            } else {
                $this->Session->setFlash('Unable to register');
        $this->redirect(array('action' => 'reg'));
            }
        }
    }
 function beforeFilter()
    {
        $this->__validateLoginStatus();
    } 
function login()
    {
        if(empty($this->data) == false)
        {
            if(($user = $this->Register->validateLogin($this->data['Register'])) == true)
            {
                $this->Session->write('Register', $user);
                $this->Session->setFlash('You\'ve successfully logged in.');
                $this->redirect('reg');
                exit();
            }
            else
            {
                $this->Session->setFlash('Sorry, the information you\'ve entered is incorrect.');
                exit();
            }
        }
    }

    function logout()
    {
        $this->Session->destroy('Register');
        $this->Session->setFlash('You\'ve successfully logged out.');
        $this->redirect('reg');
    } 


function __validateLoginStatus()
    {
        if($this->action != 'login' && $this->action != 'logout')
        {
            if($this->Session->check('User') == false)
            {
                $this->redirect('login');
                $this->Session->setFlash('The URL you\'ve followed requires you login.');
            }
        }
    }

}
?>`

您已访问秘密安全位置! `

3 个答案:

答案 0 :(得分:0)

更改此行:

<强> $user = $this->find(array('username' => $data['username'], 'password' => $data['password']), array('id', 'username'))

要:

<强> $user = $this->find(array(conditions => array('username' => $data['username'], 'password' => $data['password'])), array('id', 'username'))

希望这有帮助

答案 1 :(得分:0)

抱歉小错误。

这是经过调整的答案。

更改此行:

$user = $this->find(array('username' => $data['username'], 'password' => $data['password']), array('id', 'username'))

要:

$user = $this->find('first', array('conditions' => array('username' => $data['username'], 'password' => $data['password'])), array('id', 'username'))

希望这有帮助

答案 2 :(得分:0)

您的代码中存在多个错误:

我已经更正了控制器的登录功能:

    <?php 
    class RegistersController extends AppController
    {
    var $name = 'Register';
    var $helpers = array('Html', 'Form');
    public function reg() {
          if (!empty($this->data)){
                  $this->Register->create();
                 $this->redirect(array('action' => 'reg'));
          }
      }
    public function register() {
            if ($this->request->is('post')) {
                if ($this->Register->save($this->request->data)) {
                    $this->Session->setFlash('Your are registered');
                    $this->redirect(array('action' => 'reg'));
                } else {
                    $this->Session->setFlash('Unable to register');
            $this->redirect(array('action' => 'reg'));
                }
            }
        }
     function beforeFilter(){
            $this->__validateLoginStatus();
        } 
     function login(){
            if(!empty($this->data))
            {
                if($user = $this->Register->validateLogin($this->data['Register']))
                {
                    $this->Session->write('Register', $user);
                    $this->Session->setFlash('You\'ve successfully logged in.');
                    $this->redirect('reg');
                    exit();
                }
                else
                {
                    $this->Session->setFlash('Sorry, the information you\'ve entered is incorrect.');
                    exit();
                }
            }
        }
     function logout(){
            $this->Session->destroy('Register');
            $this->Session->setFlash('You\'ve successfully logged out.');
            $this->redirect('reg');
        }
     function __validateLoginStatus(){
            if($this->action != 'login' && $this->action != 'logout')
            {
                if($this->Session->check('User') == false)
                {
                    $this->redirect('login');
                    $this->Session->setFlash('The URL you\'ve followed requires you login.');
                }
            }
        }
   }
?>

型号代码:

<?
class Register extends AppModel
{
    var $name = 'Register'; 

    public $validate = array(
        'first_name' => array(
        'rule' => 'notEmpty',
    'required' => true ,
    'message' => 'this field can not be set empty'
    ),
        'username' => array(
        'rule' => 'notEmpty'
        ),
    'password'=>array(
    'rule'=>'notempty'
    ),
    'cnfrmpassword'=>array(
    'rule'=>'notempty'
    )
    );
    function validateLogin($data)
    {
        $user = $this->find('first',array(
                     'conditions'=> array('username' => $data['username'], 
                                          'password' => $data['password']),
                     'fields'=>array('id', 'username')));
        if(!empty($user))
            return $user['Register'];
        return false;
    }
}
?>

请参阅$ useTable不是必需的,因为您遵循蛋糕命名约定,您的查找语句不正确。