0

Whats the usual practice in getting a behavior (linked to multiple models) to build filters for SQL queries, and then read the table that belongs to that model?

I have a Behavior function which is meant to do a database query with certain SQL conditions. I currently pass in the $this->request->data.

I have issues building the SQL conditions because i'm not sure how to derive the name of the table (that corresponds to the model). See below for example, I want to change "BillingCenterDetail" which is the table name (and also the model name), to something generic I can use across different models. I want this table name to be derived automatically based on the model name. I'm not sure if i can use the $model reference for that.

public function saveWithTimeConstraintCheck(Model $model, $data) { //FIND ALL RECORDS THAT OVERLAP $overlapfilter = array( 'BillingCenterDetail.billing_center_id =' => $data['BillingCenterDetail']['billing_center_id'], 'BillingCenterDetail.startdate <=' => $data['BillingCenterDetail']['enddate'], 'BillingCenterDetail.enddate >=' => $data['BillingCenterDetail']['startdate'] ); 

... after building the filter, I can use $model->find to execute the query, this should be OK because its generic.

 $overlapresults = $model->find('all', array('conditions' => $overlapfilter)); 

1 Answer 1

0

I've answered my own question.

And actually to build filter conditions, I needed name of the model, not the name of the table, because the name of the table is a plural name with the "S" at the end.

I used $Model->name From: CakePHP: get current model name in a controller

For table names i found out u can also use $this->Model->table cakephp - get table names and its column details

Sign up to request clarification or add additional context in comments.

1 Comment

It is usually safer to use $Model->alias since this will respect the names assigned to models referenced through associations. When this isn't the case, alias is the same as name so it works just as well in the default case.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.