问题描述
我有4张桌子
设备: 笔记本电脑:ID-PK,laptop_model,laptop_series,laptop_date_acq,状态;
显示:ID-PK,display_model,display_series,display_date_acq,状态;
电话:ID-PK,电话型号,电话系列,电话日期,状态;
UserEquipmentMapping:ID-PK,laptop_id(从表笔记本电脑到FK的ID),display_id(从表显示到FK的ID),phone_id(从表Phone的FK到ID),开始日期,结束日期。
对于每个用户,我正在使用start_date和end_date映射唯一的设备,因此,现在在userequipmentmapping / index中,我为一个用户获得了4行,如下所示:
USER_X-display_model-NULL-NULL-START_DATE-END_DATE;
USER_X-display_model-NULL-NULL-START_DATE-NULL;
USER_X-NULL-手机型号-NULL-START_DATE-NULL;
USER_X-NULL-NULL-笔记本电脑型号-START_DATE-NULL;
我希望我的grid_view中的一位用户使用没有end_date的设备。
USER_X-disPLAY_MODEL-PHONE_MODEL-LAPTOP_MODEL(没有end_date的设备,因为带有end_date的设备不再可用),并且每个用户可以查看分配的所有设备,带有或不带有end_date。
作为yii2的初学者,任何指南都对您有很大帮助。预先谢谢你!
代码如下:
控制器:
<?PHP
namespace app\controllers;
use Yii;
use app\models\UserEquipmentMapping;
use app\models\UserequipmentmappingSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\models\User;
use app\models\Laptop;
use app\models\Phone;
use app\models\display;
/**
* UserequipmentmappingController implements the CRUD actions for UserEquipmentMapping model.
*/
class UserequipmentmappingController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),'actions' => [
'delete' => ['POST'],],];
}
/**
* Lists all UserEquipmentMapping models.
* @return mixed
*/
public function actionIndex()
{
$usermodel = new UserEquipmentMapping();
$userquery = $usermodel->getUsers();
$displaymodel = new UserEquipmentMapping();
$displayquery = $displaymodel->getdisplays();
$phonemodel = new UserEquipmentMapping();
$phonequery = $phonemodel->getPhones();
$laptopmodel = new UserEquipmentMapping();
$laptopquery = $laptopmodel->getLaptops();
#foreach($query as $q)
# print_r($q);
#
# die;
$searchModel = new UserequipmentmappingSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index',[
'searchModel' => $searchModel,'dataProvider' => $dataProvider,'userquery' => $userquery,'displayquery' => $displayquery,'laptopquery'=> $laptopquery,'phonequery'=> $phonequery,]);
}
/**
* displays a single UserEquipmentMapping 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 UserEquipmentMapping model.
* If creation is successful,the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new UserEquipmentMapping();
$usermodel = User::find()->all();
$laptopmodel = Laptop::find()->all();
$phonemodel = Phone::find()->all();
$displaymodel = display::find()->all();
#die;
#print_r(Yii::$app->request->post()); die;
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
//code
#foreach($laptopmodel as $m){
#print_r($m->status );
#print_r($model->load(Yii::$app->request->post()));
#print_r($model);
#print_r($model->laptop_id);
#print_r($laptopmodel);
#die;
if($model->laptop_id)
{
$laptop = Laptop::findOne($model->laptop_id);
$laptop->status = Laptop::STATUS_ASSIGNED;
$laptop->save();
}
if($model->display_id)
{
$display = display::findOne($model->display_id);
$display->status = display::STATUS_ASSIGNED;
$display->save();
}
if($model->phone_id)
{
$phone = Phone::findOne($model->phone_id);
$phone->status = Phone::STATUS_ASSIGNED;
$phone->save();
}
$model->save();
return $this->redirect(['index','id' => $model->id]);
}
return $this->render('create',[
'model' => $model,'usermodel' => $usermodel,'laptopmodel' => $laptopmodel,'phonemodel' => $phonemodel,'displaymodel' => $displaymodel,]);
}
/**
* Updates an existing UserEquipmentMapping 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);
$usermodel = User::find()->all();
$laptopmodel = Laptop::find()->all();
$phonemodel = Phone::find()->all();
$displaymodel = display::find()->all();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view','id' => $model->id]);
}
return $this->render('update',]);
}
/**
* Deletes an existing UserEquipmentMapping 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 UserEquipmentMapping model based on its primary key value.
* If the model is not found,a 404 HTTP exception will be thrown.
* @param integer $id
* @return UserEquipmentMapping the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = UserEquipmentMapping::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}
型号:
<?PHP
namespace app\models;
use Yii;
use app\models\User;
use app\models\UserQuery;
use yii\db\ActiveQuery;
/**
* This is the model class for table "user_equipment_mapping".
*
* @property int $id
* @property int $user_id
* @property int|null $laptop_id
* @property int|null $phone_id
* @property int|null $display_id
* @property string|null $start_date
* @property string|null $stop_date
*
* @property display $display
* @property Laptop $laptop
* @property Phone $phone
* @property User $user
*/
class UserEquipmentMapping extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'user_equipment_mapping';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['user_id'],'required'],[['user_id','laptop_id','phone_id','display_id'],'integer'],[['start_date','stop_date'],'safe'],[['display_id'],'exist','skipOnError' => true,'targetClass' => display::className(),'targetAttribute' => ['display_id' => 'id']],[['laptop_id'],'targetClass' => Laptop::className(),'targetAttribute' => ['laptop_id' => 'id']],[['phone_id'],'targetClass' => Phone::className(),'targetAttribute' => ['phone_id' => 'id']],[['user_id'],'targetClass' => User::className(),'targetAttribute' => ['user_id' => 'id']],];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID','user_id' => 'User ID','laptop_id' => 'Laptop ID','phone_id' => 'Phone ID','display_id' => 'display ID','start_date' => 'Start Date','stop_date' => 'Stop Date',];
}
/**
* Gets query for [[display]].
*
* @return \yii\db\ActiveQuery|displayQuery
*/
public function getdisplay()
{
return $this->hasOne(display::className(),['id' => 'display_id']);
}
/**
* Gets query for [[Laptop]].
*
* @return \yii\db\ActiveQuery|LaptopQuery
*/
public function getLaptop()
{
return $this->hasOne(Laptop::className(),['id' => 'laptop_id']);
}
/**
* Gets query for [[Phone]].
*
* @return \yii\db\ActiveQuery|PhoneQuery
*/
public function getPhone()
{
return $this->hasOne(Phone::className(),['id' => 'phone_id']);
}
/**
* Gets query for [[User]].
*
* @return \yii\db\ActiveQuery|UserQuery
*/
public function getUser()
{
return $this->hasOne(User::className(),['id' => 'user_id']);
}
public function getUsers()
{
return $this->hasMany(User::className(),['user_id','id']);
}
public function getLaptops()
{
return $this->hasMany(Laptop::className(),['laptop_id','id']);
}
public function getdisplays()
{
return $this->hasMany(display::className(),['dislpay_id','id']);
}
public function getPhones()
{
return $this->hasMany(Phone::className(),['phone_id','id']);
}
/**
* {@inheritdoc}
* @return UserEquipmentMappingQuery the active query used by this AR class.
*/
public static function find()
{
return new UserEquipmentMappingQuery(get_called_class());
}
}
索引:
<?PHP
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel app\models\UserequipmentmappingSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'User Equipment Mappings';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="user-equipment-mapping-index">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Create User Equipment Mapping',['create'],['class' => 'btn btn-success']) ?>
</p>
<?PHP // echo $this->render('_search',['model' => $searchModel]); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,'filterModel' => $searchModel,'formatter' => [
'class' => 'yii\i18n\Formatter','nulldisplay' => '-','columns' => [
#['class' => 'yii\grid\SerialColumn'],#'id',#'user.username',['attribute' => 'user','label' => 'Username','value' => 'user.username'
#},#'laptop.laptop_model',['attribute' => 'laptop','label' => 'Laptop Series','value' => 'laptop.laptop_series'
#},#'laptop_id',#'phone.phone_model',['attribute' => 'phone','label' => 'Phone Series','value' => 'phone.phone_series'
#},#'phone_id',#'display.display_model',['attribute' => 'display','label' => 'display Series','value' => 'display.display_series'
#},#'display_id','start_date','stop_date',['class' => 'yii\grid\ActionColumn','header' => "Actions"],]); ?>
</div>
表格:
<?PHP
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use app\models\User;
use kartik\date\DatePicker;
use yii\web\View;
use yii\web\JqueryAsset;
use app\models\Laptop;
use app\models\display;
use app\models\Phone;
$userData=ArrayHelper::map($usermodel,'id','username');
$laptopData=ArrayHelper::map($laptopmodel,'laptop_series');
$phoneData=ArrayHelper::map($phonemodel,'phone_series');
$displayData=ArrayHelper::map($displaymodel,'display_series');
$availablelaptopData = ArrayHelper::map(Laptop::find()->where(['status' => 'Available'])->all(),'laptop_series');
$availablephoneData = ArrayHelper::map(Phone::find()->where(['status' => 'Available'])->all(),'phone_series');
$availabledisplayData = ArrayHelper::map(display::find()->where(['status' => 'Available'])->all(),'display_series');
?>
<div class="user-equipment-mapping-form">
<?PHP $form = ActiveForm::begin(); ?>
<?=$form->field($model,'user_id')->dropDownList($userData,['prompt'=>'Select...']);?>
<?=$form->field($model,'laptop_id')->dropDownList($availablelaptopData,['prompt'=>'Select...']);?>
<?=$form->field($model,'phone_id')->dropDownList($availablephoneData,['prompt'=>'Select...']);?>
<?=$form->field($model,'display_id')->dropDownList($availabledisplayData,['prompt'=>'Select...']);?>
<?=$form->field($model,'start_date')->widget(DatePicker::classname(),[
'language' => 'en','value' => date('yyyy/mm/dd',strtotime('+7 days')),'readonly' => true,#'disabled' => true,#'size' => 'lg',#'type' => DatePicker::TYPE_COMPONENT_APPEND,'options' => ['placeholder' => 'Selectati data'],'pluginoptions' => [
#'orientation' => 'top right','format' => 'yyyy/mm/dd','todayHighlight' => true,'todayBtn' => true,'autoclose'=>true,]
]);?>
<?=$form->field($model,'stop_date')->widget(DatePicker::classname(),]
]);?>
<div class="form-group">
<?= Html::submitButton('Save',['class' => 'btn btn-success']) ?>
</div>
<?PHP ActiveForm::end(); ?>
</div>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)