Select only some fields to search in CAKEPHP

My problem is that when I retrieve the user data from the users table, all the fields of the user table are extracted from the users.But table, I do not want to include the password and email address in it, so is there any way to get only a selection of fields other than password and email addresses?

Sincerely.

+4
source share
4 answers

As juhana mentioned, you do not need to use all the fields returned by your search call. It is not clear what actual problem you are trying to solve, and it would be in your interest to clarify such details in future issues.

For direct requests

For queries directly in your user model, you can use some logic as follows:

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); } 

but

This will not do anything for queries where you request another model, for example.

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

In the above case, all user fields will be returned. For such purposes, it’s best to explicitly define your list of fields, rather than relying on any automatic magic:

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

you need to do something like this

 $this->Model->find('all',array('fields'=>array('id','name'))); 

Just specify the required fields in the field array.

if you want to extract from the model try this

 $this->find('all',array('fields'=>array('id','name'))); 

Thanks.

+2
source
 $this->User->find('all',array('fields'=>array('User.id','User.name','User.created', etc etc))); 

You may notice that I use Model.field in my field declarations, just in case you have conflicting field names in any related models.

Taken from the CakePHP manual found here , all of the options listed below are available to you 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' ) 
0
source

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 not part of CakePHP; this is a common property that I came up with. If for any reason you need to provide a password and email address, just add $this->User->includeAllFields = true; before searching.

The only drawback of this approach that I can come up with is that if the field is always added or removed from the database, it will be necessary to update the array of fields in the above code.

0
source

All Articles