0

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Benjamin Crouzier
  • 40,265
  • 44
  • 171
  • 236
  • Relevant: http://stackoverflow.com/questions/4713680/php-get-and-set-magic-methods. If `__get` was called each time, I could override the `ORMWrapper::get` function. But it's not the case. – Benjamin Crouzier Mar 01 '13 at 14:18

1 Answers1

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.

Benjamin Crouzier
  • 40,265
  • 44
  • 171
  • 236