DboSource::conditions PHP Method

conditions() public method

Results of this method are stored in a memory cache. This improves performance, but because the method uses a hashing algorithm it can have collisions. Setting DboSource::$cacheMethods to false will disable the memory cache.
public conditions ( mixed $conditions, boolean $quoteValues = true, boolean $where = true, Model $Model = null ) : string
$conditions mixed Array or string of conditions, or any value.
$quoteValues boolean If true, values should be quoted
$where boolean If true, "WHERE " will be prepended to the return value
$Model Model A reference to the Model instance making the query
return string SQL fragment
    public function conditions($conditions, $quoteValues = true, $where = true, Model $Model = null)
    {
        $clause = $out = '';
        if ($where) {
            $clause = ' WHERE ';
        }
        if (is_array($conditions) && !empty($conditions)) {
            $out = $this->conditionKeysToString($conditions, $quoteValues, $Model);
            if (empty($out)) {
                return $clause . ' 1 = 1';
            }
            return $clause . implode(' AND ', $out);
        }
        if (is_bool($conditions)) {
            return $clause . (int) $conditions . ' = 1';
        }
        if (empty($conditions) || trim($conditions) === '') {
            return $clause . '1 = 1';
        }
        $clauses = '/^WHERE\\x20|^GROUP\\x20BY\\x20|^HAVING\\x20|^ORDER\\x20BY\\x20/i';
        if (preg_match($clauses, $conditions)) {
            $clause = '';
        }
        $conditions = $this->_quoteFields($conditions);
        return $clause . $conditions;
    }

Usage Example

 /**
  * test a full example of using virtual fields
  *
  * @return void
  */
 public function testVirtualFieldsFetch()
 {
     $this->loadFixtures('Article', 'Comment');
     $Article = ClassRegistry::init('Article');
     $Article->virtualFields = array('comment_count' => 'SELECT COUNT(*) FROM ' . $this->Dbo->fullTableName('comments') . ' WHERE Article.id = ' . $this->Dbo->fullTableName('comments') . '.article_id');
     $conditions = array('comment_count >' => 2);
     $query = 'SELECT ' . join(',', $this->Dbo->fields($Article, null, array('id', 'comment_count'))) . ' FROM ' . $this->Dbo->fullTableName($Article) . ' Article ' . $this->Dbo->conditions($conditions, true, true, $Article);
     $result = $this->Dbo->fetchAll($query);
     $expected = array(array('Article' => array('id' => 1, 'comment_count' => 4)));
     $this->assertEquals($expected, $result);
 }
All Usage Examples Of DboSource::conditions
DboSource