如何从 Yii2 Gridview 值调用模型函数
How to call model function from Yii2 Gridview value
我试图在 GridView 列值中显示模型数据,但它一直显示错误:调用字符串 .[=22 上的成员函数 getUSD() =]
我只想在该数据单元格中显示一个数据。谁能知道是什么问题?
型号代码:
public static function getUSD(){
$getUSD = Rate::find()
->select('rate')
->where(['currency_name' => 'USD'])
->orderBy('rate_id DESC')
->one();
return $getUSD;
}
GirdView 中的代码:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'summary' => '',
'columns' => [
[
'label' => 'USD',
'value' => function($model){
return $model->getUSD();
}
],
预期输出:
|USD|
| 4.102|
If you handle same data over and over again the same time, then I would consider calling them not statically and save (in between) results somewhere (in a property of the class for example), saving you from having too many queries.
喜欢你问题的答案你可以这样称呼它:
return $model::getUSD()->rate; //Call to a static method from current model
或
return 'USD ' . $model::getUSD()->rate; //Call to a static method from current model
就像开头描述的选项一样,你也可以这样做:
$model = new MyModel();
$rate = MyModel::getUSD()->rate;//get value of current rate
//Gridview
<?= GridView::widget([
'dataProvider' => $dataProvider,
'summary' => '',
'columns' => [
[
'label' => 'USD',
'value' => function($model) use ($rate){
return 'USD' . $rate; //Your initially stored rate
}
],
问题是 getUSD()
函数不是 return 字符串,return 是具有 rate
.
属性的对象
为了使其像您一样工作,您必须 return 直接将对象的值设置为:
public static function getUSD(){
$getUSD = Rate::find()
->select('rate')
->where(['currency_name' => 'USD'])
->orderBy('rate_id DESC')
->one();
return $getUSD ? $getUSD->rate : null;
}
现在你首先检查查询是否找到了东西,如果找到了东西然后你 return 属性 rate
.
那么 getUSD 是一个静态方法,所以你不应该像 $model->getUsd()
那样做,而是 $model::getUSD()
.
您 return 从函数中获取对象而不是实际的 rate
列值,您应该得到错误
htmlspecialchars() expects parameter 1 to be string, object given
而不是你提到的那个,因为 gridview 列希望你使用 return 字符串,并且在尝试使用 yii\grid\DataColumn::renderDataCellContent
渲染单元格时调用 yii\i18n\Formatter
渲染为在您的情况下找到对象的文本。
您还定义了函数 getter 并且 yii 表示
Nearly every core class in the Yii framework extends from
yii\base\BaseObject
or a child class. This means, that whenever you
see a getter or setter in a core class, you can use it like a
property.
并将函数更改为
public static function getUSD()
{
$rate = Rate::find()
->select('rate')
->where(['currency_name' => 'USD'])
->orderBy('rate_id DESC')
->one();
return null !== $rate ? $rate->rate : 'NA';
}
然后像下面这样称呼它。
echo GridView::widget(
[
'dataProvider' => $dataProvider,
'summary' => '',
'columns' => [
[
'label' => 'USD',
'value' => function ($model) {
return $model->USD;
},
],
],
]
);
如果您不想更改 getUSD()
函数,那么您可能需要在 gridview 中检查它,如下所示
echo GridView::widget(
[
'dataProvider' => $dataProvider,
'summary' => '',
'columns' => [
[
'label' => 'USD',
'value' => function ($model) {
return null !== $model->USD ? $model->USD->rate: 'NA';
},
],
],
]
);
我试图在 GridView 列值中显示模型数据,但它一直显示错误:调用字符串 .[=22 上的成员函数 getUSD() =] 我只想在该数据单元格中显示一个数据。谁能知道是什么问题?
型号代码:
public static function getUSD(){
$getUSD = Rate::find()
->select('rate')
->where(['currency_name' => 'USD'])
->orderBy('rate_id DESC')
->one();
return $getUSD;
}
GirdView 中的代码:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'summary' => '',
'columns' => [
[
'label' => 'USD',
'value' => function($model){
return $model->getUSD();
}
],
预期输出:
|USD|
| 4.102|
If you handle same data over and over again the same time, then I would consider calling them not statically and save (in between) results somewhere (in a property of the class for example), saving you from having too many queries.
喜欢你问题的答案你可以这样称呼它:
return $model::getUSD()->rate; //Call to a static method from current model
或
return 'USD ' . $model::getUSD()->rate; //Call to a static method from current model
就像开头描述的选项一样,你也可以这样做:
$model = new MyModel();
$rate = MyModel::getUSD()->rate;//get value of current rate
//Gridview
<?= GridView::widget([
'dataProvider' => $dataProvider,
'summary' => '',
'columns' => [
[
'label' => 'USD',
'value' => function($model) use ($rate){
return 'USD' . $rate; //Your initially stored rate
}
],
问题是 getUSD()
函数不是 return 字符串,return 是具有 rate
.
为了使其像您一样工作,您必须 return 直接将对象的值设置为:
public static function getUSD(){
$getUSD = Rate::find()
->select('rate')
->where(['currency_name' => 'USD'])
->orderBy('rate_id DESC')
->one();
return $getUSD ? $getUSD->rate : null;
}
现在你首先检查查询是否找到了东西,如果找到了东西然后你 return 属性 rate
.
那么 getUSD 是一个静态方法,所以你不应该像 $model->getUsd()
那样做,而是 $model::getUSD()
.
您 return 从函数中获取对象而不是实际的 rate
列值,您应该得到错误
htmlspecialchars() expects parameter 1 to be string, object given
而不是你提到的那个,因为 gridview 列希望你使用 return 字符串,并且在尝试使用 yii\grid\DataColumn::renderDataCellContent
渲染单元格时调用 yii\i18n\Formatter
渲染为在您的情况下找到对象的文本。
您还定义了函数 getter 并且 yii 表示
Nearly every core class in the Yii framework extends from
yii\base\BaseObject
or a child class. This means, that whenever you see a getter or setter in a core class, you can use it like a property.
并将函数更改为
public static function getUSD()
{
$rate = Rate::find()
->select('rate')
->where(['currency_name' => 'USD'])
->orderBy('rate_id DESC')
->one();
return null !== $rate ? $rate->rate : 'NA';
}
然后像下面这样称呼它。
echo GridView::widget(
[
'dataProvider' => $dataProvider,
'summary' => '',
'columns' => [
[
'label' => 'USD',
'value' => function ($model) {
return $model->USD;
},
],
],
]
);
如果您不想更改 getUSD()
函数,那么您可能需要在 gridview 中检查它,如下所示
echo GridView::widget(
[
'dataProvider' => $dataProvider,
'summary' => '',
'columns' => [
[
'label' => 'USD',
'value' => function ($model) {
return null !== $model->USD ? $model->USD->rate: 'NA';
},
],
],
]
);