2009-11-22 06:29:24 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2013-05-20 12:18:07 +02:00
|
|
|
* Subclass of {@link DataList} representing a has_many relation.
|
|
|
|
*
|
|
|
|
* @package framework
|
|
|
|
* @subpackage model
|
2009-11-22 06:29:24 +01:00
|
|
|
*/
|
|
|
|
class HasManyList extends RelationList {
|
2013-05-20 12:18:07 +02:00
|
|
|
|
2009-11-22 06:29:24 +01:00
|
|
|
protected $foreignKey;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new HasManyList object.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param $dataClass The class of the DataObjects that this will list.
|
|
|
|
* @param $relationFilters A map of key => value filters that define which records
|
|
|
|
* in the $dataClass table actually belong to this relationship.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($dataClass, $foreignKey) {
|
2009-11-22 06:29:24 +01:00
|
|
|
parent::__construct($dataClass);
|
2013-05-20 12:18:07 +02:00
|
|
|
|
2009-11-22 06:29:24 +01:00
|
|
|
$this->foreignKey = $foreignKey;
|
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
return "\"$this->foreignKey\" IN ('" .
|
|
|
|
implode("', '", array_map('Convert::raw2sql', $id)) . "')";
|
|
|
|
} else if($id !== null){
|
2009-11-22 06:29:24 +01:00
|
|
|
return "\"$this->foreignKey\" = '" .
|
2012-12-12 05:22:45 +01:00
|
|
|
Convert::raw2sql($id) . "'";
|
2009-11-22 06:29:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the item to this relation.
|
2013-05-20 12:18:07 +02:00
|
|
|
*
|
2009-11-22 06:29:24 +01:00
|
|
|
* It does so by setting the relationFilters.
|
2013-05-20 12:18:07 +02:00
|
|
|
*
|
2009-11-22 06:29:24 +01:00
|
|
|
* @param $item The DataObject to be added, or its ID
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function add($item) {
|
2012-09-26 23:34:00 +02:00
|
|
|
if(is_numeric($item)) {
|
|
|
|
$item = DataObject::get_by_id($this->dataClass, $item);
|
|
|
|
} else if(!($item instanceof $this->dataClass)) {
|
|
|
|
user_error("HasManyList::add() expecting a $this->dataClass object, or ID value", E_USER_ERROR);
|
|
|
|
}
|
2009-11-22 06:29:24 +01:00
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
$foreignID = $this->getForeignID();
|
|
|
|
|
2009-11-22 06:29:24 +01:00
|
|
|
// Validate foreignID
|
2012-12-12 05:22:45 +01:00
|
|
|
if(!$foreignID) {
|
2009-11-22 06:29:24 +01:00
|
|
|
user_error("ManyManyList::add() can't be called until a foreign ID is set", E_USER_WARNING);
|
|
|
|
return;
|
|
|
|
}
|
2012-12-12 05:22:45 +01:00
|
|
|
if(is_array($foreignID)) {
|
2009-11-22 06:29:24 +01:00
|
|
|
user_error("ManyManyList::add() can't be called on a list linked to mulitple foreign IDs", E_USER_WARNING);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fk = $this->foreignKey;
|
2012-12-12 05:22:45 +01:00
|
|
|
$item->$fk = $foreignID;
|
2009-11-22 06:29:24 +01:00
|
|
|
|
|
|
|
$item->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an item from this relation.
|
2013-05-20 12:18:07 +02:00
|
|
|
*
|
2009-11-22 06:29:24 +01:00
|
|
|
* Doesn't actually remove the item, it just clears the foreign key value.
|
2013-05-20 12:18:07 +02:00
|
|
|
*
|
|
|
|
* @param $itemID The ID of the item to be removed.
|
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
|
|
|
$item = $this->byID($itemID);
|
2013-05-20 12:18:07 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
return $this->remove($item);
|
|
|
|
}
|
|
|
|
|
2011-03-30 03:19:27 +02:00
|
|
|
/**
|
|
|
|
* Remove an item from this relation.
|
|
|
|
* Doesn't actually remove the item, it just clears the foreign key value.
|
|
|
|
* @param $item The DataObject to be removed
|
2009-11-22 06:29:24 +01:00
|
|
|
* @todo Maybe we should delete the object instead?
|
|
|
|
*/
|
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("HasManyList::remove() expecting a $this->dataClass object, or ID",
|
|
|
|
E_USER_ERROR);
|
|
|
|
}
|
2009-11-22 06:29:24 +01:00
|
|
|
|
|
|
|
$fk = $this->foreignKey;
|
|
|
|
$item->$fk = null;
|
|
|
|
|
|
|
|
$item->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|