S
S
Stepan2015-02-09 13:36:04
Yii
Stepan, 2015-02-09 13:36:04

Yii2 How to delete all records with a specific id?

I create an action

public function actionUpdate($id){

        $model = $this->findModel($id);                          
        $postData = $this->findDataModel($id);              

        if (
            $model->load(Yii::$app->request->post())    &&
            $postData->load(Yii::$app->request->post())    &&
            $model->save() &&
            $postData->save()
          ){

            $newData = PostData::find()->where(['post_id' => $id])->all();

            $postData = new PostData();

            $postData->post_id = $id;

            $rows[] = [];

            foreach($_POST['PostData'] as $key => $value){

                $str = strpos($key, "_");
                $key = substr($key, 0, $str);

                $postData->data_type = $key;
                $postData->post_data = $value;
                $postData->post_id;

                $rows[] = [
                    'post_id' => $postData->post_id,
                    'data_type' => $postData->data_type,
                    'post_data' => $postData->post_data
                ];
            }

            array_shift($rows);

            Yii::$app->db->createCommand()->batchInsert(
                $postData::tableName(),
                ['post_id', 'data_type', 'post_data'],
                $rows
            )->execute();


            if ( isset($_POST['save']) || isset($_POST['update'])) {
                    $model->save();
                    return $this->redirect(['view', 'id' => $model->id]);
            }

I need to remove all the old entries from there before writing something to the postModel. But neither delete() nor deleteAll() nor deleteAllGroup() nor createCommand work and as a result new records still appear and old ones remain

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Albert Tobacco, 2015-02-10
@bighoc

YourModel::deleteAll('id = :id', [':id' => $id]);

M
Maxim Timofeev, 2015-02-09
@webinar

Maybe just overwrite them?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question