2016-07-05 6 views
1
The table 'category': 

cat_id category 
1  mobile 
2  watch 
..  .. 

und den 'category_brand' Aufnahmetisch:MySQL-Tabelle verbinden und eine Reihe

product_id cat_id 
1    1 
2    1 
3    2 
..    .. 

und ich habe diesen Code

public function actionEdit($id) 
    { 
     $sql="SELECT * FROM category_brand INNER JOIN category ON category_brand.cat_id=category.cat_id WHERE category_brand.product_id=$id"; 
     $editcat=Yii::$app->db->createCommand($sql)->queryOne(); 
     print_r($editcat->category);die; 
    } 

Ich versuche, die Kategorie des product_id retreice. Was mache ich hier falsch? Die Produkt-ID ist auto_increment-Wert. aber ich bin immer ‚Der Versuch, Eigentum von Nicht-Objekt zu erhalten‘

+2

Ihre Abfrage sieht gut aus, was den YII Code als Verdächtiger für den Fehler verlassen würde. –

Antwort

0

queryOne() kehrt array|false, nicht ActiveRecord Objekt

Blick auf Ergebnis print_r($editcat);

http://www.yiiframework.com/doc-2.0/yii-db-command.html#queryOne()-detail

Wenn Sie AR-Objekt als Ergebnis wollen , verwenden Sie die Funktionen

$editCategory = Category::find() 
     ->joinWith(['categoryBrand']) // hasOne relation declared in Category model 
     ->where([ 
     category_brand.product_id=:id //category_brand is table name 
     ]) 
     ->params([':id'=>$id]) 
     ->one(); 

oder t ry findBySql()

$editCategory = Category::findBySql("SELECT * FROM category INNER JOIN category_brand ON category_brand.cat_id=category.cat_id WHERE category_brand.product_id=:id",['id'=>$id])->one();

+0

ok. Ich hätte es auch mit 'print_r ($ editcat ['category']);' – micky