How to define model fields with idiorm/granada without breaking the ORM functionality?

房东的猫 提交于 2019-12-14 04:11:32

问题


The PHP orm Granada based on Idiorm works the following way to retrieve fields from database:

class ORM {
  ...

  public function __get($key) {
    return $this->get($key);
  }
}

class ORMWrapper extends ORM {
  ...

  public function get($key) {
        if (method_exists($this, 'get_' . $key)) {
            return $this->{'get_' . $key}();
        } elseif (array_key_exists($key, $this->_data)) {
            return $this->_data[$key];
        }
        elseif (array_key_exists($key, $this->ignore)) {
            return $this->ignore[$key];
        }
        // and so on ...
  }

My problem is that if I define public $field in my model classes, the magic method __get is not called, and so the ORM does not retrieve the field from the database?

How can I

  • Be able to declare public $field in my model classes
  • Still call the magic getter if $field is undefined

At the same time?


回答1:


All I actually wanted to do is the have the autocompletion working on netbeans.

Just declaring my Model classes like that did the job:

/**
 * @property int $address_id
 * @property Address $address
 * @property String $name
 * ...
 */
class Activity extends Model {

    public function address() {
      return $this->belongs_to('Address');
    }

//...
}

This way I can do

$activity->address->name;

And I have the completion and the ORM both working.



来源:https://stackoverflow.com/questions/15159709/how-to-define-model-fields-with-idiorm-granada-without-breaking-the-orm-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!