2009-11-22 06:29:24 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2013-05-20 12:18:07 +02:00
|
|
|
* Subclass of {@link DataList} representing a many_many relation.
|
|
|
|
*
|
|
|
|
* @package framework
|
|
|
|
* @subpackage model
|
2009-11-22 06:29:24 +01:00
|
|
|
*/
|
|
|
|
class ManyManyList extends RelationList {
|
2012-01-25 23:53:12 +01:00
|
|
|
|
2014-06-28 00:29:07 +02:00
|
|
|
/**
|
|
|
|
* @var string $joinTable
|
|
|
|
*/
|
2009-11-22 06:29:24 +01:00
|
|
|
protected $joinTable;
|
2012-01-25 23:53:12 +01:00
|
|
|
|
2014-06-28 00:29:07 +02:00
|
|
|
/**
|
|
|
|
* @var string $localKey
|
|
|
|
*/
|
2009-11-22 06:29:24 +01:00
|
|
|
protected $localKey;
|
2012-01-25 23:53:12 +01:00
|
|
|
|
2014-06-28 00:29:07 +02:00
|
|
|
/**
|
|
|
|
* @var string $foreignKey
|
|
|
|
*/
|
2012-12-12 05:22:45 +01:00
|
|
|
protected $foreignKey;
|
2009-11-22 06:29:24 +01:00
|
|
|
|
2014-06-28 00:29:07 +02:00
|
|
|
/**
|
|
|
|
* @var array $extraFields
|
|
|
|
*/
|
2009-11-22 06:29:24 +01:00
|
|
|
protected $extraFields;
|
|
|
|
|
2014-06-28 00:29:07 +02:00
|
|
|
/**
|
|
|
|
* @var array $_compositeExtraFields
|
|
|
|
*/
|
|
|
|
protected $_compositeExtraFields = array();
|
|
|
|
|
2009-11-22 06:29:24 +01:00
|
|
|
/**
|
|
|
|
* Create a new ManyManyList object.
|
|
|
|
*
|
2014-06-28 00:29:07 +02:00
|
|
|
* A ManyManyList object represents a list of {@link DataObject} records
|
|
|
|
* that correspond to a many-many relationship.
|
2009-11-22 06:29:24 +01:00
|
|
|
*
|
2014-06-28 00:29:07 +02:00
|
|
|
* 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.
|
2009-11-22 06:29:24 +01:00
|
|
|
*
|
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);
|
2014-06-28 00:29:07 +02:00
|
|
|
|
2009-11-22 06:29:24 +01:00
|
|
|
$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
|
|
|
|
2014-06-28 00:29:07 +02:00
|
|
|
// Add the extra fields to the query
|
|
|
|
if($this->extraFields) {
|
|
|
|
$this->appendExtraFieldsToQuery();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the many_many_extraFields to the select of the underlying
|
|
|
|
* {@link DataQuery}.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function appendExtraFieldsToQuery() {
|
|
|
|
$finalized = array();
|
|
|
|
|
|
|
|
foreach($this->extraFields as $field => $spec) {
|
|
|
|
$obj = Object::create_from_string($spec);
|
|
|
|
|
|
|
|
if($obj instanceof CompositeDBField) {
|
|
|
|
$this->_compositeExtraFields[$field] = array();
|
|
|
|
|
|
|
|
// append the composite field names to the select
|
|
|
|
foreach($obj->compositeDatabaseFields() as $k => $f) {
|
|
|
|
$col = $field . $k;
|
|
|
|
$finalized[] = $col;
|
|
|
|
|
|
|
|
// cache
|
|
|
|
$this->_compositeExtraFields[$field][] = $k;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$finalized[] = $field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->dataQuery->selectFromTable($this->joinTable, $finalized);
|
2009-11-22 06:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-28 00:29:07 +02:00
|
|
|
* Create a DataObject from the given SQL row.
|
|
|
|
*
|
|
|
|
* @param array $row
|
|
|
|
* @return DataObject
|
|
|
|
*/
|
|
|
|
protected function createDataObject($row) {
|
|
|
|
// remove any composed fields
|
|
|
|
$add = array();
|
|
|
|
|
|
|
|
if($this->_compositeExtraFields) {
|
|
|
|
foreach($this->_compositeExtraFields as $fieldName => $composed) {
|
|
|
|
// convert joined extra fields into their composite field
|
|
|
|
// types.
|
|
|
|
$value = array();
|
|
|
|
|
|
|
|
foreach($composed as $i => $k) {
|
|
|
|
if(isset($row[$fieldName . $k])) {
|
|
|
|
$value[$k] = $row[$fieldName . $k];
|
|
|
|
|
|
|
|
// don't duplicate data in the record
|
|
|
|
unset($row[$fieldName . $k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$obj = Object::create_from_string($this->extraFields[$fieldName], $fieldName);
|
|
|
|
$obj->setValue($value, null, false);
|
|
|
|
|
|
|
|
$add[$fieldName] = $obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$dataObject = parent::createDataObject($row);
|
|
|
|
|
|
|
|
foreach($add as $fieldName => $obj) {
|
|
|
|
$dataObject->$fieldName = $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dataObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a filter expression for when getting the contents of the
|
|
|
|
* relationship for some foreign ID
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
*
|
2012-12-12 05:22:45 +01:00
|
|
|
* @return string
|
2009-11-22 06:29:24 +01:00
|
|
|
*/
|
2012-12-12 05:22:45 +01:00
|
|
|
protected function foreignIDFilter($id = null) {
|
|
|
|
if ($id === null) $id = $this->getForeignID();
|
|
|
|
|
2009-11-22 06:29:24 +01:00
|
|
|
// Apply relation filter
|
2012-12-12 05:22:45 +01:00
|
|
|
if(is_array($id)) {
|
2009-11-22 06:29:24 +01:00
|
|
|
return "\"$this->joinTable\".\"$this->foreignKey\" IN ('" .
|
2012-12-12 05:22:45 +01:00
|
|
|
implode("', '", array_map('Convert::raw2sql', $id)) . "')";
|
|
|
|
} else if($id !== null){
|
2009-11-22 06:29:24 +01:00
|
|
|
return "\"$this->joinTable\".\"$this->foreignKey\" = '" .
|
2012-12-12 05:22:45 +01:00
|
|
|
Convert::raw2sql($id) . "'";
|
2009-11-22 06:29:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
/**
|
|
|
|
* Return a filter expression for the join table when writing to the join table
|
|
|
|
*
|
|
|
|
* When writing (add, remove, removeByID), we need to filter the join table to just the relevant
|
|
|
|
* entries. However some subclasses of ManyManyList (Member_GroupSet) modify foreignIDFilter to
|
|
|
|
* include additional calculated entries, so we need different filters when reading and when writing
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function foreignIDWriteFilter($id = null) {
|
|
|
|
return $this->foreignIDFilter($id);
|
|
|
|
}
|
|
|
|
|
2009-11-22 06:29:24 +01:00
|
|
|
/**
|
2014-06-28 00:29:07 +02:00
|
|
|
* Add an item to this many_many relationship. Does so by adding an entry
|
|
|
|
* to the joinTable.
|
|
|
|
*
|
|
|
|
* @param mixed $item
|
|
|
|
* @param array $extraFields A map of additional columns to insert into the
|
|
|
|
* joinTable
|
2009-11-22 06:29:24 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function add($item, $extraFields = null) {
|
2014-06-28 00:29:07 +02: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
|
|
|
|
);
|
2012-09-26 23:34:00 +02:00
|
|
|
}
|
2012-12-12 05:22:45 +01:00
|
|
|
|
|
|
|
$foreignIDs = $this->getForeignID();
|
|
|
|
$foreignFilter = $this->foreignIDWriteFilter();
|
|
|
|
|
2009-11-22 06:29:24 +01:00
|
|
|
// Validate foreignID
|
2012-12-12 05:22:45 +01:00
|
|
|
if(!$foreignIDs) {
|
2009-11-22 06:29:24 +01:00
|
|
|
throw new Exception("ManyManyList::add() can't be called until a foreign ID is set", E_USER_WARNING);
|
|
|
|
}
|
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
if($foreignFilter) {
|
2012-09-20 15:44:30 +02:00
|
|
|
$query = new SQLQuery("*", array("\"$this->joinTable\""));
|
2012-12-12 05:22:45 +01:00
|
|
|
$query->setWhere($foreignFilter);
|
2012-09-20 15:44:30 +02:00
|
|
|
$hasExisting = ($query->count() > 0);
|
|
|
|
} else {
|
|
|
|
$hasExisting = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert or update
|
2012-12-12 05:22:45 +01:00
|
|
|
foreach((array)$foreignIDs as $foreignID) {
|
2012-03-06 16:34:51 +01:00
|
|
|
$manipulation = array();
|
2014-06-28 00:29:07 +02:00
|
|
|
|
2012-09-20 15:44:30 +02:00
|
|
|
if($hasExisting) {
|
|
|
|
$manipulation[$this->joinTable]['command'] = 'update';
|
|
|
|
$manipulation[$this->joinTable]['where'] = "\"$this->joinTable\".\"$this->foreignKey\" = " .
|
|
|
|
"'" . Convert::raw2sql($foreignID) . "'" .
|
|
|
|
" AND \"$this->localKey\" = {$itemID}";
|
|
|
|
} else {
|
|
|
|
$manipulation[$this->joinTable]['command'] = 'insert';
|
|
|
|
}
|
2009-11-22 06:29:24 +01:00
|
|
|
|
2014-06-28 00:29:07 +02:00
|
|
|
if($extraFields) {
|
|
|
|
foreach($extraFields as $k => $v) {
|
|
|
|
if(is_null($v)) {
|
|
|
|
$manipulation[$this->joinTable]['fields'][$k] = 'NULL';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(is_object($v) && $v instanceof DBField) {
|
|
|
|
// rely on writeToManipulation to manage the changes
|
|
|
|
// required for this field.
|
|
|
|
$working = array('fields' => array());
|
|
|
|
|
|
|
|
// create a new instance of the field so we can
|
|
|
|
// modify the field name to the correct version.
|
|
|
|
$field = DBField::create_field(get_class($v), $v);
|
|
|
|
$field->setName($k);
|
|
|
|
|
|
|
|
$field->writeToManipulation($working);
|
|
|
|
|
|
|
|
foreach($working['fields'] as $extraK => $extraV) {
|
|
|
|
$manipulation[$this->joinTable]['fields'][$extraK] = $extraV;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$manipulation[$this->joinTable]['fields'][$k] = "'" . Convert::raw2sql($v) . "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-06 16:34:51 +01:00
|
|
|
}
|
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.
|
2014-06-28 00:29:07 +02:00
|
|
|
*
|
|
|
|
* Note that for a ManyManyList, the item is never actually deleted, only
|
|
|
|
* the join table is affected.
|
|
|
|
*
|
|
|
|
* @param DataObject $item
|
2009-11-22 06:29:24 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function remove($item) {
|
2012-12-08 12:20:20 +01:00
|
|
|
if(!($item instanceof $this->dataClass)) {
|
|
|
|
throw new InvalidArgumentException("ManyManyList::remove() expecting a $this->dataClass object");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->removeByID($item->ID);
|
2011-03-30 03:19:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the given item from this list.
|
2014-06-28 00:29:07 +02:00
|
|
|
*
|
|
|
|
* Note that for a ManyManyList, the item is never actually deleted, only
|
|
|
|
* the join table is affected
|
|
|
|
*
|
|
|
|
* @param int $itemID The item ID
|
2011-03-30 03:19:27 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function removeByID($itemID) {
|
2012-12-08 12:20:20 +01: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);
|
2012-12-12 05:22:45 +01:00
|
|
|
|
|
|
|
if($filter = $this->foreignIDWriteFilter($this->getForeignID())) {
|
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();
|
|
|
|
}
|
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
/**
|
2014-06-28 00:29:07 +02:00
|
|
|
* Remove all items from this many-many join. To remove a subset of items,
|
|
|
|
* filter it first.
|
|
|
|
*
|
|
|
|
* @return void
|
2012-12-08 12:20:20 +01:00
|
|
|
*/
|
|
|
|
public function removeAll() {
|
2013-03-02 07:23:15 +01:00
|
|
|
$base = ClassInfo::baseDataClass($this->dataClass());
|
|
|
|
|
|
|
|
// Remove the join to the join table to avoid MySQL row locking issues.
|
|
|
|
$query = $this->dataQuery();
|
|
|
|
$query->removeFilterOn($query->getQueryParam('Foreign.Filter'));
|
|
|
|
|
|
|
|
$query = $query->query();
|
|
|
|
$query->setSelect("\"$base\".\"ID\"");
|
|
|
|
|
|
|
|
$from = $query->getFrom();
|
|
|
|
unset($from[$this->joinTable]);
|
|
|
|
$query->setFrom($from);
|
2013-03-28 18:16:50 +01:00
|
|
|
$query->setDistinct(false);
|
2014-06-28 00:29:07 +02:00
|
|
|
|
|
|
|
// ensure any default sorting is removed, ORDER BY can break DELETE clauses
|
|
|
|
$query->setOrderBy(null, null);
|
2013-03-02 07:23:15 +01:00
|
|
|
|
|
|
|
// Use a sub-query as SQLite does not support setting delete targets in
|
|
|
|
// joined queries.
|
|
|
|
$delete = new SQLQuery();
|
|
|
|
$delete->setDelete(true);
|
|
|
|
$delete->setFrom("\"$this->joinTable\"");
|
|
|
|
$delete->addWhere($this->foreignIDFilter());
|
|
|
|
$delete->addWhere("\"$this->joinTable\".\"$this->localKey\" IN ({$query->sql()})");
|
|
|
|
$delete->execute();
|
2012-12-08 12:20:20 +01:00
|
|
|
}
|
2009-11-22 06:29:24 +01:00
|
|
|
|
|
|
|
/**
|
2014-06-28 00:29:07 +02:00
|
|
|
* Find the extra field data for a single row of the relationship join
|
|
|
|
* table, given the known child ID.
|
2009-11-22 06:29:24 +01:00
|
|
|
*
|
|
|
|
* @param string $componentName The name of the component
|
2012-09-20 15:44:30 +02:00
|
|
|
* @param int $itemID The ID of the child for the relationship
|
2014-06-28 00:29:07 +02:00
|
|
|
*
|
2009-11-22 06:29:24 +01:00
|
|
|
* @return array Map of fieldName => fieldValue
|
|
|
|
*/
|
2014-06-28 00:29:07 +02:00
|
|
|
public function getExtraData($componentName, $itemID) {
|
2009-11-22 06:29:24 +01:00
|
|
|
$result = array();
|
|
|
|
|
2012-09-20 15:44:30 +02:00
|
|
|
if(!is_numeric($itemID)) {
|
2009-11-22 06:29:24 +01:00
|
|
|
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) {
|
2012-10-03 04:34:39 +02:00
|
|
|
$query = new SQLQuery("\"$fieldName\"", array("\"$this->joinTable\""));
|
2012-12-12 05:22:45 +01:00
|
|
|
if($filter = $this->foreignIDWriteFilter($this->getForeignID())) {
|
2012-09-20 15:44:30 +02:00
|
|
|
$query->setWhere($filter);
|
|
|
|
} else {
|
|
|
|
user_error("Can't call ManyManyList::getExtraData() until a foreign ID is set", E_USER_WARNING);
|
|
|
|
}
|
|
|
|
$query->addWhere("\"$this->localKey\" = {$itemID}");
|
|
|
|
$result[$fieldName] = $query->execute()->value();
|
2009-11-22 06:29:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2013-01-10 22:21:08 +01:00
|
|
|
|
|
|
|
/**
|
2013-01-31 15:00:06 +01:00
|
|
|
* Gets the join table used for the relationship.
|
|
|
|
*
|
|
|
|
* @return string the name of the table
|
|
|
|
*/
|
|
|
|
public function getJoinTable() {
|
|
|
|
return $this->joinTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the key used to store the ID of the local/parent object.
|
|
|
|
*
|
|
|
|
* @return string the field name
|
|
|
|
*/
|
|
|
|
public function getLocalKey() {
|
|
|
|
return $this->localKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the key used to store the ID of the foreign/child object.
|
|
|
|
*
|
|
|
|
* @return string the field name
|
|
|
|
*/
|
|
|
|
public function getForeignKey() {
|
|
|
|
return $this->foreignKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the extra fields included in the relationship.
|
|
|
|
*
|
|
|
|
* @return array a map of field names to types
|
2013-01-10 22:21:08 +01:00
|
|
|
*/
|
2013-01-31 15:00:06 +01:00
|
|
|
public function getExtraFields() {
|
2013-01-10 22:21:08 +01:00
|
|
|
return $this->extraFields;
|
|
|
|
}
|
|
|
|
|
2009-11-22 06:29:24 +01:00
|
|
|
}
|