2009-11-22 06:29:24 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subclass of {@link DataList} representing a many_many relation
|
|
|
|
*/
|
|
|
|
class ManyManyList extends RelationList {
|
2012-01-25 23:53:12 +01:00
|
|
|
|
2009-11-22 06:29:24 +01:00
|
|
|
protected $joinTable;
|
2012-01-25 23:53:12 +01:00
|
|
|
|
2009-11-22 06:29:24 +01:00
|
|
|
protected $localKey;
|
2012-01-25 23:53:12 +01:00
|
|
|
|
2009-11-22 06:29:24 +01:00
|
|
|
protected $foreignKey, $foreignID;
|
|
|
|
|
|
|
|
protected $extraFields;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new ManyManyList object.
|
|
|
|
*
|
|
|
|
* A ManyManyList object represents a list of DataObject records that correspond to a many-many
|
|
|
|
* relationship. In addition to,
|
|
|
|
*
|
|
|
|
* Generation of the appropriate record set is left up to the caller, using the normal
|
|
|
|
* {@link DataList} methods. Addition arguments are used to support {@@link add()}
|
|
|
|
* and {@link remove()} methods.
|
|
|
|
*
|
2012-01-25 23:53:12 +01:00
|
|
|
* @param string $dataClass The class of the DataObjects that this will list.
|
|
|
|
* @param string $joinTable The name of the table whose entries define the content of this many_many relation.
|
|
|
|
* @param string $localKey The key in the join table that maps to the dataClass' PK.
|
|
|
|
* @param string $foreignKey The key in the join table that maps to joined class' PK.
|
|
|
|
* @param string $extraFields A map of field => fieldtype of extra fields on the join table.
|
|
|
|
*
|
|
|
|
* @example new ManyManyList('Group','Group_Members', 'GroupID', 'MemberID');
|
2009-11-22 06:29:24 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($dataClass, $joinTable, $localKey, $foreignKey, $extraFields = array()) {
|
2009-11-22 06:29:24 +01:00
|
|
|
parent::__construct($dataClass);
|
|
|
|
$this->joinTable = $joinTable;
|
|
|
|
$this->localKey = $localKey;
|
|
|
|
$this->foreignKey = $foreignKey;
|
|
|
|
$this->extraFields = $extraFields;
|
|
|
|
|
|
|
|
$baseClass = ClassInfo::baseDataClass($dataClass);
|
|
|
|
|
|
|
|
// Join to the many-many join table
|
2012-03-14 17:03:29 +01:00
|
|
|
$this->dataQuery->innerJoin($joinTable, "\"$joinTable\".\"$this->localKey\" = \"$baseClass\".\"ID\"");
|
2009-11-22 06:29:24 +01:00
|
|
|
|
|
|
|
// Query the extra fields from the join table
|
|
|
|
if($extraFields) $this->dataQuery->selectFromTable($joinTable, array_keys($extraFields));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a filter expression for the foreign ID.
|
|
|
|
*/
|
|
|
|
protected function foreignIDFilter() {
|
|
|
|
// Apply relation filter
|
|
|
|
if(is_array($this->foreignID)) {
|
|
|
|
return "\"$this->joinTable\".\"$this->foreignKey\" IN ('" .
|
2011-03-30 05:51:26 +02:00
|
|
|
implode("', '", array_map('Convert::raw2sql', $this->foreignID)) . "')";
|
2012-03-05 23:59:27 +01:00
|
|
|
} else if($this->foreignID !== null){
|
2009-11-22 06:29:24 +01:00
|
|
|
return "\"$this->joinTable\".\"$this->foreignKey\" = '" .
|
|
|
|
Convert::raw2sql($this->foreignID) . "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an item to this many_many relationship
|
|
|
|
* Does so by adding an entry to the joinTable.
|
|
|
|
* @param $extraFields A map of additional columns to insert into the joinTable
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function add($item, $extraFields = null) {
|
2009-11-22 06:29:24 +01:00
|
|
|
if(is_numeric($item)) $itemID = $item;
|
|
|
|
else if($item instanceof $this->dataClass) $itemID = $item->ID;
|
|
|
|
else throw new InvalidArgumentException("ManyManyList::add() expecting a $this->dataClass object, or ID value", E_USER_ERROR);
|
|
|
|
|
|
|
|
// Validate foreignID
|
|
|
|
if(!$this->foreignID) {
|
|
|
|
throw new Exception("ManyManyList::add() can't be called until a foreign ID is set", E_USER_WARNING);
|
|
|
|
}
|
2012-03-06 16:34:51 +01:00
|
|
|
|
2009-11-22 06:29:24 +01:00
|
|
|
// Delete old entries, to prevent duplication
|
2011-03-30 03:19:27 +02:00
|
|
|
$this->removeById($itemID);
|
2009-11-22 06:29:24 +01:00
|
|
|
|
2012-03-06 16:34:51 +01:00
|
|
|
// Insert new entry/entries
|
|
|
|
foreach((array)$this->foreignID as $foreignID) {
|
|
|
|
$manipulation = array();
|
|
|
|
$manipulation[$this->joinTable]['command'] = 'insert';
|
2009-11-22 06:29:24 +01:00
|
|
|
|
2012-03-06 16:34:51 +01:00
|
|
|
if($extraFields) foreach($extraFields as $k => $v) {
|
|
|
|
$manipulation[$this->joinTable]['fields'][$k] = "'" . Convert::raw2sql($v) . "'";
|
|
|
|
}
|
2009-11-22 06:29:24 +01:00
|
|
|
|
2012-03-06 16:34:51 +01:00
|
|
|
$manipulation[$this->joinTable]['fields'][$this->localKey] = $itemID;
|
|
|
|
$manipulation[$this->joinTable]['fields'][$this->foreignKey] = $foreignID;
|
2009-11-22 06:29:24 +01:00
|
|
|
|
2012-03-06 16:34:51 +01:00
|
|
|
DB::manipulate($manipulation);
|
|
|
|
}
|
2009-11-22 06:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the given item from this list.
|
|
|
|
* Note that for a ManyManyList, the item is never actually deleted, only the join table is affected
|
2011-03-30 03:19:27 +02:00
|
|
|
* @param $itemID The ID of the item to remove.
|
2009-11-22 06:29:24 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function remove($item) {
|
2011-03-30 03:19:27 +02:00
|
|
|
if(!($item instanceof $this->dataClass)) throw new InvalidArgumentException("ManyManyList::remove() expecting a $this->dataClass object");
|
|
|
|
|
|
|
|
return $this->removeByID($item->ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 $itemID The item it
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function removeByID($itemID) {
|
2011-03-30 03:19:27 +02:00
|
|
|
if(!is_numeric($itemID)) throw new InvalidArgumentException("ManyManyList::removeById() expecting an ID");
|
2009-11-22 06:29:24 +01:00
|
|
|
|
2011-10-07 14:11:07 +02:00
|
|
|
$query = new SQLQuery("*", array("\"$this->joinTable\""));
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setDelete(true);
|
2009-11-22 06:29:24 +01:00
|
|
|
|
|
|
|
if($filter = $this->foreignIDFilter()) {
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setWhere($filter);
|
2009-11-22 06:29:24 +01:00
|
|
|
} else {
|
|
|
|
user_error("Can't call ManyManyList::remove() until a foreign ID is set", E_USER_WARNING);
|
|
|
|
}
|
|
|
|
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->addWhere("\"$this->localKey\" = {$itemID}");
|
2009-11-22 06:29:24 +01:00
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-29 06:23:53 +02:00
|
|
|
* Remove all items from this many-many join. To remove a subset of items, filter it first.
|
2009-11-22 06:29:24 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function removeAll() {
|
2011-10-29 06:23:53 +02:00
|
|
|
$query = $this->dataQuery()->query();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setDelete(true);
|
|
|
|
$query->setSelect(array('*'));
|
|
|
|
$query->setFrom("\"$this->joinTable\"");
|
2009-11-22 06:29:24 +01:00
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the extra field data for a single row of the relationship
|
|
|
|
* join table, given the known child ID.
|
|
|
|
*
|
|
|
|
* @todo Add tests for this / refactor it / something
|
|
|
|
*
|
|
|
|
* @param string $componentName The name of the component
|
|
|
|
* @param int $childID The ID of the child for the relationship
|
|
|
|
* @return array Map of fieldName => fieldValue
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getExtraData($componentName, $childID) {
|
2009-11-22 06:29:24 +01:00
|
|
|
$ownerObj = $this->ownerObj;
|
|
|
|
$parentField = $this->ownerClass . 'ID';
|
|
|
|
$childField = ($this->childClass == $this->ownerClass) ? 'ChildID' : ($this->childClass . 'ID');
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
if(!isset($componentName)) {
|
|
|
|
user_error('ComponentSet::getExtraData() passed a NULL component name', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!is_numeric($childID)) {
|
|
|
|
user_error('ComponentSet::getExtraData() passed a non-numeric child ID', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
// @todo Optimize into a single query instead of one per extra field
|
|
|
|
if($this->extraFields) {
|
|
|
|
foreach($this->extraFields as $fieldName => $dbFieldSpec) {
|
|
|
|
$query = DB::query("SELECT \"$fieldName\" FROM \"$this->tableName\" WHERE \"$parentField\" = {$this->ownerObj->ID} AND \"$childField\" = {$childID}");
|
|
|
|
$value = $query->value();
|
|
|
|
$result[$fieldName] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|