PageRenderTime 24ms CodeModel.GetById 10ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/framework/core/db/DbRelationshipOptions.php

http://zoop.googlecode.com/
PHP | 67 lines | 57 code | 10 blank | 0 comment | 8 complexity | 587803b3aab6a90a56dd6a2790f200cf MD5 | raw file
 1<?php
 2class DbRelationshipOptions extends DbRelationship
 3{
 4	protected $options;
 5	protected $localField;
 6	protected $remoteTable;
 7	protected $remoteKeyField;
 8	protected $remoteValueField;
 9	
10	function __construct($name, $params, $dbObject)
11	{
12		parent::__construct($name, $params, $dbObject);
13		
14		if(isset($params['options']))
15		{
16			if(isset($params['localField']))
17				$this->localFieldName = $params['localField'];
18			else
19				$this->localFieldName = $name . '_id';
20			
21			$this->options = $params['options'];
22		}
23		else
24		{
25			if(isset($params['local_field']))
26				$this->localFieldName = $params['local_field'];
27			else
28				$this->localFieldName = $name . '_id';
29			
30			if(isset($params['option_table']))
31				$this->remoteTable = $params['option_table'];
32			else
33				$this->remoteTable = $name;
34			
35			if(isset($params['option_key_field']))
36				$this->remoteKeyField = $params['option_key_field'];
37			else
38				$this->remoteKeyField = 'id';
39
40			if(isset($params['option_value_field']))
41				$this->remoteValueField = $params['option_value_field'];
42			else
43				$this->remoteValueField = 'name';
44		}
45		
46	}
47	
48	public function isTiedToField($fieldName)
49	{
50		return $this->localFieldName == $fieldName;
51	}
52	
53	public function getOptions()
54	{
55		if($this->options)
56			return $this->options;
57		else
58			return SqlFetchSimpleMap("select {$this->remoteKeyField}, {$this->remoteValueField} from {$this->remoteTable}", $this->remoteKeyField, $this->remoteValueField, array());
59	}
60	
61	public function getInfo()
62	{
63		$options = $this->getOptions();
64		$field = $this->localFieldName;
65		return $options[$this->dbObject->$field];
66	}
67}