Yii2框架的右连接 看法

Yii2框架的右连接 看法,yii2,right-join,Yii2,Right Join,查看页面中的脚本代码 把身份证寄给我 查看页面中的脚本 控制器文档 控制器 错误 PHP通知-yii\base\ErrorException键入以获取的属性 非对象 Yii::$app->db->createCommand返回数组。每行都是一个具有列名和值的关联数组。 如果选择不返回任何内容,将接收空数组 <?php public function actionProduct($id){ $products = Yii::db->createCommand('select

查看页面中的脚本代码 把身份证寄给我

查看页面中的脚本 控制器文档

控制器 错误 PHP通知-yii\base\ErrorException键入以获取的属性 非对象

Yii::$app->db->createCommand返回数组。每行都是一个具有列名和值的关联数组。 如果选择不返回任何内容,将接收空数组

<?php

public function actionProduct($id){
    $products = Yii::db->createCommand('select products.* from products right join (select * from product_category where product_category.cat_pro_id ='.$id.') as t on(products.id = t.product_id)')->queryAll();

$option ='';
echo "<option>select ...</option>";
foreach($products  as $value){
  $option.="<option value=$value->id>$value->title</option>";
}

return $option;

}

?>
在您的示例中,$value不是objact。它是一个数组:

Yii::$app->db->createCommand()->queryAll();
要调试ajax结果,我建议使用 使用此服务,您可以简单地跟踪粘贴到ajax的url返回的结果和错误。

尝试以下方法:

$products = Yii::db->createCommand('select products.* from products right join (select * from product_category where product_category.cat_pro_id ='.$id.') as t on(products.id = t.product_id)')->queryAll();

$option ='';
//No needed in this variant
//echo "<option>select ...</option>";
If(!empty($products)){
   foreach($products  as $value){
      $option.="<option value=$value['id']>$value['title']</option>";
   }
}else{
    $option.= "<option selected disabled>No results!</option>"
}
return $option;
Yii::$app->db->createCommand()->queryAll();
$products = Yii::db->createCommand('select products.* from products right join (select * from product_category where product_category.cat_pro_id ='.$id.') as t on(products.id = t.product_id)')->queryAll();

$option ='';
//No needed in this variant
//echo "<option>select ...</option>";
If(!empty($products)){
   foreach($products  as $value){
      $option.="<option value=$value['id']>$value['title']</option>";
   }
}else{
    $option.= "<option selected disabled>No results!</option>"
}
return $option;
public function actionProduct($id){
      $commands = Yii::$app->getDb();
      $products = $commands->createCommand('select products.* from products right join (select * from product_category where product_category.cat_pro_id ='.$id.') as t on(products.id = t.product_id)')->queryAll();    
        $option ='';
        echo "<option>select ...</option>";
        foreach($products  as $value){
          $option.="<option value=$value['id']>$value['title']</option>";
        }    
    return $option;    
    }