Misc many fixes
This commit is contained in:
@@ -73,5 +73,56 @@ class ORM extends Kohana_ORM {
|
||||
else
|
||||
return HTML::nbsp($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Override KH's ORM has() function, to include our site_id in the query.
|
||||
*
|
||||
* This is a copy of KH's ORM has() function, with the addition of a where
|
||||
* clause to include the site id.
|
||||
*/
|
||||
public function has($alias, $far_keys) {
|
||||
$far_keys = ($far_keys instanceof ORM) ? $far_keys->pk() : $far_keys;
|
||||
|
||||
// We need an array to simplify the logic
|
||||
$far_keys = (array) $far_keys;
|
||||
|
||||
// Nothing to check if the model isn't loaded or we don't have any far_keys
|
||||
if ( ! $far_keys OR ! $this->_loaded)
|
||||
return FALSE;
|
||||
|
||||
$count = (int) DB::select(array('COUNT("*")', 'records_found'))
|
||||
->from($this->_has_many[$alias]['through'])
|
||||
->where($this->_has_many[$alias]['foreign_key'], '=', $this->pk())
|
||||
->where($this->_has_many[$alias]['far_key'], 'IN', $far_keys)
|
||||
->where('site_id', '=', Config::siteid())
|
||||
->execute($this->_db)->get('records_found');
|
||||
|
||||
// Rows found need to match the rows searched
|
||||
return $count === count($far_keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if this object has a relationship to a different model,
|
||||
* or an array of different models.
|
||||
*
|
||||
* // Check for any of the following roles
|
||||
* $model->has('roles', array(1, 2, 3, 4));
|
||||
*
|
||||
* @param string $alias Alias of the has_many "through" relationship
|
||||
* @param mixed $far_keys An array of primary keys
|
||||
* @return Database_Result
|
||||
*/
|
||||
public function has_any($alias, array $far_keys) {
|
||||
// Nothing to check if the model isn't loaded or we don't have any far_keys
|
||||
if ( ! $far_keys)
|
||||
return FALSE;
|
||||
|
||||
// Rows found need to match the rows searched
|
||||
return (int) DB::select(array('COUNT("*")', 'records_found'))
|
||||
->from($this->_has_many[$alias]['through'])
|
||||
->where($this->_has_many[$alias]['foreign_key'], '=', $this->pk())
|
||||
->where($this->_has_many[$alias]['far_key'], 'IN', $far_keys)
|
||||
->execute($this->_db)->get('records_found');
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
Reference in New Issue
Block a user