如何从其他表中获取随机值?在我的员工php我有3条记录,所以有3个id值。在我的ticket.php中,一旦我创建了一个票据,它将自动从employee表中获取id值,但它不是随机的,我该怎么做?
每当我创建一张票时,我的员工都会获得与员工相同的价值。
在ticketcontroller.php
中 public function actionCreate()
{
$model = new Ticket();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
$model->time_start = date('y-m-d h:i:s');
$model->status = ('On Going');
$model->employee_respond_id = array_rand('id');
return $this->render('create', [
'model' => $model,
]);
}
}
在_form.php
中<?= $form->field($model, 'employee_respond_id')->dropDownlist(
ArrayHelper::map(Employee::find()->all(), 'id', 'id'),
[
'readOnly' => true,
'style' => 'width:200px'
]
); ?>
答案 0 :(得分:2)
尝试这种方式:
public function actionCreate()
{
$model = new Ticket();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
$employeeIDs = ArrayHelper::map(Employee::find()->all(), 'id', 'id'),
$model->time_start = date('y-m-d h:i:s');
$model->status = ('On Going');
$model->employee_respond_id = array_rand($employeeIDs);
return $this->render('create', [
'model' => $model,
'employeeIDs' => $employeeIDs
]);
}
}
<强> _form.php这个强>
<?= $form->field($model, 'employee_respond_id')->dropDownlist(
$employeeIDs,
[
'readOnly' => true,
'style' => 'width:200px'
]
); ?>
最好使用员工姓名作为标签,并将ID作为下拉列表中的值,仍然适用于随机功能。