PageRenderTime 36ms CodeModel.GetById 28ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/core/db/DbRelationshipBelongsTo.php

http://zoop.googlecode.com/
PHP | 40 lines | 35 code | 5 blank | 0 comment | 4 complexity | 322eea9e3b10e0739ab06bfc64303545 MD5 | raw file
 1<?php
 2class DbRelationshipBelongsTo extends DbRelationship
 3{
 4	private $owner;
 5	protected $localFieldName;
 6	protected $remoteClassName;
 7	protected $remoteFieldName;
 8	
 9	function __construct($name, $params, $dbObject)
10	{
11		parent::__construct($name, $params, $dbObject);
12		if(isset($params['class']))
13			$this->remoteClassName = $params['class'];
14		else
15			$this->remoteClassName = $name;
16		
17		if(isset($params['remote_field']))
18			$this->remoteFieldName = $params['remote_field'];
19		else
20			$this->remoteFieldName = 'id';
21		
22		if(isset($params['local_field']))
23			$this->localFieldName = $params['local_field'];
24		else
25			$this->localFieldName = DbObject::_getTableName($this->remoteClassName) . '_id';
26	}
27		
28	public function getInfo()
29	{
30		if(!$this->owner)
31		{
32			$remoteTableName = DbObject::_getTableName($this->remoteClassName);
33			$sql = "select * from $remoteTableName where {$this->remoteFieldName} = :id:int";
34			$row = $this->dbObject->getDb()->fetchRow($sql, array($this->remoteFieldName => $this->dbObject->getField($this->localFieldName)));
35			$this->owner = new $this->remoteClassName($row);
36		}
37		
38		return $this->owner;
39	}
40}