检索模型而不获取关联模型-CakePHP

检索模型而不获取关联模型-CakePHP,php,cakephp,Php,Cakephp,我使用find('all')函数从数据库中检索post记录,但这也将返回与具有belongsTo-hasMany关系的post模型关联的所有用户信息 这样做的缺点是用户模型包含密码和其他重要信息。这被认为是安全问题吗?我没有在视图上显示信息 谢谢 编辑: 我修改了我的代码,但我仍然得到相关的模型 $this->set('posts_list',$this->Post->find('all',array('contain' => false, 'order

我使用find('all')函数从数据库中检索post记录,但这也将返回与具有belongsTo-hasMany关系的post模型关联的所有用户信息

这样做的缺点是用户模型包含密码和其他重要信息。这被认为是安全问题吗?我没有在视图上显示信息

谢谢


编辑:

我修改了我的代码,但我仍然得到相关的模型

        $this->set('posts_list',$this->Post->find('all',array('contain' => false, 'order' => array('Post.price ASC'))));
有什么想法吗?

你用这个吗:

$this->Post->find('all')// If u access it from Post controller
或者

不一定

但是,当您不需要信息时,您正在检索信息。现在这不是一个问题,但请记住,当您有大量相关数据时,这将成为一个巨大的问题

考虑将
递归
属性设置为
-1
(如果需要,也可以设置为0)

这将仅从选定模型中提取数据

或者,对于更精细的选择,您可以使用可包含的行为:


这允许您选择在检索数据时保留哪些关联

您有几种选择。您可以在模型上设置
递归
属性:

$this->Post->recursive = -1;
$posts = $this->Post->find('all');
或者,您可以指定
递归
作为搜索选项:

$posts = $this->Post->find('all', array(
    'recursive' => -1,
    'conditions' => ...
);
您还可以在Post模型中使用
Containable
行为。在这种情况下,可以指定一个空集:

class Post extends AppModel {
    var $actsAs = array('Containable');
}

$this->Post->contain();
$posts = $this->Post->find('all');
或者,在查询中指定:

$posts = $this->Post->find('all', array(
    'contain' => false,
);
Containable
行为的好处在于,当您稍后将其他模型与您的帖子关联时。假设您实现了一个标记模型。现在,您想查找带有标签的帖子,但不想查找使用模型:

$posts = $this->Post->find('all', array(
    'contain' => array('Tag'),
);
只是想让你知道

$this->Model->recursive = -1 will remove all associations
$this->Model->recursive = 0 will remove only hasMany assosiation (so it keeps belongsTo)

谢谢你的意见。我尝试了'contain'=>false方法,但出于某种原因,它仍然返回关联的模型。查看“我的代码”的编辑您是否在模型中包含了
可包含的
行为?见我的编辑上面。我错过了。。。谢谢!出色的工作节省了我的时间。谢谢,这是一个完美的方法。
$posts = $this->Post->find('all', array(
    'contain' => array('Tag'),
);
$this->Model->recursive = -1 will remove all associations
$this->Model->recursive = 0 will remove only hasMany assosiation (so it keeps belongsTo)