Blame view

framework/model/RelationList.php 1.46 KB
0084d336   Administrator   Importers CRUD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  <?php
  
  /**
   * A DataList that represents a relation.
   *
   * Adds the notion of a foreign ID that can be optionally set.
   * 
   * @package framework
   * @subpackage model
   */
  abstract class RelationList extends DataList {
  
  	public function getForeignID() {
  		return $this->dataQuery->getQueryParam('Foreign.ID');
  	}
  
  	/**
  	 * Returns a copy of this list with the ManyMany relationship linked to 
  	 * the given foreign ID.
  	 *
  	 * @param int|array $id An ID or an array of IDs.
  	 */
  	public function forForeignID($id) {
  		// 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);
  
  		$list = $this->alterDataQuery(function($query, $list) use ($id, $filter){
  			// 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);
  		});
  
  		return $list;
  	}
  
  	/**
  	 * Returns a where clause that filters the members of this relationship to 
  	 * just the related items.
  	 *
  	 * @param $id (optional) An ID or an array of IDs - if not provided, will use the current ids as per getForeignID
  	 */
  	abstract protected function foreignIDFilter($id = null);
  }