Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 914987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:48:41+00:00 2026-05-15T17:48:41+00:00

I’m trying to execute some logic before deleting a field. I have some models

  • 0

I’m trying to execute some logic before deleting a field. I have some models that are dependent on the model being deleted, and I want to make sure that image files related to those dependent models are also deleted, but I’m a bit confused on how the model callbacks work.

I know that I define the before Delete function in the model class, but How do I access the data in the current model or dependent models being deleted?

function beforeDelete() {

}

I’m just a little confused as how to use these callbacks, and I haven’t seen any great documentation out there.

Edit:
After adding this to the parent model, it seems to always return false.

function beforeDelete() {
    if ($this->DependentModel->find('count', array('conditions' => array('DependentModel.parent_id' => $this->id))) == 1){  
        return true;
    } else{
        return false;
    }
}

Should be obvious what I’m trying to do here. If there is one entry of the dependent model present in the table, return true and continue the deletion. I made sure that there is in fact one table entry that is dependent on the object being deleted. When I execute the delete action it always returns false. What’s going on here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:48:42+00:00Added an answer on May 15, 2026 at 5:48 pm

    When using callbacks, you can refer to the API for the class you are extending to check the parameters it accepts. Your implementation should accept, at minimum, the same parameters as the methods you are overriding.

    For example, Model::beforeDelete is implemented like so:

    /**
     * Called before every deletion operation.
     *
     * @param boolean $cascade If true records that depend on this record will also be deleted
     * @return boolean True if the operation should continue, false if it should abort
     * @link http://book.cakephp.org/2.0/en/models/callback-methods.html#beforedelete
     */
        public function beforeDelete($cascade = true) {
            return true;
        }
    

    And also, ModelBehavior::beforeDelete is implemented like this (ie. when making a behavior):

    /**
     * Before delete is called before any delete occurs on the attached model, but after the model's
     * beforeDelete is called.  Returning false from a beforeDelete will abort the delete.
     *
     * @param Model $model Model using this behavior
     * @param boolean $cascade If true records that depend on this record will also be deleted
     * @return mixed False if the operation should abort. Any other result will continue.
     */
        public function beforeDelete(Model $model, $cascade = true) {
            return true;
        }
    

    Next, it is useful to know that when saving to a model and passing in the controller’s data (ie. $this->data in the controller) that data is set to the model (ie. $this->data in the model). [This happens during Model::save(), currently on line 1225.]

    In the case of the first example you can access the model using $this and in the second example you can access the model using $model (as $this would be the behavior in that context). So to get at the data, you want to use $this->data or $model->data. You can also access that model’s related models using chaining (ie. $this->RelatedModel or $model->RelatedModel).

    As the docblock comments state, $cascade should let you know if this is a cascading delete that is happening (true by default) in case your code needs to take different actions when this is or isn’t the case; and your implementation of the method should return false if you want to abort the save operation (otherwise, return true when you are done).

    There is a Media plugin for CakePHP which implements this exact functionality that can be used as a reference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.