1.数据库准备好3张表,注意表之间有外链关联
[code]
CREATE TABLE `companies` (
`company_id` int(11) NOT NULL ,
`company_name` varchar(100) NULL ,
`company_email` varchar(100) NULL ,
`company_address` varchar(255) NULL ,
`company_created_date` datetime NULL ,
`company_status` enum('inactive','active') NULL ,
PRIMARY KEY (`company_id`)
);
CREATE TABLE `branches` (
`branch_id` int(11) NOT NULL ,
`companies_company_id` int(11) NOT NULL ,
`branch_name` varchar(100) NULL ,
`branch_address` varchar(255) NULL ,
`branch_created_date` datetime NULL ,
`branch_status` enum('inactive','active') NULL ,
PRIMARY KEY (`branch_id`)
);
CREATE TABLE `departmentes` (
`department_id` int(11) NOT NULL ,
`branches_branch_id` int(11) NOT NULL ,
`department_name` varchar(100) NULL ,
`companies_company_id` int(11) NULL ,
`department_created_date` datetime NULL ,
`department_status` enum('inactive','active') NULL ,
PRIMARY KEY (`department_id`)
);
[/code]
2.使用http://https://https://amyflash.com/yii/advanced/backend/web/index.php?r=gii,创建mvc,模型中的自增长id可以注释掉
3.修改backend/controllers里创建中关于日期的函数,取系统日期
[code]
public function actionCreate()
{
$model = new Companies();
if ($model->load(Yii::$app->request->post()) /*&& $model->save()*/) {
$model->company_created_date = date('y-m-d h:m:s');
$model->save();
return $this->redirect(['view', 'id' => $model->company_id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
[/code]
4.修改backend\views\companies\index.php _form.php等看页面效果
[code]
<?= $form->field($model, 'company_status')->dropDownList([ 'inactive' => 'Inactive', 'active' => 'Active', '' => '', ], ['prompt' => 'status']) //添加一个默认状态
?>
[/code]