【问题标题】:Kohana combine orm factoriesKohana 联合 orm 工厂
【发布时间】:2022-12-29 18:45:06
【问题描述】:

我有两个工厂“News”和“Photoset”。我能以某种方式将它们结合起来吗?

$news = ORM::factory('News')
        ->where('title_'.I18n::$lang, '<>', '')
        ->and_where('image', '<>', '')
        ->and_where('published', '=', 1)
        ->and_where('is_slider', '=', 1)
        ->and_where('date', '<', DB::expr('UNIX_TIMESTAMP()'))
        ->order_by('date','desc')
        ->limit(4)
        ->find_all();

$photoset = ORM::factory('Photoset')
        ->where('name_'.I18n::$lang, '<>', '')
        ->and_where('published', '=', 1)
        ->and_where('is_slider', '=', 0)
        ->order_by('date','desc')
        ->limit(4)
        ->find_all();

【问题讨论】:

    标签: orm factory kohana


    【解决方案1】:

    好的,只是将它们转换为数组并合并。

    $news = ORM::factory('News')
            ->where('title_'.I18n::$lang, '<>', '')
            ->and_where('image', '<>', '')
            ->and_where('published', '=', 1)
            ->and_where('is_slider', '=', 1)
            ->and_where('date', '<', DB::expr('UNIX_TIMESTAMP()'))
            ->order_by('date','desc')
            ->limit(4)
            ->find_all()
            ->as_array();
    
    $photoset = ORM::factory('Photoset')
            ->where('name_'.I18n::$lang, '<>', '')
            ->and_where('published', '=', 1)
            ->and_where('is_slider', '=', 0)
            ->order_by('date','desc')
            ->limit(4)
            ->find_all()
            ->as_array();
    
    $newArray = Arr::merge($news, $photoset);
    

    【讨论】:

      【解决方案2】:

      你想要做的是 SQL 中的 UNION。 ORM 不是为此而生的……

      第二件事是将 SQL 结果映射到 Kohana 中的对象很慢。 (但你似乎并没有那样做。)如果你只需要显示结果,最好使用查询生成器。

      
      $q2 = DB::SELECT('id', DB::expr('set' AS 't'))->from('photoset')
              ->where('name_'.I18n::$lang, '<>', '')
              ->and_where('published', '=', 1)
              ->and_where('is_slider', '=', 0)
              ->order_by('date','desc')
              ->limit(4);
      $q = DB::SELECT('id', DB::expr('news' AS 't'))->from('news')
              ->where('title_'.I18n::$lang, '<>', '')
              ->and_where('image', '<>', '')
              ->and_where('published', '=', 1)
              ->and_where('is_slider', '=', 1)
              ->and_where('date', '<', DB::expr('UNIX_TIMESTAMP()'))
              ->order_by('date','desc')
              ->limit(4)
           ->union($q2, TRUE);
      $newArray = $q->execute()->as_array();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-11
        • 2011-10-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多