Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Problems with Cascade Delete
  • Hi guys, I'm having a bit of trouble with Cascade Delete in the ORM. I have the following models: Track Model
    http://scrp.at/bI1 File Model
    http://scrp.at/bI0 Each Track has a file, a file belongs to a track. Everything works as I'd expect here, the relationship is fine, however, when I try to delete the Track, i got an error saying the Model was frozen and no changes were allowed. With that error, I then wrote a test to expose this bug which I did and it's as follows:
     public function test_cascade_deletes_file()
     {
      $file = \Model_File::forge(array(
       'filename' => 'hiyo.jpg',
      ));
      $file->save();
    
      $track = Test\Helper\Track::create();
      $track->file = $file;
      $track->save();
    
      $this->assertNotEmpty( $track->file );
    
      $file_id = $track->file->id;
    
      $track = Model_Track::find( 1, array( "related" => "file" ) );
    
      $track->delete();
    
      $file = Model_File::find( $file_id );
    
      $this->assertNull( $file );
     }
    

    This returns the following error:
    1) Tests_Model_Track::test_cascade_deletes_file
    Orm\FrozenObject: No changes allowed.
    
    However, when I remove the following line from the test:
    $track = Model_Track::find( 1, array( "related" => "file" ) );
    

    It works fine. I'm not quite sure what's happening, anyone have any ideas?
  • Are you sure this is what you want? You're deleting a child, and want to auto-delete it's parent? Normally it's the other way around. The issue here is probably that when you delete the child, the ORM wants to delete the parent (and this happens first), but the parent will say it can't, since it still has children. If you remove the find(), there is no parent loaded, so the child can be safely deleted.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion