Hi @softark, the variable $ _diff is not valued and gives me an error.
In the meantime, I redone everything in this way, also because I realized that the column ‘qusistop’ (Used Quantity) in the mysql table always remained NULL.
So I did it again:
CREATE TABLE `sistop` (
`id_sistop` int(11) NOT NULL,
`elencosistop` varchar(255) DEFAULT NULL COMMENT 'Sistema Operativo',
`licsistop` varchar(80) DEFAULT NULL COMMENT 'Licenza Uso',
`qdsistop` int(5) NOT NULL DEFAULT '0' COMMENT 'Q.tà disp.',
`qusistop` int(5) NOT NULL DEFAULT '0' COMMENT 'Q.ta util.',
`qrsistop` int(5) NOT NULL DEFAULT '0' COMMENT 'Q.tà rim.',
`created_at` timestamp NULL DEFAULT NULL COMMENT 'Data inserimento',
`created_by` int(11) DEFAULT NULL COMMENT 'Inserito da',
`updated_at` timestamp NULL DEFAULT NULL COMMENT 'Data modifica',
`updated_by` int(11) DEFAULT NULL COMMENT 'Modificato da'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
app\models\Sistop.php
namespace app\models;
use Da\User\Model\User;
use Yii;
use yii\db\Expression;
use yii\behaviors\TimestampBehavior;
use yii\behaviors\BlameableBehavior;
/**
* This is the model class for table "{{%sistop}}".
*
* @property int $id_sistop
* @property string|null $elencosistop Sistema Operativo
* @property string|null $licsistop Licenza Uso
* @property int|null $qdsistop Q.tà disp.
* @property int|null $qusistop Q.tà rim.
* @property string|null $created_at Data inserimento
* @property int|null $created_by Inserito da
* @property string|null $updated_at Data modifica
* @property int|null $updated_by Modificato da
*
* @property Gestionalepc[] $gestionalepcs
*/
class Sistop extends \yii\db\ActiveRecord
{
private $Gestionalepc;
/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{%sistop}}';
}
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => new Expression('NOW()'),
],
[
'class' => BlameableBehavior::className(),
'createdByAttribute' => 'created_by',
'updatedByAttribute' => 'updated_by',
],
];
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['elencosistop'], 'string', 'max' => 255],
[['licsistop'], 'string', 'max' => 80],
[['qdsistop', 'qusistop','qrsistop'], 'integer'],
[['elencosistop'], 'unique'],
[['elencosistop','qdsistop', 'licstop'], 'required'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id_sistop' => Yii::t('app', 'ID'),
'elencosistop' => Yii::t('app', 'Sistema Operativo'),
'licsistop' => Yii::t('app', 'Product Key'),
'qdsistop' => Yii::t('app', 'Q.tà disp.'),
'qusistop' => Yii::t('app', 'Q.tà util.'),
'qrsistop' => Yii::t('app', 'Q.tà rim.'),
'created_at' => Yii::t('app', 'Data inserimento'),
'created_by' => Yii::t('app', 'Inserito da'),
'updated_at' => Yii::t('app', 'Data modifica'),
'updated_by' => Yii::t('app', 'Modificato da'),
];
}
public function getCreatore()
{
return $this->hasOne(User::className(), ['id' => 'created_by']);
}
public function getModificatore()
{
return $this->hasOne(User::className(), ['id' => 'updated_by']);
}
/**
* Gets query for [[Gestionepcs]].
*
* @return \yii\db\ActiveQuery|GestionalepcQuery
*/
public function getGestionalepcs()
{
return $this->hasMany(Gestionalepc::className(), ['os' => 'id_sistop']);
}
/**
* {@inheritdoc}
* @return SistopQuery the active query used by this AR class.
*/
public static function find()
{
return new SistopQuery(get_called_class());
}
}
app\models\SistopSearch.php
namespace app\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Sistop;
class SistopSearch extends Sistop
{
public function rules()
{
return [
[['id_sistop', 'qdsistop', 'qusistop', 'qrsistop'], 'integer'],
[['elencosistop', 'licsistop'], 'safe'],
];
}
public function scenarios()
{
return Model::scenarios();
}
public function search($params)
{
$query = Sistop::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id_sistop' => $this->id_sistop,
'qdsistop' => $this->qdsistop,
'qusistop' => $this->qusistop,
'created_at' => $this->created_at,
'created_by' => $this->created_by,
'updated_at' => $this->updated_at,
'updated_by' => $this->updated_by,
]);
$query->andFilterWhere(['like', 'elencosistop', $this->elencosistop])
->andFilterWhere(['like', 'licsistop', $this->licsistop]);
return $dataProvider;
}
}
app\views\sistop\index.php
use app\models\Sistop;
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
/* @var $this yii\web\View */
/* @var $searchModel app\models\SistopSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = Yii::t('app', 'Elenco Sistemi Operativi');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="sistop-index">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a(Yii::t('app', 'Aggiungi nuovo'), ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?php Pjax::begin(); ?>
<?php // echo $this->render('_search', ['model' => $searchModel]);
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
//'id_sistop',
'elencosistop',
'licsistop',
'qdsistop',
[
'attribute' => 'qusistop',
'value' => function ($sistopModel) {
return $sistopModel->getGestionalepcs()->count();
}
],
[
'attribute' => 'qrsistop',
'value' => $data->qdsistop - $data->getGestionalepcs()->count()
],
//'created_at:datetime',
//[
// 'attribute' => 'created_by',
// 'value' => 'creatore.username',
//],
//'updated_at:datetime',
// [
// 'attribute' => 'updated_by',
// 'value' => 'modificatore.username',
//],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>
</div>
app\views\sistop\view.php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model app\models\Sistop */
$this->title = $model->elencosistop;
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Elenco Sistemi Operativi'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
?>
<div class="sistop-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a(Yii::t('app', 'Modifica'), ['update', 'id' => $model->id_sistop], ['class' => 'btn btn-primary']) ?>
<?= Html::a(Yii::t('app', 'Elimina'), ['delete', 'id' => $model->id_sistop], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'),
'method' => 'post',
],
]) ?>
<?= Html::a(Yii::t('app', 'Aggiungi'), ['create'], ['class' => 'btn btn-success']) ?>
<?= Html::a(Yii::t('app', 'Elenco Completo'), ['index'], ['class' => 'btn btn-warning']) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
//'id_sistop',
'elencosistop',
'licsistop',
'qdsistop',
[
'attribute' => 'qusistop',
'value' => function ($sistopModel) {
return $sistopModel->getGestionalepcs()->count();
}
],
'qrsistop',
'created_at:datetime',
'creatore.username',
'updated_at:datetime',
'modificatore.username',
],
]) ?>
</div>
In this way I solved the problem on the column ‘qusistop’ of the table in mysql. I have not resolved however on ‘qrsistop’ because it gives me an error:
PHP Notice - yii \ base \ ErrorException Undefined variable: data
And if i comment (in app\views\sistop\index.php)
//'value' => $data->qdsistop - $data->getGestionalepcs()->count()
Not appare error but not working ‘qrsistop’ column.
I don’t know where I mistake.