Skip to content

Instantly share code, notes, and snippets.

@muskie9
Created February 28, 2018 22:44
Show Gist options
  • Save muskie9/a71983adf20d563004add18c88b7990e to your computer and use it in GitHub Desktop.
Save muskie9/a71983adf20d563004add18c88b7990e to your computer and use it in GitHub Desktop.
<?php
/**
* Class DynamicManyManyList
*/
class DynamicManyManyList extends ManyManyList {
/**
* Remove the given item from this list.
*
* Note that for a ManyManyList, the item is never actually deleted, only
* the join table is affected
*
* @param int $itemID The item ID
*/
public function removeByID($itemID) {
if(!is_numeric($itemID)) throw new InvalidArgumentException("ManyManyList::removeById() expecting an ID");
$query = new SQLDelete("\"{$this->joinTable}\"");
$foreignID = $this->getForeignID();
$this->extend('onBeforeRemoveByID', $itemID, $this->joinTable, $foreignID);
if($filter = $this->foreignIDWriteFilter($foreignID)) {
$query->setWhere($filter);
} else {
user_error("Can't call ManyManyList::remove() until a foreign ID is set", E_USER_WARNING);
}
$query->addWhere(array("\"{$this->localKey}\"" => $itemID));
$query->execute();
$this->extend('onAfterRemoveByID', $itemID, $this->joinTable, $foreignID);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment