【问题标题】:right join of Yii2 frameworkYii2框架的右连接
【发布时间】:2019-10-24 09:05:21
【问题描述】:

查看

<?php 
$url = Yii::$app->urlManager->createUrl(['admin/sale/prduct']);
?>

查看页面中的脚本代码 通过 GET 发送 id

视图页面中的脚本

<script>

function ddlcategor(id){
$.ajax({
 type:'GET',
url:'<?=$url?>',
data:{id:id},
success: function(data){
$("#test").html(data);
}
});
}

</script>

控制器文档!

控制器

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

}

?>

错误

PHP 通知 - yii\base\ErrorException 输入获取属性 非对象

【问题讨论】:

    标签: yii2 right-join


    【解决方案1】:

    Yii::$app-&gt;db-&gt;createCommand() 返回数组。每行都是一个具有列名和值的关联数组。 如果选择不返回任何内容,则会收到一个空数组

    Yii::$app->db->createCommand()->queryAll();
    

    在您的示例中 $value 不是对象。它是数组:

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

    要调试 ajax 结果,我建议使用https://www.getpostman.com/ 使用此服务,您可以简单地跟踪粘贴到 ajax 的 url 返回的结果和错误。

    【讨论】:

      【解决方案2】:

      试试这个:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-15
        • 2022-07-12
        • 1970-01-01
        • 2016-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多