我对Kartik Select2有问题。当我想在控制器中保存多个数据时,什么也不会保存。我删除了控制器中的foreach,Yii返回有关$ university_id IS NULL信息的错误。
用户表格
<?= $form->field($user_university, 'university_id')->widget(Select2::classname(),[
'name' => 'university_id[]',
'data' => ArrayHelper::map(
\app\models\University::find()->asArray()->all(),
'id',
function ($university) {
return $university['name'];
}
),
'size' => Select2::MEDIUM,
'options' => ['placeholder' => 'Select a state ...', 'multiple' => true],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
用户控制器
$user = new User();
$user->scenario = User::SCENARIO_CREATE;
$auth_assignment = new AuthAssignment();
$user_company = new UserCompany();
$user_university = new UserUniversity();
$user_status = UserStatus::find()
->select('id')
->where(['name' => UserStatus::NON_AUTHORIZED_USER])
->one();
if ($user->load(Yii::$app->request->post()) && $auth_assignment->load(Yii::$app->request->post())) {
$user->auth_key = Yii::$app->getSecurity()->generateRandomString();
$user->password_hash = Yii::$app->getSecurity()->generatePasswordHash($user->password);
$user->password_reset_token = Yii::$app->getSecurity()->generateRandomString();
$user->status_id = $user_status->id;
$user->created_at = date('Y-m-d H:i:s');
$user->updated_at = date('Y-m-d H:i:s');
if ($user->save()) {
$auth_assignment->user_id = $user->id;
$auth_assignment->created_at = date('Y-m-d H:i:s');
$universities = $user_university->university_id;
foreach((array) $universities as $university)
{
$newUserUniversity = new UserUniversity();
$newUserUniversity->user_id = $user->id;
$newUserUniversity->university_id = $university;
$newUserUniversity->created_at = date('Y-m-d H:i:s');
$newUserUniversity->save(false);
}...
用户大学模型规则
public function rules()
{
return [
[['user_id', 'university_id', 'created_at'], 'required'],
[['user_id', 'university_id','confirm'], 'integer'],
[['created_at','university_id'], 'safe'],
[['university_id'], 'exist', 'skipOnError' => true, 'targetClass' => University::className(), 'targetAttribute' => ['university_id' => 'id']],
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
];
}
答案 0 :(得分:0)
select2
有多种使用方式,目前您正在使用Usage with ActiveForm and model
,请参见以下多种方式。
use kartik\select2\Select2
// Usage with ActiveForm and model
echo $form->field($model, 'state_1')->widget(Select2::classname(), [
'data' => $data,
'options' => ['placeholder' => 'Select a state ...'],
'pluginOptions' => [
'allowClear' => true
],
]);
// With a model and without ActiveForm
echo Select2::widget([
'model' => $model,
'attribute' => 'state_2',
'data' => $data,
'options' => ['placeholder' => 'Select a state ...'],
'pluginOptions' => [
'allowClear' => true
],
]);
// Without model and implementing a multiple select
echo '<label class="control-label">Provinces</label>';
echo Select2::widget([
'name' => 'state_10',
'data' => $data,
'options' => [
'placeholder' => 'Select provinces ...',
'multiple' => true
],
]);
// A disabled select2 list input
echo '<label class="control-label">State</label>';
echo Select2::widget([
'name' => 'state_11',
'data' => $data,
'disabled' => true
]);
我猜你的问题出在
'data' => ArrayHelper::map(
\app\models\University::find()->asArray()->all(),
'id',
function ($university) {
return $university['name'];
}
),
请检查数据属性..检查是否传递正确的数组。
答案 1 :(得分:0)
在开始在$user_university
上循环之前,我看不到代码将发布请求中的数据加载到university_id
中。您需要调用$user_university->load(Yii::$app->request->post())
,然后从脚本中查找以下代码
if ($user->save()) {
$auth_assignment->user_id = $user->id;
$auth_assignment->created_at = date('Y-m-d H:i:s');
// this below line will give you an empty string rather than
// the selected ids of the `university` that you selected
// in the multi-dropdown
$universities = $user_university->university_id;
,下一行是您要保存的foreach((array) $universities as $university)
,即使在$unversities
中没有任何数组的情况下,它也不会运行一次。因此,您可以在从属性获取数组之前添加一个调用,如果没有为$user_university
加载数据,则不知道您是否要求返回错误用户,否则您需要将其添加到主检查
if ($user->load(Yii::$app->request->post()) && $auth_assignment->load(Yii::$app->request->post()) &&
$user->university->load(Yii::$app->request->post())) {
因此您的代码应如下所示
$user = new User();
$user->scenario = User::SCENARIO_CREATE;
$auth_assignment = new AuthAssignment();
$user_company = new UserCompany();
$user_university = new UserUniversity();
$user_status = UserStatus::find()
->select('id')
->where(['name' => UserStatus::NON_AUTHORIZED_USER])
->one();
//validate all models
$allModelsValid=$user->load(Yii::$app->request->post()) && $auth_assignment->load(Yii::$app->request->post()) && $user_university->load(Yii::$app->request->post());
if ($allModelsValid) {
$user->auth_key = Yii::$app->getSecurity()->generateRandomString();
$user->password_hash = Yii::$app->getSecurity()->generatePasswordHash($user->password);
$user->password_reset_token = Yii::$app->getSecurity()->generateRandomString();
$user->status_id = $user_status->id;
$user->created_at = date('Y-m-d H:i:s');
$user->updated_at = date('Y-m-d H:i:s');
if ($user->save()) {
$auth_assignment->user_id = $user->id;
$auth_assignment->created_at = date('Y-m-d H:i:s');
$universities = $user_university->university_id;
foreach((array) $universities as $university)
{
$newUserUniversity = new UserUniversity();
$newUserUniversity->user_id = $user->id;
$newUserUniversity->university_id = $university;
$newUserUniversity->created_at = date('Y-m-d H:i:s');
$newUserUniversity->save(false);
}...
除了上面的内容外,我没有找到将名称用作university_id[]
而不是university_id