/framework/core/db/DbRelationshipOptions.php
PHP | 67 lines | 57 code | 10 blank | 0 comment | 8 complexity | 587803b3aab6a90a56dd6a2790f200cf MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
- <?php
- class DbRelationshipOptions extends DbRelationship
- {
- protected $options;
- protected $localField;
- protected $remoteTable;
- protected $remoteKeyField;
- protected $remoteValueField;
-
- function __construct($name, $params, $dbObject)
- {
- parent::__construct($name, $params, $dbObject);
-
- if(isset($params['options']))
- {
- if(isset($params['localField']))
- $this->localFieldName = $params['localField'];
- else
- $this->localFieldName = $name . '_id';
-
- $this->options = $params['options'];
- }
- else
- {
- if(isset($params['local_field']))
- $this->localFieldName = $params['local_field'];
- else
- $this->localFieldName = $name . '_id';
-
- if(isset($params['option_table']))
- $this->remoteTable = $params['option_table'];
- else
- $this->remoteTable = $name;
-
- if(isset($params['option_key_field']))
- $this->remoteKeyField = $params['option_key_field'];
- else
- $this->remoteKeyField = 'id';
- if(isset($params['option_value_field']))
- $this->remoteValueField = $params['option_value_field'];
- else
- $this->remoteValueField = 'name';
- }
-
- }
-
- public function isTiedToField($fieldName)
- {
- return $this->localFieldName == $fieldName;
- }
-
- public function getOptions()
- {
- if($this->options)
- return $this->options;
- else
- return SqlFetchSimpleMap("select {$this->remoteKeyField}, {$this->remoteValueField} from {$this->remoteTable}", $this->remoteKeyField, $this->remoteValueField, array());
- }
-
- public function getInfo()
- {
- $options = $this->getOptions();
- $field = $this->localFieldName;
- return $options[$this->dbObject->$field];
- }
- }