Yii2框架的右连接
right join of Yii2 framework
查看
<?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 Notice - yii\base\ErrorException typing to get property of
non-object
Yii::$app->db->createCommand()
returns数组。每行都是一个包含列名和值的关联数组。
如果选择 returns 什么都没有,将收到一个 空数组 。
Yii::$app->db->createCommand()->queryAll();
在您的示例中 $value 不是 objact。它是数组:
$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/
使用此服务,您可以简单地跟踪 url 返回的结果和错误粘贴到 ajax。
试试这个:
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;
}
查看
<?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 Notice - yii\base\ErrorException typing to get property of non-object
Yii::$app->db->createCommand()
returns数组。每行都是一个包含列名和值的关联数组。
如果选择 returns 什么都没有,将收到一个 空数组 。
Yii::$app->db->createCommand()->queryAll();
在您的示例中 $value 不是 objact。它是数组:
$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/ 使用此服务,您可以简单地跟踪 url 返回的结果和错误粘贴到 ajax。
试试这个:
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;
}