插件模型关联不工作在cakephp 2.3与cake ACL


Plugin model assiciation is not working in cakephp 2.3 with cake ACL

我只是想在一个名为"Cauth"的插件中实现Cakephp ACL用户身份验证。我之前做的同样的事情工作得很好。这次的不同之处在于,它在插件下。这里,在控制器中,$ this ->用户->组->找到("列表");不起作用。我得到了以下致命错误:

Fatal Error
Error: Call to a member function find() on a non-object
File: my_dir_path'app'Plugin'Cauth'Controller'UsersController.php
Line: 60
Notice: If you want to customize this error message, create app'View'Errors'fatal_error.ctp
我的代码如下:

组模型:

var $useTable = 'groups';
public $hasMany = array (
    'User' => array (
        'className'    => 'Cauth.User',
        'foreignKey'   => 'group_id',
        'dependent'    => false,
        'conditions'   => '',
        'fields'       => '',
        'order'        => '',
        'limit'        => '',
        'offset'       => '',
        'exclusive'    => '',
        'finderQuery'  => '',
        'counterQuery' => ''
    )
);
用户模式:

var $useTable = 'users';
public $belongsTo = array (
    'Group' => array (
        'className'  => 'Cauth.Group',
        'foreignKey' => 'group_id',
        'conditions' => '',
        'fields'     => '',
        'order'      => ''
    )
);

用户控制器添加动作,组选择框不工作。

Users controller add action:

App::uses('CauthAppController', 'Cauth.Controller');
class UsersController extends CauthAppController {
public function add() {
    if ($this->request->is('post')) {
        $this->User->create();
        if ($this->User->save($this->request->data)) {
            $this->Session->setFlash(__('The user has been saved'));
            $this->redirect(array ('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    }
    $groups = $this->User->Group->find('list');
    $this->set(compact('groups'));
}

有谁能帮我解决这个问题吗?

特别注意:它正在处理以下情况。

案例1:

如果我从控制器绑定模型,它会工作得很好。

$this->User->bindModel(
    array ('belongsTo' => array ('Group'))
);

public function add() {
    $this->User->bindModel(
        array ('belongsTo' => array ('Group'))
    );
    if ($this->request->is('post')) {
        $this->User->create();
        if ($this->User->save($this->request->data)) {
            $this->Session->setFlash(__('The user has been saved'));
            $this->redirect(array ('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    }
    $groups = $this->User->Group->find('list');
    $this->set(compact('groups'));
}

案例2:

如果我允许所有的操作名称。Admin拥有所有权限,虽然我也需要为Admin编写beforeFilter。

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('index', 'add');
}

我又找到了一个可行的案例。

案例3:

AppController的代码是-

class AppController extends Controller {

    public $helpers    = array ('Form', 'Time', 'Html', 'Session', 'Js', 'DebugKit.Toolbar');
    public $counter    = 0;
    public $components = array (
        'DebugKit.Toolbar',
        'RequestHandler',
        'Acl',
        'Auth' => array (
            'authorize' => array (
                'Actions' => array ('actionPath' => 'controllers')
            )
        ),
        'Session'
    );
    public function beforeFilter() {
        //Configure AuthComponent
        $this->Auth->loginAction    = array ('plugin'     => 'cauth', 'controller' => 'users', 'action'     => 'login');
        $this->Auth->logoutRedirect = array ('plugin'     => 'cauth', 'controller' => 'users', 'action'     => 'login');
        $this->Auth->loginRedirect  = array ('plugin'     => '', 'controller' => 'pages', 'action'     => 'display');
    }
}

在这种情况下,它不起作用。但是当我把它-

class AppController extends Controller {
    public $helpers    = array ('Form', 'Time', 'Html', 'Session', 'Js', 'DebugKit.Toolbar');
    public $counter    = 0;
    public $components = array (
        'DebugKit.Toolbar',
        'RequestHandler',
        'Acl',
        'Auth' => array (
            'authenticate' => array('Form')
        ),
        'Session'
    );
    public function beforeFilter() {
        //Configure AuthComponent
        $this->Auth->loginAction    = array ('plugin'     => 'cauth', 'controller' => 'users', 'action'     => 'login');
        $this->Auth->logoutRedirect = array ('plugin'     => 'cauth', 'controller' => 'users', 'action'     => 'login');
        $this->Auth->loginRedirect  = array ('plugin'     => '', 'controller' => 'pages', 'action'     => 'display');
    }
}

然后关联再次开始工作。还是不明白这个组件声明有什么问题

public $components = array (
    'DebugKit.Toolbar',
    'RequestHandler',
    'Acl',
    'Auth' => array (
        'authorize'    => array (
            'Actions' => array ('actionPath' => 'controllers')
        )
    ),
    'Session'
);

如果我不这样声明,我的ACL不工作。

请帮帮我。我已经写了很长时间了

经过长时间的努力,我终于找到了解决方案-

https://cakephp.lighthouseapp.com/projects/42648/tickets/2464-plugin-using-acl-not-loading-proper-model

当我使用Auth组件时,用户控制器没有使用Auth。用户模型。它是用AppModel加载的。但没有auth组件,它工作正常。所以正确使用auth组件的方法是

    'Auth' => array (
        'authorize' => array (
            'Actions' => array (
                'actionPath' => 'controllers',
                'userModel'  => 'Cauth.User',
            ),
        )
    ),

也就是AppController的总长度-

class AppController extends Controller {
    public $helpers    = array ('Form', 'Time', 'Html', 'Session', 'Js', 'DebugKit.Toolbar');
    public $counter    = 0;
    public $components = array (
        'DebugKit.Toolbar',
        'RequestHandler',
        'Acl',
        'Auth' => array (
            'authorize' => array (
                'Actions' => array (
                    'actionPath' => 'controllers',
                    'userModel'  => 'Cauth.User',
                ),
            )
        ),
        'Session'
    );
    public function beforeFilter() {
        //Configure AuthComponent
        $this->Auth->loginAction    = array ('plugin'     => 'cauth', 'controller' => 'users', 'action'     => 'login');
        $this->Auth->logoutRedirect = array ('plugin'     => 'cauth', 'controller' => 'users', 'action'     => 'login');
        $this->Auth->loginRedirect  = array ('plugin'     => '', 'controller' => 'pages', 'action'     => 'display');
    }
}

现在一切都很好。谢谢大家。

假设Group是一个模型(表)的名称,将这一行修改为:

  $groups = $this->User->Group->find('list');

  $groups = $this->Group->find('list');

希望对你有帮助!

尝试两种方法来解决这个问题。首先将以下内容添加到你的Users和Groups插件控制器中。

public $uses = array('Users.User', 'Users.Group');

第二,定义插件控制器中的组件。在cake ACL Auth教程中,他们把它放在主AppController中,你需要把它移到插件中。

你可以在UsersAppController或UsersController

中执行以下操作
public $components = array(
        'Acl',
        'Auth',
        'Session'
    );