我在模型中声明了变量状态。状态是gridview中的一列,使用以下内容填充:
[
'attribute' => 'status',
'contentOptions' => ['class' => 'text-center'],
'value' => function($model){
if($model->nginSum != $model->sapValue){
return 'NOK';
} else {
return 'OK';
}
},
],
我想用数字os OKs和NOK来计算与计数器相同和不同的条目。
我尝试在搜索模型中声明一个函数:
public function getStatus()
{
return $this->status;
}
并且在视野中我这样做了:
$equal = 0;
$different = 0;
$stat = new EtuLojaSearch;
if ($stat->getStatus() == 'OK') {
$equal += 1;
} else {
$different += 1;
}
但它返回null。
我做错了什么?
编辑: 模型中的功能:
public function getStatus()
{
if($this->nginSum != $this->sapValue){
return 'NOK';
} else {
return 'OK';
}
}
在视图中:
$iguais = 0;
$diferentes = 0;
$stat = new EtuLojaSearch;
if ($stat->status == 'OK') {
$iguais += 1;
} else {
$diferentes += 1;
}
编辑2:
状态是使用getStatus()
函数绑定到模型。
模型
class EtuLoja extends \yii\db\ActiveRecord
{
public $nginSum;
public $sapValue;
public $status;
public $Data_Sap;
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'etu_loja';
}
public function getStatus()
{
if($this->nginSum != $this->sapValue){
return 'NOK';
} else {
return 'OK';
}
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id_master', 'nginSum', 'sapValue'], 'integer'],
[['user'], 'required'],
[['user', 'status'], 'string', 'max' => 100],
[['Atendedora', 'Loja', 'Obs'], 'string', 'max' => 255],
[['id_master'], 'exist', 'skipOnError' => true, 'targetClass' => MasterLoja::className(), 'targetAttribute' => ['id_master' => 'id']],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'ID' => 'ETU(Slave)',
'id_master' => 'Master',
'master.regiao' => 'Master/Região',
'user' => 'User',
'Atendedora' => 'Atendedora',
'Loja' => 'Loja',
'Obs' => 'Obs',
'nginValue' => 'Valor Ngin',
'status' => 'Estado',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getMaster()
{
return $this->hasOne(MasterLoja::className(), ['id' => 'id_master']);
}
public function getNginAgentDetail()
{
return $this->hasMany(NginAgentDetail::className(),['CLIENT_ID' => 'ID']);
}
public function getSap()
{
return $this->hasMany(Sap::className(),['Criado_por' => 'user']);
}
}
搜索模型:
class EtuLojaSearch extends EtuLoja
{
public $regiao;
public $nginValue;
public $sapValue;
public $operationDate;
public $from_date;
public $to_date;
public $Data_Sap;
public function rules()
{
return [
[['ID'], 'integer'],
[['Atendedora', 'Loja', 'Obs', 'id_master', 'regiao', 'user', 'nginValue', 'sapValue', 'operationDate', 'status', 'from_date', 'to_date', 'Data_Sap'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = EtuLoja::find();
// add conditions that should always apply here
$query->joinWith('master');
// $query->joinWith('nginAgentDetail');
// $query->joinWith('sap');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->sort->attributes['Data_Sap'] = [
'asc' => ['Data_Sap' => SORT_ASC],
'desc' => ['Data_Sap' => SORT_DESC],
];
$dataProvider->sort->attributes['regiao'] = [
// The tables are the ones our relation are configured to
// in my case they are prefixed with "tbl_"
'asc' => ['master_loja.regiao' => SORT_ASC],
'desc' => ['master_loja.regiao' => SORT_DESC],
];
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'etu_loja.ID' => $this->ID,
// 'id_master' => $this->id_master,
]);
$query->andFilterWhere(['like', 'Atendedora', $this->Atendedora])
->andFilterWhere(['like', 'Loja', $this->Loja])
->andFilterWhere(['like', 'Obs', $this->Obs])
->andFilterWhere(['like', 'user', $this->user]);
if ($this->status === 'OK') {
$query->andWhere('nginSum = sapValue');
} elseif ($this->status === 'NOK') {
$query->andWhere('nginSum <> sapValue');
}
if(" " !== $this->operationDate) {
$query->andFilterWhere(['master_loja.regiao'=>$this->regiao]);
}
return $dataProvider;
}
}
答案 0 :(得分:0)
使用以下内容更新您的getStatus
方法。
public function getStatus()
{
if($this->nginSum != $this->sapValue){
return 'NOK';
} else {
return 'OK';
}
}
当获取活动记录时,它将使用status属性绑定值(OK,NOK),稍后您可以以任何方式使用该值,例如$model->status
。使用这种方法,您的属性呈现代码将如下所示。
[
'attribute' => 'status',
'contentOptions' => ['class' => 'text-center'],
'value' => function($model){
return $model->status;
},
],
但要计算不同的状态值,您必须遍历所有模型i-e
$equal = 0;
$different = 0;
foreach($dataProvider->getModels() as $item){
if ($item->status == 'OK') {
$equal += 1;
} else {
$different += 1;
}
}