Ciao a tutti, ho due tabelle in cui in una prendo i dati di una colonna e li uso in un’altra. Tuttavia, desidero che la tabella da cui viene preso il valore contenga una colonna “status” in cui viene automaticamente valutata come, ad esempio, “non disponibile”. Come spiegazione non è sicuramente delle migliori. Ecco lo screenshot:
Quindi, se utilizzo il PC001 su “Gestione PC”, questo deve risultare non disponibile nell’Elenco Numerazioni, il massimo sarebbe non farlo apparire anche nel menu a tendina nel form di Gestione PC.
app\models\Numeri.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 "{{%numeri}}".
*
* @property int $id_numeri
* @property string|null $numeri Numerazioni
* @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 $gestionalepc
*/
class Numeri extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{%numeri}}';
}
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 [
[['elenconumeri'], 'string', 'max' => 80],
[['elenconumeri'], 'unique'],
[['elenconumeri'], 'required'],
[['elenconumeri', 'status'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id_numeri' => Yii::t('app', 'Id Numeri'),
'elenconumeri' => Yii::t('app', 'Nr.'),
'status' => Yii::t('app', 'Status'),
'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']);
}
public function getGestionalepc()
{
return $this->hasMany(Gestionalepc::className(), ['numerazioni' => 'id_numeri']);
}
/**
* {@inheritdoc}
* @return NumeriQuery the active query used by this AR class.
*/
public static function find()
{
return new NumeriQuery(get_called_class());
}
}
app\controllers\NumeriController.php
namespace app\controllers;
use Yii;
use app\models\Numeri;
use app\models\NumeriSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* NumeriController implements the CRUD actions for Numeri model.
*/
class NumeriController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
'access' => [
'class' => \yii\filters\AccessControl::className(),
'rules' => [
[
'allow' => true,
'actions' => ['index', 'view'],
'roles' => ['visualizza-base']
],
[
'allow' => true,
'actions' => ['update'],
'roles' => ['aggiorna-base']
],
[
'allow' => true,
'actions' => ['create'],
'roles' => ['crea-base']
],
[
'allow' => true,
'actions' => ['delete'],
'roles' => ['elimina-base']
],
[
'allow' => false
]
]
]
];
}
/**
* Lists all Numeri models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new NumeriSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Numeri model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Numeri model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Numeri();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id_numeri]);
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing Numeri model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id_numeri]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing Numeri model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Numeri model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Numeri the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Numeri::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
}
}
app\views\numeri\index.php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use app\models\Gestionalepc;
/* @var $this yii\web\View */
/* @var $searchModel app\models\NumeriSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = Yii::t('app', 'Elenco Numerazioni');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="numeri-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_numeri',
'elenconumeri',
'status',
'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>
table "numeri" in mysql
CREATE TABLE `numeri` (
`id_numeri` int(11) NOT NULL,
`elenconumeri` varchar(80) DEFAULT NULL COMMENT 'Numerazioni',
`status` int(11) DEFAULT '0' COMMENT 'Status',
`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;
ALTER TABLE `numeri`
ADD PRIMARY KEY (`id_numeri`),
ADD UNIQUE KEY `numeri_UNIQUE` (`elenconumeri`),
ADD KEY `created_by` (`created_by`,`updated_by`),
ADD KEY `status` (`status`);
Ho provato anche a inserire sul model Numeri.php
class Numeri extends \yii\db\ActiveRecord
{
const STATUS_DISPONIBILE = 0;
const STATUS_INDISPONIBILE = 1;
Ma li mi fermo.
Grazie in anticipo.