Php 更新时如何设置yii2中数组对象的多选值
我有一个表,它有多个参考ohter表,如Php 更新时如何设置yii2中数组对象的多选值,php,mysql,activerecord,yii2,yii2-advanced-app,Php,Mysql,Activerecord,Yii2,Yii2 Advanced App,我有一个表,它有多个参考ohter表,如 user id name email categories id title user_categories user_id category_id 在这里,用户将拥有多个与其关联的类别 我能够通过以下新记录成功保存这些记录 查看文件: echo $form->field($package_categories, 'category_id')->dropDownList( ArrayHe
user
id name email
categories
id title
user_categories
user_id category_id
在这里,用户将拥有多个与其关联的类别
我能够通过以下新记录成功保存这些记录
查看文件:
echo $form->field($package_categories, 'category_id')->dropDownList( ArrayHelper::map(
StudyMaterialCategories::find()->all(), 'id', 'title'),
['multiple' => true]
);
$model = new Packages();
$package_categories = new PackageCategories();
$request = Yii::$app->request;
if ($request->isPost) {
$transaction = Yii::$app->db->beginTransaction();
try {
$post = $request->post();
$model->load($post);
$model->save();
foreach ($post['PackageCategories']['category_id'] as $key => $value) {
$package_categories = new PackageCategories();
$package_categories->category_id = $value;
$package_categories->package_id = $model->id;
$package_categories->save();
}
$transaction->commit();
return $this->redirect(['view', 'id' => $model->id]);
} catch (Exception $ex) {
$transaction->rolback();
Yii::$app->session->setFlash("error", $ex->getMessage());
}
}
保存新记录:
echo $form->field($package_categories, 'category_id')->dropDownList( ArrayHelper::map(
StudyMaterialCategories::find()->all(), 'id', 'title'),
['multiple' => true]
);
$model = new Packages();
$package_categories = new PackageCategories();
$request = Yii::$app->request;
if ($request->isPost) {
$transaction = Yii::$app->db->beginTransaction();
try {
$post = $request->post();
$model->load($post);
$model->save();
foreach ($post['PackageCategories']['category_id'] as $key => $value) {
$package_categories = new PackageCategories();
$package_categories->category_id = $value;
$package_categories->package_id = $model->id;
$package_categories->save();
}
$transaction->commit();
return $this->redirect(['view', 'id' => $model->id]);
} catch (Exception $ex) {
$transaction->rolback();
Yii::$app->session->setFlash("error", $ex->getMessage());
}
}
到目前为止,它正在成功运行
但是我在更新表格的时候卡住了。问题部分是下拉列表。如果我带着对象数组,如何根据数据库设置多个选定选项。
看看下面的代码
$package_categories = PackageCategories::find()
->where('package_id=:package_id', ['package_id' => $id])->all();
if (count($package_categories) < 1) {
$package_categories = new PackageCategories();
}
$request = Yii::$app->request;
if ($request->isPost) {
$transaction = Yii::$app->db->beginTransaction();
try {
$post = $request->post();
$model->load($post);
$model->save();
$package_categories = new PackageCategories();
$package_categories->deleteAll(
"package_id=:package_id",
[':package_id' => $model->id]
);
foreach ($post['PackageCategories']['category_id'] as $key => $value) {
$package_categories = new PackageCategories();
$package_categories->category_id = $value;
$package_categories->package_id = $model->id;
$package_categories->save();
}
$transaction->commit();
return $this->redirect(['view', 'id' => $model->id]);
} catch (Exception $ex) {
$transaction->rolback();
Yii::$app->session->setFlash("error", $ex->getMessage());
}
}
$package\u categories=PackageCategories::find()
->其中('package_id=:package_id',['package_id'=>$id])->all();
if(计数($package_categories)<1){
$package_categories=新的PackageCategories();
}
$request=Yii::$app->request;
如果($request->isPost){
$transaction=Yii::$app->db->beginTransaction();
试一试{
$post=$request->post();
$model->load($post);
$model->save();
$package_categories=新的PackageCategories();
$package\u categories->deleteAll(
“package_id=:package_id”,
[':package_id'=>$model->id]
);
foreach($key=>value的post['PackageCategories']['category\u id']as$key){
$package_categories=新的PackageCategories();
$package\u categories->category\u id=$value;
$package\u categories->package\u id=$model->id;
$package_categories->save();
}
$transaction->commit();
返回$this->redirect(['view','id'=>$model->id]);
}捕获(例外$ex){
$transaction->rolback();
Yii::$app->session->setFlash(“错误”、$ex->getMessage());
}
}
如果我试图获取数组
$package\u categories
的第一个对象,该对象只能设置所选的一个选项,这是一个模型类许可证的示例代码,它通过许可证
(透视表模型)与活动
有多对多
关系
示范班活动
public class Permit extends \yii\db\ActiveRecord {
public $activities_ids;
...
public function rules() {
return [
...
[['activities_ids'], 'safe'],
...
];
}
...
// Method called after record is saved, be it insert or update.
public function afterSave($insert, $changedAttributes) {
// If this is not a new record, unlink all records related through relationship 'activities'
if(!$this->isNewRecord) {
// We unlink all related records from the 'activities' relationship.
$this->unlinkAll('activities', true);
// NOTE: because this is a many to many relationship, we send 'true' as second parameter
// so the records in the pivot table are deleted. However on a one to many relationship
// if we send true, this method will delete the records on the related table. Because of this,
// send false on one to many relationships if you don't want the related records deleted.
}
foreach($this->activities_ids as $activity_id) {
// Find and link every model from the array of ids we got from the user.
$activity = Activity::findOne($activity_id);
$this->link('activities', $activity);
}
parent::afterSave($insert, $changedAttributes);
}
...
// Declare relationship with Activity through the pivot table permitActivity
public function getActivities(){
return $this->hasMany(Activitiy::className(), ['id' => 'activity_id'])
->viaTable('permitActivity',['permit_id' => 'id']);
}
...
public function afterFind(){
parent::afterFind();
$this->activities_id = ArrayHelper::getColumn($this->activities, 'id');
}
}
这样,模型类负责使用透视表创建和更新关系
最重要的是正确声明relationship方法
编辑
这是使用的视图的一个示例。我真的不知道dropDownList是否支持多个选择,但是Select2有很多有用的功能,我通常使用它而不是其他选项
echo $form->field($model, 'activities')->widget(Select2::classname(), [
'data' => $data,
'options' => [
'placeholder' => '...'
],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
],
]);
我的问题是如何在视图中设置选中的多个项目在我的示例中,多个项目字段是activities\u ids
给我一个示例在视图中显示,比如$form->field($modal_obj,'attribute_name')->dropdownlist($items,['multiple'=>true]),我认为小部件中的$data变量是数组,但我的问题是,当我们在更新表单中时,如何将两个选项设置为选中?若要在“多重选择”中显示选定的多个项目,只需将值设置为用于显示“多重选择”的模型字段的数组。