问题
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