/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
  2. class DbRelationshipBelongsTo extends DbRelationship
  3. {
  4. private $owner;
  5. protected $localFieldName;
  6. protected $remoteClassName;
  7. protected $remoteFieldName;
  8. function __construct($name, $params, $dbObject)
  9. {
  10. parent::__construct($name, $params, $dbObject);
  11. if(isset($params['class']))
  12. $this->remoteClassName = $params['class'];
  13. else
  14. $this->remoteClassName = $name;
  15. if(isset($params['remote_field']))
  16. $this->remoteFieldName = $params['remote_field'];
  17. else
  18. $this->remoteFieldName = 'id';
  19. if(isset($params['local_field']))
  20. $this->localFieldName = $params['local_field'];
  21. else
  22. $this->localFieldName = DbObject::_getTableName($this->remoteClassName) . '_id';
  23. }
  24. public function getInfo()
  25. {
  26. if(!$this->owner)
  27. {
  28. $remoteTableName = DbObject::_getTableName($this->remoteClassName);
  29. $sql = "select * from $remoteTableName where {$this->remoteFieldName} = :id:int";
  30. $row = $this->dbObject->getDb()->fetchRow($sql, array($this->remoteFieldName => $this->dbObject->getField($this->localFieldName)));
  31. $this->owner = new $this->remoteClassName($row);
  32. }
  33. return $this->owner;
  34. }
  35. }