使用 PHP 和 Kohana 3,可以递归地查找并输出任何模型的关系吗?

发布于 2024-09-27 16:34:27 字数 239 浏览 3 评论 0原文

假设我们有一个包含许多帖子的用户模型。

帖子模型有很多类别

帖子模型也有很多评论。

我们如何动态地找到用户模型所具有的关系?

这个想法是为一个网站创建一个管理后端,我可以在其中拥有一个函数,当传递模型时,可以检索所有数据与该对象相关,并根据找到的数据的关系来显示它。

我猜我需要访问模型本身,而不是实例。

任何帮助将不胜感激,谢谢。

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 would be appreciated, thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月寒剑心 2024-10-04 16:34:27

我终于做到了,所以如果其他人想要的话,这是我使用的代码。

我并不声称自己是一名优秀的程序员,我还是个新手。

如果有人真的要使用这个,并且需要一些关于我的逻辑的帮助,我会完全注释掉它,但今晚我空着……

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文