【问题标题】:Kohana and ORM relationship has_many_throughKohana 和 ORM 的关系 has_many_through
【发布时间】:2013-09-16 22:39:24
【问题描述】:

我对 Kohana 3.3 和 ORM 关系 has_many_through 有疑问。我有两个模型

Model_Category

class Model_Category extends ORM {

    protected $_table_name = 'category';
    protected $_primary_key = 'category_id';
    protected $_has_many = array(
        'question' => array(
            'model' => 'Question',
            'through' => 'cat_question'
        ),
    );

}

Model_Question

class Model_Question extends ORM {

    protected $_table_name = 'question';
    protected $_primary_key = 'question_id';
    protected $_has_many = array(
        'category' => array(
            'model' => 'Category',
            'through' => 'cat_question'
        ),
    );

}
  • 在表cat_question中有两列,category_id, question_id,
  • 在表中question:question_id, title, content, date,
  • 在category:category_id, name

但它的效果并不好......当我这样做时

$orm = ORM::factory('Question')->find_all();
foreach($orm as $el) {
    var_dump($el->category->name);
}

他们告诉我 NULL,但我不知道为什么。

【问题讨论】:

    标签: php frameworks kohana kohana-orm kohana-3.3


    【解决方案1】:

    我处理这个, 问题模型应该是这样的:

    class Model_Question extends ORM {
    
        protected $_table_name = 'question';
        protected $_primary_key = 'question_id';
    
         protected $_has_many = array(
            'category' => array(
                'model' => 'Category',
                'through' => 'cat_question',
                'far_key' => 'category_id',
                'foreign_key' => 'question_id',
                ),
        );
      }
    

    和类别模型

    class Model_Category extends ORM {
    
        protected $_table_name = 'category';
        protected $_primary_key = 'category_id';
    
    
        protected $_has_many = array(
            'question' => array(
                'model' => 'Question', 
                'far_key' => 'question_id',
                'through' => 'cat_question',
                'foreign_key' => 'category_id'
                ),
        );
    
    }
    

    如果我们想要所有包含计数问题的类别,请执行以下操作:

      public function get_category_and_question() {
            $orm = ORM::factory('Category');
            $find = $orm->find_all();
            foreach ($find as $element) {
                $count = ORM::factory('Category', $element->category_id)->question->count_all();
                $new_array[] = array(
                    'name' => $element->name,
                    'id' => $element->category_id,
                    'how_much' => $count
                );
            }
            return $new_array;
        }
    

    我不太确定这是否真的很好解决但对我来说还不错。

    【讨论】:

      【解决方案2】:

      问题是,has_many_through 意味着多对多。所以一个类别包含多个问题,反之亦然。现在,如果您遵循 Kohana 的标准,您的数据库名称将是 categories, questions, categories_questions,名称将是复数,因此可以通过 categories 或 questions 访问。

      但你没有,你的代码需要如下所示

      $orm = ORM::factory('Question')->find_all();
      foreach($orm as $el)
      {
          $categories = $el->category->find_all();
          foreach ($categories as $category)
          {
              var_dump($category->name);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-27
        • 1970-01-01
        • 2011-03-24
        • 2011-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多