A
A
akula222016-07-26 16:57:39
Yii
akula22, 2016-07-26 16:57:39

How to pass the $id of the category to be deleted to beforeDelete?

I delete the selected category

public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

now I need to delete all the posts that were in this category
, as I understand it, in the category model I should have beforeDelete
public function beforeDelete()
    {
        if(parent::beforeValidate())
        {

            return true;
        }

        return FALSE;
    }

tell me what to write there, how to pass $id to beforeDelete.
And then I need to delete all the comments that were in the posts with the $id category!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2016-07-26
@akula22

public function beforeDelete()
    {
        if(parent::beforeDelete())
        {
            $this->id; //тут твой айди
            return true;
        }

        return FALSE;
    }

And it is better, as they wrote above, to indicate ON DELETE - CASCADE in the connection. MySQL will automatically delete child records if the table is on the InnoDB engine. You should manually delete it when, in addition to writing to the database, you need to delete some other files on the server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question