问题
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?
regards.
回答1:
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 direct queries
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')
));
回答2:
you have to do something like this
$this->Model->find('all',array('fields'=>array('id','name')));
Just mention your require fields on fields array.
if you want to fetch from model then try this
$this->find('all',array('fields'=>array('id','name')));
Thanks.
回答3:
$this->User->find('all',array('fields'=>array('User.id','User.name','User.created', etc etc)));
You may notice I use Model.field in my field declarations, this is just in case you have any conflicting field names in any related models.
Taken from the CakePHP manual found here, all of these below options are available to you when using the Find method on Models....
array(
'conditions' => array('Model.field' => $thisValue), //array of conditions
'recursive' => 1, //int
'fields' => array('Model.field1', 'DISTINCT Model.field2'), //array of field names
'order' => array('Model.created', 'Model.field3 DESC'), //string or array defining order
'group' => array('Model.field'), //fields to GROUP BY
'limit' => n, //int
'page' => n, //int
'offset' => n, //int
'callbacks' => true //other possible values are false, 'before', 'after'
)
回答4:
Add this to your User
model:
public function beforeFind($queryData) {
if (isset($this->includeAllFields)) {
return $queryData;
}
if (empty($queryData['fields'])) {
$queryData['fields'] = array(
// Specify all fields EXCEPT for "password" and "email".
'User.field1',
'User.field2',
'User.field3'
);
}
return $queryData;
}
$this->includeAllFields
is not part of CakePHP; it's a custom property that I made up. If, for whatever reason, you need to include the password and e-mail address, just add $this->User->includeAllFields = true;
before the find.
The only downside to this approach that I can think of is that if a field is ever added to or removed from the database, the array of fields in the above code will need to be updated.
来源:https://stackoverflow.com/questions/14229470/fetch-only-some-fields-on-find-in-cakephp