Tim Joyce
Tim Joyce

Reputation: 4517

Bind a condition to a model in cakephp

<?php
   class User extends AppModel {
var $name = 'User';
var $displayField = 'fname';

}

How can I only return users from this model that have a "standing" of "1"? I am not looking to do this from the controller but, from the model.

[Solution] In model

function beforeFind($queryData){
    $queryData['conditions']['standing'] = 1;
    return $queryData;
}

Upvotes: 0

Views: 738

Answers (1)

JohnP
JohnP

Reputation: 50039

The easiest way to do this would be to put in some filtering conditions in your beforeFind callback. Modifying the $queryData variable and adding your restriction to the conditions key should do it.

From the manual entry - http://book.cakephp.org/1.3/en/view/1049/beforeFind

Called before any find-related operation. The $queryData passed to this callback contains information about the current query: conditions, fields, etc.

If you do not wish the find operation to begin (possibly based on a decision relating to the $queryData options), return false. Otherwise, return the possibly modified $queryData, or anything you want to get passed to find and its counterparts.

You might use this callback to restrict find operations based on a user’s role, or make caching decisions based on the current load.

Upvotes: 2

Related Questions