Skip to content

Commit

Permalink
cleaning up some code conventions and simplifying recent code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
isaiahdw committed Dec 31, 2010
1 parent c27802a commit e62f93e
Showing 1 changed file with 14 additions and 32 deletions.
46 changes: 14 additions & 32 deletions classes/kohana/orm.php
Original file line number Diff line number Diff line change
Expand Up @@ -1372,10 +1372,7 @@ public function delete()
*/
public function has($alias, $far_keys)
{
if ($far_keys instanceof ORM)
{
$far_keys = $far_keys->pk();
}
$far_keys = ($far_keys instanceof ORM) ? $far_keys->pk() : $far_keys;

// We need an array to simplify the logic
$far_keys = (array) $far_keys;
Expand All @@ -1400,32 +1397,22 @@ public function has($alias, $far_keys)
* // Add multiple roles (for example, from checkboxes on a form)
* $model->add('roles', array(1, 2, 3, 4));
*
* @param string $alias Alias of the has_many "through" relationship
* @param mixed $far_key Related model, primary key, or an array of primary keys
* @param string $alias Alias of the has_many "through" relationship
* @param mixed $far_keys Related model, primary key, or an array of primary keys
* @return ORM
*/
public function add($alias, $far_key)
public function add($alias, $far_keys)
{
if ($far_key instanceof ORM)
{
$far_key = $far_key->pk();
}
$far_keys = ($far_keys instanceof ORM) ? $far_keys->pk() : $far_keys;

$columns = array($this->_has_many[$alias]['foreign_key'], $this->_has_many[$alias]['far_key']);
$foreign_key = $this->pk();

$query = DB::insert($this->_has_many[$alias]['through'], $columns);

if (is_array($far_key))
foreach ( (array) $far_keys as $key)
{
foreach ($far_key as $key)
{
$query->values(array($foreign_key, $key));
}
}
else
{
$query->values(array($foreign_key, $far_key));
$query->values(array($foreign_key, $key));
}

$query->execute($this->_db);
Expand All @@ -1445,26 +1432,21 @@ public function add($alias, $far_key)
* // Remove all related roles
* $model->remove('roles');
*
* @param string $alias Alias of the has_many "through" relationship
* @param mixed $far_key Related model, primary key, or an array of primary keys
* @param string $alias Alias of the has_many "through" relationship
* @param mixed $far_keys Related model, primary key, or an array of primary keys
* @return ORM
*/
public function remove($alias, $far_key = NULL)
public function remove($alias, $far_keys = NULL)
{
$far_keys = ($far_keys instanceof ORM) ? $far_keys->pk() : $far_keys;

$query = DB::delete($this->_has_many[$alias]['through'])
->where($this->_has_many[$alias]['foreign_key'], '=', $this->pk());

$far_key = ($far_key instanceof ORM) ? $far_key->pk() : $far_key;

if (is_array($far_key))
if ($far_keys !== NULL)
{
// Remove all the relationships in the array
$query->where($this->_has_many[$alias]['far_key'], 'IN', $far_key);
}
elseif ($far_key !== NULL)
{
// Remove a single relationship
$query->where($this->_has_many[$alias]['far_key'], '=', $far_key);
$query->where($this->_has_many[$alias]['far_key'], 'IN', (array) $far_keys);
}

$query->execute($this->_db);
Expand Down

0 comments on commit e62f93e

Please sign in to comment.