开发者

Using PHP and Kohana 3, can one recursively find and output any model's relationships?

Let's say we have a User Model that has many Posts.

The Posts Model has many Categories

The Posts Model also has many Comments.

How do we find dynamically, the relationships that the User Model has?

The idea is to make an admin backend to a site, where I can have one function, that when passed a model, can retrieve all data related to that Object, and show it based on the found data's relationship.

I'm guessing I need to access the Model Itself, and not an instance.

Any help开发者_如何学Go would be appreciated, thank you.


I Finally did this, so here's the code I used if anyone else wants it.

I make no claims to being a good programmer, I'm pretty new.

If someone actually was going to use this, and needed some help with my logic, I'll comment it out fully, but I'm running on empty tonight...

public function get_child_objects($recursive = 0, $init = FALSE)
{
    if ( ! $init )
    {
        Session::instance()->delete('model_register');
        $init = TRUE;
        $model_register = array();
    }
    else
    {
        $model_register = Session::instance()->get('model_register');
    }

    $return_array = array();

    $parent_id = $this->id;
    $parent_type = $this->_object_name;

    // prevent unending loops of many to many relationships
    if ( ! isset($model_register[$this->_object_name]) )
    {
        $model_register[$this->_object_name] = TRUE;
    }
    else
    {
        return;
    }

    Session::instance()->set('model_register', $model_register);

    if ( ! count($this->_has_many))
    {
        return;
    }

    // foreach _has_many relationship get objects
    foreach ($this->_has_many as $child_object_type => $child_object_data) {
        $child_object_type_singular = Inflector::singular( (string) $child_object_type);
        $child_object_type_plural = Inflector::plural( (string) $child_object_type);

        $many_to_many = FALSE;
        if (isset($child_object_data['through']))
        {
            $many_to_many = TRUE;
        }

        if (isset($child_object_data['model']))
        {
            $child_object_type = $child_object_data['model'];
        }

        if ( ! $many_to_many)
        {
            $child_objects = ORM::factory($child_object_type_singular)
                        ->where($parent_type.'_id', '=', $parent_id)
                        ->find_all();

            if ( ! count($child_objects))
            {
                continue;
            }
        }
        else
        {
            $child_object_type_plural = Inflector::plural( (string) $child_object_type);
            $child_objects = $this->$child_object_type_plural->find_all();

            if ( ! count($child_objects))
            {
                continue;
            }
        }

        $obj_arr = array();
        foreach ($child_objects as $child_object) 
        {
            if ( (bool) $recursive)
            {
                $recursive = $recursive - 1;
                $obj_arr[$child_object->id] = $child_object->get_child_objects($recursive, $init);
            }
            else
            {
                $obj_arr[$child_object->id] = NULL;
            }               
        }
        $return_array[$child_object_type_plural] = $obj_arr;


    } // foreach

    return $return_array;
} // get_child_objects
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜