where('title_'.I18n::$lang, '<>', '') ->and_whe" /> where('title_'.I18n::$lang, '<>', '') ->and_whe" /> where('title_'.I18n::$lang, '<>', '') ->and_whe"/>

Kohana combine orm factories

46 views Asked by At

I have two factories "News" and "Photoset". Can I somehow combine them?

$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();
2

There are 2 answers

0
Starina On BEST ANSWER

Okey, just converted them to array and merged.

$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);
0
bato3 On

What you want to do is the equivalent of UNION from SQL. ORMs aren't made for that...

The second thing is that mapping SQL results to objects in Kohana is slow. (But you don't seem to do that.) If you only need to display the results, it's better to use the query builder.


$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();