在yii2中乘以两列

时间:2016-11-01 02:53:31

标签: yii2

我有两张桌子 - 1. rmtemplate

id, productname, rmname, qty

2。 RMNAME

rmid,rmname,min,unitcost

我要运行的查询是 -

SELECT id,productname,rmtemplate.rmname,qty,rmtemplate.qty * rmname.unitcost as cost 
FROM `rmtemplate` 
left join rmname on rmtemplate.rmname = rmname.rmname

在Yii2中,我已经加入了两张表,没有任何问题。单位成本没有任何问题。但是,当我尝试将rmtemplate.rmname乘以rmname.unitcost时,它会返回错误 - 'Getting unknown property: frontend\models\Rmtemplate::cost'

RmtemplateSearch.php -

public $cost;
public function rules()
    {
        return [
            [['id'], 'integer'],
            [['productname', 'rmname', 'qty', 'unitcost','cost'], 'safe'],
        ];
    }
$query = Rmtemplate::find()
        ->joinWith(['unitcost'])
        ->select(['id','productname','rmtemplate.rmname','qty', '(rmtemplate.qty * rmname.unitcost) as cost']);

Rmtemplate模型

public function getUnitcost()
    {
        return $this->hasOne(Rmname::className(), ['rmname' => 'rmname']);
    }

Rmtemplate index.php

<?php

use yii\helpers\Html;
use kartik\grid\GridView;
use kartik\select2\Select2;
use yii\helpers\ArrayHelper;
use frontend\models\Rmtemplate;

/* @var $this yii\web\View */
/* @var $searchModel frontend\modules\rmprod\models\RmtemplateSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Rmtemplates';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="rmtemplate-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?= Html::a('Create Rmtemplate', ['create'], ['class' => 'btn btn-success']) ?>
    </p>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'kartik\grid\CheckboxColumn'],

            'id',
            //'productname',
            [
                'attribute'=>'productname',
                'filterType'=>GridView::FILTER_SELECT2,
                'filter'=>ArrayHelper::map(Rmtemplate::find()->orderBy(['productname' => SORT_ASC])->asArray()->all(), 'productname', 'productname'),
                'filterWidgetOptions'=>[
                'pluginOptions'=>['allowClear'=>true],
                                    ],
                'filterInputOptions'=>['placeholder'=>'Charge Name'],
            ],
            'rmname',
            'qty',
            [
             'attribute' => 'unitcost',
             'value' => 'unitcost.unitcost',
            ],
            'cost',

            ['class' => 'kartik\grid\ActionColumn'],
        ],
    ]); ?>
</div>

如果需要进一步输入,请告诉我。

0 个答案:

没有答案