【问题标题】:CakeDc User $hasMany not returning Associated modelCakeDc 用户 $hasMany 未返回关联模型
【发布时间】:2014-08-04 21:02:36
【问题描述】:

我正在使用 CakeDc 用户插件 2.0,在插件 admin_index 视图中,我想查看与用户相关的书籍。

我已将 $hasMany 添加到 User.php 模型中

public $hasMany = array(
    'Book' => array(
        'className' => 'Book',
        'foreignKey' => 'user_id',
        'dependent' => true,
        'conditions' => '',
        'fields' => '',
        'order' => 'order',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);

在 UserController.php 中我添加了“包含”

protected function _setupAdminPagination() {
    $this->Paginator->settings = array(
        'limit' => 20,
        'order' => array(
            $this->modelClass . '.created' => 'desc'),
        'contain' => array('Book')
    );
}

public function admin_index() {
    $this->Prg->commonProcess();
    unset($this->{$this->modelClass}->validate['username']);
    unset($this->{$this->modelClass}->validate['email']);
    $this->{$this->modelClass}->data[$this->modelClass] = $this->passedArgs;

    if ($this->{$this->modelClass}->Behaviors->loaded('Searchable')) {
        $parsedConditions = $this->{$this->modelClass}->parseCriteria($this->passedArgs);
    } else {
        $parsedConditions = array();
    }
    $this->_setupAdminPagination();
    $this->Paginator->settings[$this->modelClass]['conditions'] = $parsedConditions;
    $this->set('usersList', $this->Paginator->paginate());
    $this->layout = 'btst';
}

但是我没有在视图中获得相关的书籍,我仍然只是用户数组。这应该可以吗?

更新:

debug ($this->Paginator->settings);

输出

array(
    'limit' => (int) 20,
    'order' => array(
         'User.created' => 'desc'
    ),
    'contain' => array(
         (int) 0 => 'Book'
    ),
    'User' => array(
         'conditions' => array()
    )
)

【问题讨论】:

    标签: cakephp cakephp-2.5


    【解决方案1】:

    在调用 $this->Paginator 之前调试 $this->Paginator->settings 并尝试

    'contain' => array('Book')
    

    但是直接修改插件是不好的做法,在最坏的情况下,您将无法再顺利更新它们(合并冲突、API 更改......)。插件文档附带了很多文档,解释了如何扩展插件而不是直接修改它。

    编辑:这实际上是插件中的一个错误,已在https://github.com/cakedc/users/commit/73aa350修复它

    【讨论】:

    • 是的,我已经扩展了模型和控制器。文档对此有很好的解释。但是由于我没有得到想要的结果,我认为直接更改代码可能会显示我哪里出错了。我已经更新了我的代码,但我仍然只得到用户数组。我完全打算回到扩展文件。谢谢
    • 查看分页器数组中的“用户”键。在调用 paginate() 之前取消设置以进行测试,我认为这可能是插件中的错误。我猜会发生什么是使用“用户”数组的设置,除了空条件之外什么都没有。
    • 当我尝试 unset($this->Paginator->settings['User']); 时出现数据库错误- 未找到列:1054“订单子句”中的未知列“订单”
    • 在 _setupAdminPagination() 中将 settings = array( 更改为 settings = array('User' => array(。我想你知道我的意思。然后再试一次。
    • 我在插件本身中修复了这个问题。您无需进行此更改。查看最新版本的 changelog.md。已在github.com/cakedc/users/commit/73aa350 中修复
    猜你喜欢
    • 1970-01-01
    • 2011-10-19
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多