PageRenderTime 20ms CodeModel.GetById 11ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/core/db/DbRelationshipHasMany.php

http://zoop.googlecode.com/
PHP | 145 lines | 99 code | 28 blank | 18 comment | 10 complexity | f0e586926a1517911aa7bb47db139791 MD5 | raw file
  1<?php
  2class DbRelationshipHasMany extends DbRelationshipBasic implements Iterator, ArrayAccess, Countable
  3{
  4	private $theMany;
  5	
  6	public function __construct($name, $params, $dbObject)
  7	{
  8		if(isset($params['order_by']))
  9			trigger_error("depricated paramater: user orderBy");
 10		if(isset($params['orderby']))
 11			trigger_error("depricated paramater: user orderBy");
 12		if(isset($params['map_field']))
 13			trigger_error("depricated paramater: user mapField");
 14		
 15		parent::__construct($name, $params, $dbObject);
 16		if(!isset($params['orderBy']))
 17			$this->params['orderBy'] = array();
 18			
 19		if(!isset($params['mapField']))
 20			$this->params['mapField'] = false;
 21			
 22		if(!isset($params['conditions']))
 23			$this->params['conditions'] = array();
 24			
 25		if(!isset($params['createOrderedRows']))
 26			$this->params['createOrderedRows'] = false;
 27		
 28		if(!isset($params['createDefaultRows']))
 29			$this->params['createDefaultRows'] = false;
 30	}
 31	
 32	public function add()
 33	{
 34		return new $this->remoteClassName(array($this->remoteFieldName => $this->dbObject->getField($this->localFieldName)));
 35	}
 36	
 37	public function push($object)
 38	{
 39		$this->theMany[] = $object;
 40	}
 41	
 42	public function getInfo()
 43	{
 44		if(!$this->theMany)
 45		{
 46			$params = array();
 47			$params['orderby'] = $this->params['orderBy'];
 48			$remoteTableName = DbObject::_getTableName($this->remoteClassName);
 49			$conditions = $this->params['conditions'];
 50			$conditions[$this->remoteFieldName] = $this->dbObject->getField($this->localFieldName);
 51			$selectInfo = DbConnection::generateSelectInfo($remoteTableName, '*', $conditions, $params);
 52			$rows = $this->dbObject->getDb()->fetchRows($selectInfo['sql'], $selectInfo['params']);
 53			$this->theMany = array();
 54			foreach($rows as $thisRow)
 55			{
 56				if($this->params['mapField'])
 57					$this->theMany[$thisRow[$this->params['mapField']]] = new $this->remoteClassName($thisRow);
 58				else
 59					$this->theMany[] = new $this->remoteClassName($thisRow);
 60			}
 61		}
 62		
 63		return $this;
 64	}
 65	
 66	//
 67	//	begin iterator functions
 68	//
 69	
 70	public function rewind()
 71	{
 72		reset($this->theMany);
 73	}
 74
 75	public function current()
 76	{
 77		$var = current($this->theMany);
 78		return $var;
 79	}
 80
 81	public function key()
 82	{
 83		$var = key($this->theMany);
 84		return $var;
 85	}
 86
 87	public function next()
 88	{
 89		$var = next($this->theMany);
 90		return $var;
 91	}
 92
 93	public function valid()
 94	{
 95		$var = $this->current() !== false;
 96		return $var;
 97	}
 98	
 99	//
100	//	end iterator functions
101	//
102	
103	//
104	//	begin array access functions
105	//
106	
107	public function offsetExists($offset)
108	{
109		return isset($this->theMany[$offset]);
110	}
111	
112	public function offsetGet($offset)
113	{
114		return isset($this->theMany[$offset]) ? $this->theMany[$offset] : null;
115	}
116	
117	public function offsetSet($offset, $value)
118	{
119		$this->theMany[$offset] = $value;
120	}
121	
122	public function offsetUnset($offset)
123	{
124		unset($this->theMany[$offset]);
125	}
126	
127	//
128	//	end array access functions
129	//
130	
131	
132	//
133	//	begin countable functions
134	//
135	
136	public function count()
137	{
138		return count($this->theMany);
139	}
140	
141	//
142	//	end countable functions
143	//
144
145}