在CAKEPHP中仅获取找到的某些字段 [英] Fetch only some fields on find in CAKEPHP

查看:86
本文介绍了在CAKEPHP中仅获取找到的某些字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,当我从用户表中获取用户数据时,用户表的所有字段都从用户表中获取..但是我不想在其中包含密码和电子邮件地址,因此有什么方法只能获取

My problem is when I fetch data of user from users table , all the fields of user table fetched from users table..but i don't want to include password and email address into that so is there any way to only fetch fields other than password and email address?

问候。

推荐答案

juhana 所述,您不需要使用查找呼叫返回的所有字段。目前尚不清楚您要解决的实际问题,在将来的问题中澄清这些细节是否符合您的利益。

As mentioned by juhana you don't need to use all fields that are returned by your find call. It's not clear what actual problem you're trying to solve and it would be in your interest to clarify such details in future questions.

对于直接在用户模型上进行查询,您可以使用如下逻辑:

For queries directly on your user model you can use some logic like this:

public function beforeFind($queryData) {
    if (empty($queryData['fields'])) {
        $schema = $this->schema();
        unset($schema['password']);
        unset($schema['email']);

        foreach (array_keys($schema) as $field) {
            $queryData['fields'][] = $this->alias . '.' . $field;
        }
        return $queryData;
    }

    return parent::beforeFind($queryData);

}



但是



对于查询其他模型的查询,它不会做任何事情,例如

However

This won't do anything for queries where you query another model e.g.

$results = $PostModel->find('all', array(
    'contain' => array('User')
));

在上述情况下,将返回所有用户字段。对于这样的用途,最好是显式定义字段列表,而不要依赖于任何自动魔术:

In the above case, all user fields will be returned. For uses like this it's probably best to define your field list explicitly rather than rely on any automatic magic:

$results = $PostModel->find('all', array(
    'contain' => array('User')
    'fields' => array('Post.*', 'User.name')
));

这篇关于在CAKEPHP中仅获取找到的某些字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆