在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')
));