开发者

Using Set::extract to filter part of a dataset

I have a query to find Homes from a database, along with some extra data from related tables, as follows:

$homes_data = $this->Home->find('all',array(
    'conditions'=>$conditions,
    'order'=>array('Home.mls_status ASC','Home.num DESC'),
    'contain'=>array('Realtor.show_price','Realtor.show_address','HighlightImage.num')
))

At开发者_如何转开发 this point, I'd like to be able to use Set::extract to filter my resultset to remove any results where the MLS number is not valid, which in this case means is not exactly 6 numeric digits:

$homes_data = Set::extract('/Home/[mls=/^[[:digit:]]{6}$/]', $homes_data);

The problem with this is that it's losing the Realtor and HighlightImage results of the array. So:

Is there a simple way of recasting this, to keep the associated data for homes with a valid MLS?

Failing this, is there a way to work the condition "must be numeric and exactly 6 digits in length" into the SQL query, preferably while keeping it within the realms of tidy, idiomatic CakePHP?


thesunneversets,

if validation during data entry is not an option, maybe writing a customized model function is indicated to keep your controller thin. You should be able to set up the validation rules for the model and use validation manually in this customized function.

Edit0: Another approach could be to map the existing data to your favorite set of values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜