Kevin Cittadini
Kevin Cittadini

Reputation: 1469

CakePHP strange memory exhausted on save()

Brief Explanation

I'm working locally on XAMP. CakePHP version: 2.6.3.

I've a model named Brand. A brand has an id, a name and an enabled field.

There're many permission levels on this project, let's just take two: Admin and Superadmin.

In this project nothing gets deleted for real, this is why the enabled field exists.

My Code Logic

Cake provides fast methods to save() or delete() records so I decided to create a convenient method that I can use in any model to disable elements and to restore elements. I've called them disable and restore and I've put them in my AppController.php

protected function disable($id)
{
    return $this->{$this->modelClass}->save(array('id' => $id, 'enabled' => 0));
}


protected function restore($id)
{
    return $this->{$this->modelClass}->save(array('id' => $id, 'enabled' => 1));
}

My objective was to use these methods in any model I'll create in the future for convenience.

How I use these methods

The following code is the delete action of BrandController

public function delete( $brand_id )
{
    if( $this->disable($brand_id) )
    {
        $this->flash('Brand deleted successfully', DS . $this->Session->read('locale') . DS . 'brand' . DS . 'index', 2);
    }
    else
    {
        $this->flash('An error occured', DS . $this->Session->read('locale') . DS . 'brand' . DS . 'index', 2);
    }
}

An here is restore action of BrandController

public function restore( $brand_id )
{
    if( $this->restore($brand_id) )
    {
        $this->flash('Brand restored successfully', DS . $this->Session->read('locale') . DS . 'brand' . DS . 'index', 2);
    }
    else
    {
        $this->flash('An error occured.', DS . $this->Session->read('locale') . DS . 'brand' . DS . 'index', 2);
    }
}

My Problem

If I use if ($this->disable($brand_id)) ... no problems, If I use if ($this->restore($brand_id)) ... I get infinite loading and this Error

Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 130968 bytes) in /lib/Cake/Model/Datasource/DboSource.php on line 803

BUT... the funny thing is that I replace if ($this->restore($brand_id)) with the code inside restore method ( becoming if ($this->Brand->save(array('id' => $brand_id, 'enabled' => 1))) I get no error AT ALL

Why it does this? Those two methods are just identical.

I don't want to set memory_limit to -1 is not a solution at all.

Upvotes: 1

Views: 205

Answers (1)

Jim
Jim

Reputation: 22656

I've snipped the code unrelated to your problem:

public function restore( $brand_id )
{
    $this->restore($brand_id);
}

The BrandController restore method is calling itself. You need to change this to call the appropriate restore method.

Upvotes: 3

Related Questions