Cleanup of RelationList, HasManyList and ManyManyList

This commit is contained in:
Daniel Hensby 2016-08-19 10:37:58 +01:00
parent 6e864702ce
commit 8737ddefba
No known key found for this signature in database
GPG Key ID: B00D1E9767F0B06E
3 changed files with 25 additions and 9 deletions

View File

@ -8,6 +8,9 @@
*/ */
class HasManyList extends RelationList { class HasManyList extends RelationList {
/**
* @var string
*/
protected $foreignKey; protected $foreignKey;
/** /**
@ -34,6 +37,10 @@ class HasManyList extends RelationList {
return $this->foreignKey; return $this->foreignKey;
} }
/**
* @param null|int $id
* @return array
*/
protected function foreignIDFilter($id = null) { protected function foreignIDFilter($id = null) {
if ($id === null) $id = $this->getForeignID(); if ($id === null) $id = $this->getForeignID();
@ -51,7 +58,7 @@ class HasManyList extends RelationList {
* *
* It does so by setting the relationFilters. * It does so by setting the relationFilters.
* *
* @param $item The DataObject to be added, or its ID * @param DataObject|int $item The DataObject to be added, or its ID
*/ */
public function add($item) { public function add($item) {
if(is_numeric($item)) { if(is_numeric($item)) {
@ -83,7 +90,7 @@ class HasManyList extends RelationList {
* *
* Doesn't actually remove the item, it just clears the foreign key value. * Doesn't actually remove the item, it just clears the foreign key value.
* *
* @param $itemID The ID of the item to be removed. * @param int $itemID The ID of the item to be removed.
*/ */
public function removeByID($itemID) { public function removeByID($itemID) {
$item = $this->byID($itemID); $item = $this->byID($itemID);
@ -95,7 +102,7 @@ class HasManyList extends RelationList {
* Remove an item from this relation. * Remove an item from this relation.
* Doesn't actually remove the item, it just clears the foreign key value. * Doesn't actually remove the item, it just clears the foreign key value.
* *
* @param $item The DataObject to be removed * @param DataObject $item The DataObject to be removed
* @todo Maybe we should delete the object instead? * @todo Maybe we should delete the object instead?
*/ */
public function remove($item) { public function remove($item) {

View File

@ -47,7 +47,7 @@ class ManyManyList extends RelationList {
* @param string $joinTable The name of the table whose entries define the content of this many_many relation. * @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 $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 $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. * @param array $extraFields A map of field => fieldtype of extra fields on the join table.
* *
* @example new ManyManyList('Group','Group_Members', 'GroupID', 'MemberID'); * @example new ManyManyList('Group','Group_Members', 'GroupID', 'MemberID');
*/ */
@ -151,9 +151,9 @@ class ManyManyList extends RelationList {
* Return a filter expression for when getting the contents of the * Return a filter expression for when getting the contents of the
* relationship for some foreign ID * relationship for some foreign ID
* *
* @param int $id * @param int|null $id
* *
* @return string * @return array
*/ */
protected function foreignIDFilter($id = null) { protected function foreignIDFilter($id = null) {
if ($id === null) { if ($id === null) {
@ -176,7 +176,7 @@ class ManyManyList extends RelationList {
* entries. However some subclasses of ManyManyList (Member_GroupSet) modify foreignIDFilter to * 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 * include additional calculated entries, so we need different filters when reading and when writing
* *
* @param array|integer $id (optional) An ID or an array of IDs - if not provided, will use the current ids * @param array|int|null $id (optional) An ID or an array of IDs - if not provided, will use the current ids
* as per getForeignID * as per getForeignID
* @return array Condition In array(SQL => parameters format) * @return array Condition In array(SQL => parameters format)
*/ */
@ -188,7 +188,10 @@ class ManyManyList extends RelationList {
* Add an item to this many_many relationship * Add an item to this many_many relationship
* Does so by adding an entry to the joinTable. * Does so by adding an entry to the joinTable.
* *
* @param mixed $item * @throws InvalidArgumentException
* @throws Exception
*
* @param DataObject|int $item
* @param array $extraFields A map of additional columns to insert into the joinTable. * @param array $extraFields A map of additional columns to insert into the joinTable.
* Column names should be ANSI quoted. * Column names should be ANSI quoted.
*/ */

View File

@ -10,6 +10,9 @@
*/ */
abstract class RelationList extends DataList { abstract class RelationList extends DataList {
/**
* @return string|null
*/
public function getForeignID() { public function getForeignID() {
return $this->dataQuery->getQueryParam('Foreign.ID'); return $this->dataQuery->getQueryParam('Foreign.ID');
} }
@ -19,6 +22,8 @@ abstract class RelationList extends DataList {
* the given foreign ID. * the given foreign ID.
* *
* @param int|array $id An ID or an array of IDs. * @param int|array $id An ID or an array of IDs.
*
* @return DataList
*/ */
public function forForeignID($id) { public function forForeignID($id) {
// Turn a 1-element array into a simple value // Turn a 1-element array into a simple value
@ -27,7 +32,8 @@ abstract class RelationList extends DataList {
// Calculate the new filter // Calculate the new filter
$filter = $this->foreignIDFilter($id); $filter = $this->foreignIDFilter($id);
$list = $this->alterDataQuery(function($query, $list) use ($id, $filter){ $list = $this->alterDataQuery(function($query) use ($id, $filter){
/** @var DataQuery $query */
// Check if there is an existing filter, remove if there is // Check if there is an existing filter, remove if there is
$currentFilter = $query->getQueryParam('Foreign.Filter'); $currentFilter = $query->getQueryParam('Foreign.Filter');
if($currentFilter) { if($currentFilter) {