FOF30\Model\DataModel::where PHP Method

where() public method

Automatically uses the Filters behaviour to filter records in the model based on your criteria.
public where ( string $fieldName, string $method = '=', mixed $values = null )
$fieldName string The field name to filter on
$method string The filtering method, e.g. <>, =, != and so on
$values mixed The value you're filtering on. Some filters (e.g. interval or between) require an array of values
    public function where($fieldName, $method = '=', $values = null)
    {
        // Make sure the Filters behaviour is added to the model
        if (!$this->behavioursDispatcher->hasObserverClass('FOF30\\Model\\DataModel\\Behaviour\\Filters')) {
            $this->addBehaviour('filters');
        }
        // If we are dealing with the primary key, let's set the field name to "id". This is a convention and it will
        // be used inside the Filters behaviour
        // -- Let's not do this. The Filters behaviour works just fine with the regular field name!
        /**
        		if ($fieldName == $this->getIdFieldName())
        		{
        			$fieldName = 'id';
        		}
        		**/
        $options = array('method' => $method, 'value' => $values);
        // Handle method aliases
        switch ($method) {
            case '<>':
                $options['method'] = 'search';
                $options['operator'] = '!=';
                break;
            case 'lt':
                $options['method'] = 'search';
                $options['operator'] = '<';
                break;
            case 'le':
                $options['method'] = 'search';
                $options['operator'] = '<=';
                break;
            case 'gt':
                $options['method'] = 'search';
                $options['operator'] = '>';
                break;
            case 'ge':
                $options['method'] = 'search';
                $options['operator'] = '>=';
                break;
            case 'eq':
                $options['method'] = 'search';
                $options['operator'] = '=';
                break;
            case 'neq':
            case 'ne':
                $options['method'] = 'search';
                $options['operator'] = '!=';
                break;
            case '<':
            case '!<':
            case '<=':
            case '!<=':
            case '>':
            case '!>':
            case '>=':
            case '!>=':
            case '!=':
            case '=':
                $options['method'] = 'search';
                $options['operator'] = $method;
                break;
            case 'like':
            case '~':
            case '%':
                $options['method'] = 'partial';
                break;
            case '==':
            case '=[]':
            case '=()':
            case 'in':
                $options['method'] = 'exact';
                break;
            case '()':
            case '[]':
            case '[)':
            case '(]':
                $options['method'] = 'between';
                break;
            case ')(':
            case ')[':
            case '](':
            case '][':
                $options['method'] = 'outside';
                break;
            case '*=':
            case 'every':
                $options['method'] = 'interval';
                break;
            case '?=':
                $options['method'] = 'search';
                break;
            default:
                throw new InvalidSearchMethod('Method ' . $method . ' is unsupported');
                break;
        }
        // Handle real methods
        switch ($options['method']) {
            case 'between':
            case 'outside':
                if (is_array($values) && count($values) > 1) {
                    // Get the from and to values from the $values array
                    if (isset($values['from']) && isset($values['to'])) {
                        $options['from'] = $values['from'];
                        $options['to'] = $values['to'];
                    } else {
                        $options['from'] = array_shift($values);
                        $options['to'] = array_shift($values);
                    }
                    unset($options['value']);
                } else {
                    // $values is not a from/to array. Treat as = (between) or != (outside)
                    if (is_array($values)) {
                        $values = array_shift($values);
                    }
                    $options['operator'] = $options['method'] == 'between' ? '=' : '!=';
                    $options['value'] = $values;
                    $options['method'] = 'search';
                }
                break;
            case 'interval':
                if (is_array($values) && count($values) > 1) {
                    // Get the value and interval from the $values array
                    if (isset($values['value']) && isset($values['interval'])) {
                        $options['value'] = $values['value'];
                        $options['interval'] = $values['interval'];
                    } else {
                        $options['value'] = array_shift($values);
                        $options['interval'] = array_shift($values);
                    }
                } else {
                    // $values is not a value/interval array. Treat as =
                    if (is_array($values)) {
                        $values = array_shift($values);
                    }
                    $options['value'] = $values;
                    $options['method'] = 'search';
                    $options['operator'] = '=';
                }
                break;
            case 'search':
                // We don't have to do anything if the operator is already set
                if (isset($options['operator'])) {
                    break;
                }
                if (is_array($values) && count($values) > 1) {
                    // Get the operator and value from the $values array
                    if (isset($values['operator']) && isset($values['value'])) {
                        $options['operator'] = $values['operator'];
                        $options['value'] = $values['value'];
                    } else {
                        $options['operator'] = array_shift($values);
                        $options['value'] = array_shift($values);
                    }
                }
                break;
        }
        $this->setState($fieldName, $options);
        return $this;
    }

Usage Example

コード例 #1
0
ファイル: HasMany.php プロジェクト: Joal01/fof
 /**
  * Applies the relation filters to the foreign model when getData is called
  *
  * @param DataModel  $foreignModel   The foreign model you're operating on
  * @param DataModel\Collection $dataCollection If it's an eager loaded relation, the collection of loaded parent records
  *
  * @return boolean Return false to force an empty data collection
  */
 protected function filterForeignModel(DataModel $foreignModel, DataModel\Collection $dataCollection = null)
 {
     // Decide how to proceed, based on eager or lazy loading
     if (is_object($dataCollection)) {
         // Eager loaded relation
         if (!empty($dataCollection)) {
             // Get a list of local keys from the collection
             $values = array();
             /** @var $item DataModel */
             foreach ($dataCollection as $item) {
                 $v = $item->getFieldValue($this->localKey, null);
                 if (!is_null($v)) {
                     $values[] = $v;
                 }
             }
             // Keep only unique values
             $values = array_unique($values);
             // Apply the filter
             if (!empty($values)) {
                 $foreignModel->where($this->foreignKey, 'in', $values);
             } else {
                 return false;
             }
         } else {
             return false;
         }
     } else {
         // Lazy loaded relation; get the single local key
         $localKey = $this->parentModel->getFieldValue($this->localKey, null);
         if (is_null($localKey)) {
             return false;
         }
         $foreignModel->where($this->foreignKey, '==', $localKey);
     }
     return true;
 }
All Usage Examples Of FOF30\Model\DataModel::where