问题描述
我使用CakeDC-Users插件。
I'm using CakeDC-Users plugin.
<?php
class Post extends AppModel {
public $useTable='posts';
public $belongsTo = array('User');
public $hasMany=array('Comment');
}
我不得不使用paginate:
I had to use paginate:
$ allposts = $ this-> paginate('Post');
以这种方式获取user_id:
I can get the user_id in this way:
foreach ($allposts as $post) {
debug($post['Post']['user_id']);
但我需要用户名而不是user_id。如何获取用户名?
But i need the username not the user_id. How can i get username?
推荐答案
CakPHP的containble功能默认隐藏所有关联模型:
The containble feature of CakPHP hides all associated models by default: http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
如果您只想添加一个关联模型的一个字段,可以使用以下语法:
If you want to add only one field of an associated Model you can use this syntax:
$allposts = $this->Post->find('all', array('contain' => 'User.username'));
或者使用paginate在你的控制器类中使用this:(http://book.cakephp.org/ 1.3 / en / view / 1232 / Controller-Setup)
or with paginate use this in your controller class: (http://book.cakephp.org/1.3/en/view/1232/Controller-Setup)
var $paginate = array('contain' => 'User.username');
尝试以下方式访问它:
$post['User']['username'];
这篇关于CakePHP-2.0如何从$ allposts = $ this-> paginate('Post')获取用户名;其中user_id是posts表的外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!