2009-11-22 06:29:24 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A DataList that represents a relation.
|
|
|
|
* Adds the notion of a foreign ID that can be optionally set.
|
|
|
|
*
|
|
|
|
* @todo Is this additional class really necessary?
|
|
|
|
*/
|
|
|
|
abstract class RelationList extends DataList {
|
|
|
|
protected $foreignID;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the ID of the record that this ManyManyList is linking *from*.
|
|
|
|
* @param $id A single ID, or an array of IDs
|
|
|
|
*/
|
|
|
|
function setForeignID($id) {
|
2012-03-06 16:34:51 +01:00
|
|
|
// If already filtered on foreign ID, remove that first
|
|
|
|
if($this->foreignID !== null) {
|
|
|
|
$oldFilter = $this->foreignIDFilter();
|
|
|
|
try {
|
|
|
|
$this->dataQuery->removeFilterOn($oldFilter);
|
|
|
|
} catch(InvalidArgumentException $e) {}
|
|
|
|
}
|
|
|
|
|
2009-11-22 06:29:24 +01:00
|
|
|
// Turn a 1-element array into a simple value
|
|
|
|
if(is_array($id) && sizeof($id) == 1) $id = reset($id);
|
|
|
|
$this->foreignID = $id;
|
|
|
|
|
2011-04-05 13:01:57 +02:00
|
|
|
$this->dataQuery->where($this->foreignIDFilter());
|
2012-03-06 16:34:51 +01:00
|
|
|
|
|
|
|
return $this;
|
2009-11-22 06:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns this ManyMany relationship linked to the given foreign ID.
|
|
|
|
* @param $id An ID or an array of IDs.
|
|
|
|
*/
|
|
|
|
function forForeignID($id) {
|
|
|
|
$this->setForeignID($id);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract protected function foreignIDFilter();
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|