views:

87

answers:

3

In one of my models, I have a "LONGTEXT" field that has a big dump of a bunch of stuff that I never care to read, and it slows things down, since I'm moving much more data between the DB and the web app.

Is there a way to specify in the model that I want CakePHP to simply ignore that 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, only for this field.

Thanks!
Daniel

+4  A: 

The parameter fields may help you.It doesn't ignore fields but specifies fields you want:

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

You can get more information of retrieving data in the cookbook .

Another idea:

Define your special query 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);
SpawnCxy
Do you know whether there is any way to set this list of fields as the "default" for the model every time it's read?Because i'm querying it in dozens of places, and i wouldn't want to have to specify the long list of fields in each and every place.Thanks!!
Daniel Magliola
+4  A: 

As @SpawnCxy said, you'll need to use the 'fields' => array(...) option in a find to limit the data you want to retrieve. If you don't want to do this every time you write a find, you can add something like this to your models beforeFind() callback, which will automatically populate the fields options 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 comment:

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

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

I haven't tested it, but this should prevent the field from being included in the query. If you want to make this switchable for each query, you could move it to another variable like $Model->_schemaInactiveFields and move it back when needed. You could even make a Behavior for this.

deceze
Sorry it took me so long to respond, I wasn't able to test this before. This is a great idea, and it's in fact the closest to what I had in mind. However, the problem I'm getting with it is that explicitly specifying all the fields from this model makes the fields from the "related" models not be queried. In other words, I lost the data I was bringing through "belongsTo" relationships. Any ideas on how to fix that part?Thanks!!
Daniel Magliola
@Daniel That's true… See updated answer for a different approach.
deceze
Awesome, the unset() solution worked perfectly. Thank you so much!!
Daniel Magliola
+1  A: 

Also try containable behaviour will strip out all unwanted fields and works on model associations as well. Containable

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

where Post is your model

openprojdevel