2009-11-22 06:29:24 +01:00
|
|
|
<?php
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
namespace SilverStripe\ORM;
|
|
|
|
|
|
|
|
use Exception;
|
2015-02-13 05:35:39 +01:00
|
|
|
|
2009-11-22 06:29:24 +01:00
|
|
|
/**
|
|
|
|
* A DataList that represents a relation.
|
2013-05-20 12:18:07 +02:00
|
|
|
*
|
2009-11-22 06:29:24 +01:00
|
|
|
* Adds the notion of a foreign ID that can be optionally set.
|
|
|
|
*/
|
2015-02-13 05:35:39 +01:00
|
|
|
abstract class RelationList extends DataList implements Relation {
|
2012-03-06 16:34:51 +01:00
|
|
|
|
2016-08-19 11:37:58 +02:00
|
|
|
/**
|
2016-09-26 07:22:19 +02:00
|
|
|
* Any number of foreign keys to apply to this list
|
|
|
|
*
|
|
|
|
* @return string|array|null
|
2016-08-19 11:37:58 +02:00
|
|
|
*/
|
2012-12-12 05:22:45 +01:00
|
|
|
public function getForeignID() {
|
|
|
|
return $this->dataQuery->getQueryParam('Foreign.ID');
|
2009-11-22 06:29:24 +01:00
|
|
|
}
|
2012-12-12 05:22:45 +01:00
|
|
|
|
2016-02-25 05:32:41 +01:00
|
|
|
public function getQueryParams() {
|
|
|
|
$params = parent::getQueryParams();
|
|
|
|
|
|
|
|
// Remove `Foreign.` query parameters for created objects,
|
|
|
|
// as this would interfere with relations on those objects.
|
|
|
|
foreach(array_keys($params) as $key) {
|
2016-03-02 06:18:10 +01:00
|
|
|
if(stripos($key, 'Foreign.') === 0) {
|
2016-02-25 05:32:41 +01:00
|
|
|
unset($params[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
2009-11-22 06:29:24 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Returns a copy of this list with the ManyMany relationship linked to
|
2013-05-20 12:18:07 +02:00
|
|
|
* the given foreign ID.
|
|
|
|
*
|
|
|
|
* @param int|array $id An ID or an array of IDs.
|
2016-08-19 11:37:58 +02:00
|
|
|
*
|
2016-02-25 05:32:41 +01:00
|
|
|
* @return static
|
2009-11-22 06:29:24 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function forForeignID($id) {
|
2012-12-12 05:22:45 +01:00
|
|
|
// Turn a 1-element array into a simple value
|
|
|
|
if(is_array($id) && sizeof($id) == 1) $id = reset($id);
|
|
|
|
|
|
|
|
// Calculate the new filter
|
|
|
|
$filter = $this->foreignIDFilter($id);
|
|
|
|
|
2016-09-26 07:22:19 +02:00
|
|
|
$list = $this->alterDataQuery(function(DataQuery $query) use ($id, $filter){
|
2012-12-12 05:22:45 +01:00
|
|
|
// Check if there is an existing filter, remove if there is
|
|
|
|
$currentFilter = $query->getQueryParam('Foreign.Filter');
|
|
|
|
if($currentFilter) {
|
|
|
|
try {
|
|
|
|
$query->removeFilterOn($currentFilter);
|
|
|
|
}
|
|
|
|
catch (Exception $e) { /* NOP */ }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the new filter
|
|
|
|
$query->setQueryParam('Foreign.ID', $id);
|
|
|
|
$query->setQueryParam('Foreign.Filter', $filter);
|
|
|
|
$query->where($filter);
|
2012-07-23 00:31:47 +02:00
|
|
|
});
|
2012-12-12 05:22:45 +01:00
|
|
|
|
|
|
|
return $list;
|
2009-11-22 06:29:24 +01:00
|
|
|
}
|
2012-12-12 05:22:45 +01:00
|
|
|
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Returns a where clause that filters the members of this relationship to
|
2013-05-20 12:18:07 +02:00
|
|
|
* just the related items.
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param array|integer $id (optional) An ID or an array of IDs - if not provided, will use the current ids as
|
|
|
|
* per getForeignID
|
|
|
|
* @return array Condition In array(SQL => parameters format)
|
2012-12-12 05:22:45 +01:00
|
|
|
*/
|
|
|
|
abstract protected function foreignIDFilter($id = null);
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|