Hamid Farrokhi
Hamid Farrokhi

Reputation: 113

right join of Yii2 framework

view

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

script code in view page send id whit GET

script in view page

<script>

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

</script>

controller document !

controller

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

}

?>

Error

PHP Notice - yii\base\ErrorException typing to get property of non-object

Upvotes: 0

Views: 355

Answers (2)

Sergio Codev
Sergio Codev

Reputation: 104

Yii::$app->db->createCommand() returns array. Each row is an associative array with column names and values. if the selection returns nothing, an empty array will be received.

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

In your example $value not objact. It is array:

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

To debug ajax result I recommend using https://www.getpostman.com/ Using this service, you can track results and errors returned by url pasted to ajax simply.

Upvotes: 1

Jyoti Garg
Jyoti Garg

Reputation: 93

Try this:

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

Upvotes: 0

Related Questions