CakePHP: Can I ignore a field when reading a model from a database?

In one of my models, I have a β€œLONGTEXT” field, in which there is a large dump of many things that I have never read, and this slows down as I move much more data between the database and the web application.

Is there a way to indicate in the model that I want CakePHP to simply ignore this field and never read it or do anything with it?

I really want to avoid the hassle of creating a separate table and a separate model for this field only.

Thanks!
Daniel

+4
source share
4 answers

As @SpawnCxy said, you need to use the 'fields' => array(...) option in find to restrict the data you want to get. If you don't want to do this every time you write find , you can add something like this to your beforeFind() model beforeFind() , which will automatically fill the field parameters with all fields except the longtext field:

 function beforeFind($query) { if (!isset($query['fields'])) { foreach ($this->_schema as $field => $foo) { if ($field == 'longtextfield') { continue; } $query['fields'][] = $this->alias . '.' . $field; } } return $query; } 

Regarding the comment:

That's true ... The easiest way in this case is probably to remove the field from the schema.

 unset($this->Model->_schema['longtextfield']); 

I have not tested it, but this should prevent the inclusion of the field in the request. If you want to make it switchable for each request, you can transfer it to another variable of type $Model->_schemaInactiveFields and move it if necessary. You can even do Behavior for this.

+8
source

The fields parameter can help you. It does not ignore the fields, but indicates the required fields:

 array( 'conditions' => array('Model.field' => $thisValue), //array of conditions 'fields' => array('Model.field1', 'Model.field2'), //list columns you want ) 

You can get more information about retrieving data from a cookbook .

Another idea :

Define your special request in the model:

 function myfind($type,$params) { $params['fields'] = array('Model.field1','Model.field2',...); return $this->find($type,$params); } 

Then use it in the controller

 $this->Model->myfind($type,$params); 
+4
source

Also, try inline behavior to remove all unnecessary fields and work with model associations. Content

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

Where is your model located?

+1
source

You can add the beforeFilter function to your table and add a selection to the query

Excample:

 public function beforeFind(Event $event, Query $query){ $protected = $this->newEntity()->hidden; $tableSchema = $event->subject()->schema(); $fields = $tableSchema->columns(); foreach($fields as $key => $name){ if(in_array($name,$protected)){ unset($fields[$key]); } } $query->select($fields); return $event; 

}

In this example, I took hidden fields from ModelClass to exclude from the result.

Took this from my answer to a simulated question: Hidden fields are still listed from the database in cakephp 3

0
source

Source: https://habr.com/ru/post/1311804/


All Articles