DboSource::hasAny PHP Method

hasAny() public method

Checks if the specified table contains any record matching specified SQL
public hasAny ( Model $Model, string $sql ) : boolean
$Model Model Model to search
$sql string SQL WHERE clause (condition only, not the "WHERE" part)
return boolean True if the table has a matching record, else false
    public function hasAny(Model $Model, $sql)
    {
        $sql = $this->conditions($sql);
        $table = $this->fullTableName($Model);
        $alias = $this->alias . $this->name($Model->alias);
        $where = $sql ? "{$sql}" : ' WHERE 1 = 1';
        $id = $Model->escapeField();
        $out = $this->fetchRow("SELECT COUNT({$id}) {$this->alias}count FROM {$table} {$alias}{$where}");
        if (is_array($out)) {
            return $out[0]['count'];
        }
        return false;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * test hasAny()
  *
  * @return void
  */
 public function testHasAny()
 {
     $this->Dbo = $this->getMock('Mysql', array('connect', '_execute', 'execute', 'value'));
     $this->Model = $this->getMock('TestModel', array('getDataSource'));
     $this->Model->expects($this->any())->method('getDataSource')->will($this->returnValue($this->Dbo));
     $this->Dbo->expects($this->at(0))->method('value')->with('harry')->will($this->returnValue("'harry'"));
     $this->Dbo->expects($this->at(1))->method('execute')->with('SELECT COUNT(`TestModel`.`id`) AS count FROM `test_models` AS `TestModel` WHERE `TestModel`.`name` = \'harry\'');
     $this->Dbo->expects($this->at(2))->method('execute')->with('SELECT COUNT(`TestModel`.`id`) AS count FROM `test_models` AS `TestModel` WHERE 1 = 1');
     $this->Dbo->hasAny($this->Model, array('TestModel.name' => 'harry'));
     $this->Dbo->hasAny($this->Model, array());
 }
DboSource