2009-11-22 06:29:24 +01:00
|
|
|
<?php
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
namespace SilverStripe\ORM;
|
|
|
|
|
|
|
|
use Object;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use Exception;
|
|
|
|
use SilverStripe\ORM\Queries\SQLSelect;
|
|
|
|
use SilverStripe\ORM\Queries\SQLDelete;
|
|
|
|
use SilverStripe\ORM\FieldType\DBComposite;
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2009-11-22 06:29:24 +01:00
|
|
|
/**
|
2013-05-20 12:18:07 +02:00
|
|
|
* Subclass of {@link DataList} representing a many_many relation.
|
|
|
|
*
|
|
|
|
* @package framework
|
2016-06-15 06:03:16 +02:00
|
|
|
* @subpackage orm
|
2009-11-22 06:29:24 +01:00
|
|
|
*/
|
|
|
|
class ManyManyList extends RelationList {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-06-28 00:29:07 +02:00
|
|
|
/**
|
|
|
|
* @var string $joinTable
|
|
|
|
*/
|
2009-11-22 06:29:24 +01:00
|
|
|
protected $joinTable;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-06-28 00:29:07 +02:00
|
|
|
/**
|
|
|
|
* @var string $localKey
|
|
|
|
*/
|
2009-11-22 06:29:24 +01:00
|
|
|
protected $localKey;
|
2014-08-15 08:53:05 +02: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-08-15 08:53:05 +02:00
|
|
|
*
|
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.
|
2014-08-15 08:53:05 +02: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.
|
2014-08-15 08:53:05 +02: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.
|
2016-05-25 07:09:29 +02:00
|
|
|
* @param array $extraFields A map of field => fieldtype of extra fields on the join table.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-01-25 23:53:12 +01:00
|
|
|
* @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;
|
|
|
|
|
2014-07-14 23:31:03 +02:00
|
|
|
$this->linkJoinTable();
|
|
|
|
}
|
2009-11-22 06:29:24 +01:00
|
|
|
|
2014-07-14 23:31:03 +02:00
|
|
|
/**
|
|
|
|
* Setup the join between this dataobject and the necessary mapping table
|
|
|
|
*/
|
|
|
|
protected function linkJoinTable() {
|
2009-11-22 06:29:24 +01:00
|
|
|
// Join to the many-many join table
|
2016-05-25 07:09:29 +02:00
|
|
|
$dataClassIDColumn = DataObject::getSchema()->sqlColumnForField($this->dataClass(), 'ID');
|
|
|
|
$this->dataQuery->innerJoin(
|
|
|
|
$this->joinTable,
|
|
|
|
"\"{$this->joinTable}\".\"{$this->localKey}\" = {$dataClassIDColumn}"
|
|
|
|
);
|
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);
|
|
|
|
|
2015-08-30 07:02:55 +02:00
|
|
|
if($obj instanceof DBComposite) {
|
2014-06-28 00:29:07 +02:00
|
|
|
$this->_compositeExtraFields[$field] = array();
|
|
|
|
|
|
|
|
// append the composite field names to the select
|
2014-07-14 23:31:03 +02:00
|
|
|
foreach($obj->compositeDatabaseFields() as $subField => $subSpec) {
|
|
|
|
$col = $field . $subField;
|
2014-06-28 00:29:07 +02:00
|
|
|
$finalized[] = $col;
|
|
|
|
|
|
|
|
// cache
|
2014-07-14 23:31:03 +02:00
|
|
|
$this->_compositeExtraFields[$field][] = $subField;
|
2014-06-28 00:29:07 +02:00
|
|
|
}
|
|
|
|
} 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) {
|
2014-07-14 23:31:03 +02:00
|
|
|
// convert joined extra fields into their composite field types.
|
2014-06-28 00:29:07 +02:00
|
|
|
$value = array();
|
|
|
|
|
2015-09-04 05:49:22 +02:00
|
|
|
foreach($composed as $subField) {
|
|
|
|
if(isset($row[$fieldName . $subField])) {
|
|
|
|
$value[$subField] = $row[$fieldName . $subField];
|
2014-06-28 00:29:07 +02:00
|
|
|
|
|
|
|
// don't duplicate data in the record
|
2015-09-04 05:49:22 +02:00
|
|
|
unset($row[$fieldName . $subField]);
|
2014-06-28 00:29:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
|
*
|
2016-08-19 11:37:58 +02:00
|
|
|
* @param int|null $id
|
2014-06-28 00:29:07 +02:00
|
|
|
*
|
2016-08-19 11:37:58 +02:00
|
|
|
* @return array
|
2009-11-22 06:29:24 +01:00
|
|
|
*/
|
2012-12-12 05:22:45 +01:00
|
|
|
protected function foreignIDFilter($id = null) {
|
2013-06-21 00:32:08 +02:00
|
|
|
if ($id === null) {
|
|
|
|
$id = $this->getForeignID();
|
|
|
|
}
|
2012-12-12 05:22:45 +01:00
|
|
|
|
2009-11-22 06:29:24 +01:00
|
|
|
// Apply relation filter
|
2014-07-14 23:31:03 +02:00
|
|
|
$key = "\"{$this->joinTable}\".\"{$this->foreignKey}\"";
|
2012-12-12 05:22:45 +01:00
|
|
|
if(is_array($id)) {
|
2013-06-21 00:32:08 +02:00
|
|
|
return array("$key IN (".DB::placeholders($id).")" => $id);
|
2012-12-12 05:22:45 +01:00
|
|
|
} else if($id !== null){
|
2013-06-21 00:32:08 +02:00
|
|
|
return array($key => $id);
|
2009-11-22 06:29:24 +01:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02: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
|
|
|
|
*
|
2016-08-19 11:37:58 +02:00
|
|
|
* @param array|int|null $id (optional) An ID or an array of IDs - if not provided, will use the current ids
|
2013-06-21 00:32:08 +02:00
|
|
|
* as per getForeignID
|
|
|
|
* @return array Condition In array(SQL => parameters format)
|
2012-12-12 05:22:45 +01:00
|
|
|
*/
|
|
|
|
protected function foreignIDWriteFilter($id = null) {
|
|
|
|
return $this->foreignIDFilter($id);
|
|
|
|
}
|
|
|
|
|
2009-11-22 06:29:24 +01:00
|
|
|
/**
|
|
|
|
* Add an item to this many_many relationship
|
|
|
|
* Does so by adding an entry to the joinTable.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-08-19 11:37:58 +02:00
|
|
|
* @throws InvalidArgumentException
|
|
|
|
* @throws Exception
|
|
|
|
*
|
|
|
|
* @param DataObject|int $item
|
2014-07-14 23:31:03 +02:00
|
|
|
* @param array $extraFields A map of additional columns to insert into the joinTable.
|
|
|
|
* Column names should be ANSI quoted.
|
2016-05-25 07:09:29 +02:00
|
|
|
* @throws Exception
|
2009-11-22 06:29:24 +01:00
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
public function add($item, $extraFields = array()) {
|
|
|
|
// Ensure nulls or empty strings are correctly treated as empty arrays
|
|
|
|
if(empty($extraFields)) $extraFields = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Determine ID of new record
|
|
|
|
if(is_numeric($item)) {
|
|
|
|
$itemID = $item;
|
|
|
|
} elseif($item instanceof $this->dataClass) {
|
|
|
|
$itemID = $item->ID;
|
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
throw new InvalidArgumentException("ManyManyList::add() expecting a $this->dataClass object, or ID value",
|
|
|
|
E_USER_ERROR);
|
|
|
|
}
|
2012-12-12 05:22:45 +01:00
|
|
|
|
2009-11-22 06:29:24 +01:00
|
|
|
// Validate foreignID
|
2013-06-21 00:32:08 +02:00
|
|
|
$foreignIDs = $this->getForeignID();
|
|
|
|
if(empty($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);
|
|
|
|
}
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Apply this item to each given foreign ID record
|
|
|
|
if(!is_array($foreignIDs)) $foreignIDs = array($foreignIDs);
|
|
|
|
foreach($foreignIDs as $foreignID) {
|
|
|
|
// Check for existing records for this item
|
2014-07-14 23:31:03 +02:00
|
|
|
if($foreignFilter = $this->foreignIDWriteFilter($foreignID)) {
|
2013-06-21 00:32:08 +02:00
|
|
|
// With the current query, simply add the foreign and local conditions
|
|
|
|
// The query can be a bit odd, especially if custom relation classes
|
|
|
|
// don't join expected tables (@see Member_GroupSet for example).
|
2014-07-14 23:31:03 +02:00
|
|
|
$query = new SQLSelect("*", "\"{$this->joinTable}\"");
|
2013-06-21 00:32:08 +02:00
|
|
|
$query->addWhere($foreignFilter);
|
|
|
|
$query->addWhere(array(
|
2014-07-14 23:31:03 +02:00
|
|
|
"\"{$this->joinTable}\".\"{$this->localKey}\"" => $itemID
|
2013-06-21 00:32:08 +02:00
|
|
|
));
|
|
|
|
$hasExisting = ($query->count() > 0);
|
2012-09-20 15:44:30 +02:00
|
|
|
} else {
|
2014-08-15 08:53:05 +02:00
|
|
|
$hasExisting = false;
|
2012-09-20 15:44:30 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-08 05:44:27 +02:00
|
|
|
// Blank manipulation
|
|
|
|
$manipulation = array(
|
|
|
|
$this->joinTable => array(
|
|
|
|
'command' => $hasExisting ? 'update' : 'insert',
|
|
|
|
'fields' => array()
|
|
|
|
)
|
|
|
|
);
|
2014-07-14 23:31:03 +02:00
|
|
|
if($hasExisting) {
|
|
|
|
$manipulation[$this->joinTable]['where'] = array(
|
|
|
|
"\"{$this->joinTable}\".\"{$this->foreignKey}\"" => $foreignID,
|
2013-06-21 00:32:08 +02:00
|
|
|
"\"{$this->joinTable}\".\"{$this->localKey}\"" => $itemID
|
2014-07-14 23:31:03 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-06-08 05:44:27 +02:00
|
|
|
if($extraFields && $this->extraFields) {
|
|
|
|
// Write extra field to manipluation in the same way
|
|
|
|
// that DataObject::prepareManipulationTable writes fields
|
|
|
|
foreach($this->extraFields as $fieldName => $fieldSpec) {
|
|
|
|
// Skip fields without an assignment
|
|
|
|
if(array_key_exists($fieldName, $extraFields)) {
|
|
|
|
$fieldObject = Object::create_from_string($fieldSpec, $fieldName);
|
|
|
|
$fieldObject->setValue($extraFields[$fieldName]);
|
|
|
|
$fieldObject->writeToManipulation($manipulation[$this->joinTable]);
|
2014-07-14 23:31:03 +02:00
|
|
|
}
|
|
|
|
}
|
2012-03-06 16:34:51 +01:00
|
|
|
}
|
2014-07-14 23:31:03 +02:00
|
|
|
|
|
|
|
$manipulation[$this->joinTable]['fields'][$this->localKey] = $itemID;
|
|
|
|
$manipulation[$this->joinTable]['fields'][$this->foreignKey] = $foreignID;
|
|
|
|
|
|
|
|
DB::manipulate($manipulation);
|
2012-03-06 16:34:51 +01:00
|
|
|
}
|
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");
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
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
|
|
|
|
2014-07-14 23:31:03 +02:00
|
|
|
$query = new SQLDelete("\"{$this->joinTable}\"");
|
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);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
$query->addWhere(array(
|
|
|
|
"\"{$this->joinTable}\".\"{$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
|
|
|
|
|
|
|
// Remove the join to the join table to avoid MySQL row locking issues.
|
|
|
|
$query = $this->dataQuery();
|
2013-06-21 00:32:08 +02:00
|
|
|
$foreignFilter = $query->getQueryParam('Foreign.Filter');
|
|
|
|
$query->removeFilterOn($foreignFilter);
|
2013-03-02 07:23:15 +01:00
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
// Select ID column
|
2013-06-21 00:32:08 +02:00
|
|
|
$selectQuery = $query->query();
|
2016-05-25 07:09:29 +02:00
|
|
|
$dataClassIDColumn = DataObject::getSchema()->sqlColumnForField($this->dataClass(), 'ID');
|
|
|
|
$selectQuery->setSelect($dataClassIDColumn);
|
2013-03-02 07:23:15 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$from = $selectQuery->getFrom();
|
2013-03-02 07:23:15 +01:00
|
|
|
unset($from[$this->joinTable]);
|
2013-06-21 00:32:08 +02:00
|
|
|
$selectQuery->setFrom($from);
|
|
|
|
$selectQuery->setOrderBy(); // ORDER BY in subselects breaks MS SQL Server and is not necessary here
|
|
|
|
$selectQuery->setDistinct(false);
|
2013-03-02 07:23:15 +01:00
|
|
|
|
|
|
|
// Use a sub-query as SQLite does not support setting delete targets in
|
|
|
|
// joined queries.
|
2013-06-21 00:32:08 +02:00
|
|
|
$delete = new SQLDelete();
|
2014-07-14 23:31:03 +02:00
|
|
|
$delete->setFrom("\"{$this->joinTable}\"");
|
2013-03-02 07:23:15 +01:00
|
|
|
$delete->addWhere($this->foreignIDFilter());
|
2013-06-21 00:32:08 +02:00
|
|
|
$subSelect = $selectQuery->sql($parameters);
|
|
|
|
$delete->addWhere(array(
|
2014-07-14 23:31:03 +02:00
|
|
|
"\"{$this->joinTable}\".\"{$this->localKey}\" IN ($subSelect)" => $parameters
|
2013-06-21 00:32:08 +02:00
|
|
|
));
|
2013-03-02 07:23:15 +01:00
|
|
|
$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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
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();
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-08 05:44:27 +02:00
|
|
|
// Skip if no extrafields or unsaved record
|
|
|
|
if(empty($this->extraFields) || empty($itemID)) {
|
|
|
|
return $result;
|
|
|
|
}
|
2009-11-22 06:29:24 +01:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-06-08 05:44:27 +02:00
|
|
|
$cleanExtraFields = array();
|
|
|
|
foreach ($this->extraFields as $fieldName => $dbFieldSpec) {
|
|
|
|
$cleanExtraFields[] = "\"{$fieldName}\"";
|
|
|
|
}
|
2015-07-31 05:49:35 +02:00
|
|
|
$query = new SQLSelect($cleanExtraFields, "\"{$this->joinTable}\"");
|
2015-06-08 05:44:27 +02:00
|
|
|
$filter = $this->foreignIDWriteFilter($this->getForeignID());
|
|
|
|
if($filter) {
|
|
|
|
$query->setWhere($filter);
|
|
|
|
} else {
|
|
|
|
user_error("Can't call ManyManyList::getExtraData() until a foreign ID is set", E_USER_WARNING);
|
|
|
|
}
|
|
|
|
$query->addWhere(array(
|
2016-05-25 07:09:29 +02:00
|
|
|
"\"{$this->joinTable}\".\"{$this->localKey}\"" => $itemID
|
2015-06-08 05:44:27 +02:00
|
|
|
));
|
|
|
|
$queryResult = $query->execute()->current();
|
|
|
|
if ($queryResult) {
|
2015-04-26 11:02:02 +02:00
|
|
|
foreach ($queryResult as $fieldName => $value) {
|
|
|
|
$result[$fieldName] = $value;
|
2009-11-22 06:29:24 +01:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
|
|
}
|