2016-05-13 1 views
0

私は私のquery.Kindlyで問題に直面していますが、「ヒット」を見ることができるように問題(私はKohanaのフレームワークで初心者です)

$posts->select(array(DB::expr('(SELECT COUNT(id_visit) FROM `oc2_visits` WHERE `oc2_post`.`id_post` = `oc2_visits`.`id_ad` AND `oc2_visits`.controller = "Blog" GROUP BY `oc2_visits`.`id_ad`)'), 'hits')); 

     //we sort all ads with few parameters 
     $posts = $posts->order_by('created','desc') 
         ->limit($pagination->items_per_page) 
         ->offset($pagination->offset) 
         ->limit(Theme::get('num_home_blog_posts', 4))->cached() 
         ->find_all(); 

を並べ替えるために私を助けるには、プロパティセットですDB:expr()で取得します。私の見解では、$ posts-> hitsにアクセスしようとしています。プロパティ。次に、出現しているヒットのプロパティが存在しません。 enter image description here

イメージが添付されています。私はコナナフレームワークの専門家ではありません。

+0

のは、あなたの「ポスト」モデルクラスファイルを見てみましょう。エラーメッセージ – pogeybait

+0

を参考にして、そこに「ヒット」フィールドがありません。バージョン2.0でKohanaを使用し始めたとき、私はORMをたくさん使っていましたが、あまりにも制限が厳しいと感じました。 ORMを中止し、DBインスタンスへの直接アプローチを使用しています:$ query = DB :: select( '*') - > from( 'oc2_visits) - > where(' id '、' = '、1); \t $ shipping = $ query-> as_object() - > execute() - > current(); – pogeybait

答えて

0

は、この非ORMのアプローチを試してみてください。

$query = DB::select(array(DB::Expr('COUNT(v.id_visit)'), 'hits')) 
    ->from(array('oc2_visits', 'v')) 
    ->join(array('oc2_post', 'p'), 'INNER') 
     ->on('p.id_post', '=', 'v.id_ad') 
    ->where('v.controller', '=', 'Blog') 
    ->group_by('v.id_ad') 
    ->order_by('v.created','desc') 
    ->limit($pagination->items_per_page) //why 2 limit statements? Oversight? 
    ->offset($pagination->offset) 
    ->limit(Theme::get('num_home_blog_posts', 4)); 
$posts = $query->execute(); 
//Or if you want objects use: 
$posts = $query->as_object()->execute();