Starina
Starina

Reputation: 11

Kohana combine orm factories

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

Upvotes: 0

Views: 47

Answers (2)

bato3
bato3

Reputation: 2815

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

Upvotes: 0

Starina
Starina

Reputation: 11

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

Upvotes: 0

Related Questions