ActiveRecord's relation doesn't resolve when being used without :: operator

Hi, please see this Code:


$posts = Post::model()->findAll();

foreach($posts as $post) echo $post->author;


works fine, but :


$post = new Post();

$posts = $post->findAll();

foreach($posts as $post) echo $post->author;


says is not defined.

Unfortunately even this won’t work:


$post = new Post();

$posts = $post->with('author')->findAll();

foreach($posts as $post) echo $post->author;


I need to have this function because a $dynamic_var::model()->findAll() only works in PHP >5.3 (AFAIK!),

and the $obj = new $dynamic_var; $objects = $dynamic_var->findAll() isn’t able to handle relations.

Or am I doing something wrong/is there another way to accomplish this?

Thanks in advance

I did some investigation…

Scenario: grandparent 1:n to parent 1:n to child


$var_i_want_to_fetch = "parent.grandparent";

$childobj = new Child();

$childobjs = $childobj->findAll();

echo $childobjs[0]->$var_i_want_to_fetch;


This doesn’t work because he translates this into


but he could be more intelligent and translate this into


maybe we can make the magic _get() function of ActiveRecord a little bit more intelligent and allow for parent.grandparent syntax?

My Investigations went further… :)

i would suggest something like this:



   * PHP getter magic method.

   * This method is overridden so that AR attributes can be accessed like properties.

   * @param string property name

   * @return mixed property value

   * @see getAttribute


  public function __get($name)



      return $this->_attributes[$name];

    else if(isset($this->getMetaData()->columns[$name]))

      return null;

    else if(isset($this->_related[$name]))

      return $this->_related[$name];

    else if(isset($this->getMetaData()->relations[$name]))

      return $this->getRelated($name);

    else if(strstr($name, '.') != FALSE) {

      $data = explode('.', $name);




      return parent::__get($name);


qiang: what do you think about this small but useful addition?

$objects = CActiveRecord::model($dynamic_var)->findAll();