Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Deleting model deletes relations
  • I'm deleting a model using "->delete()" but it also deletes another models data..
    Namely, I have the following relations:
    tmuser  ->  tmfriend  ..has_many, belongs_to
    tmuser  ->  tmgroup   ...has_many, belongs_to
    tmgroup  <-> tmfriend ... many_many, many_many

    When I delete the model Model_Tmgroup, it also deletes the user..
    What could be the issue?
    -thanks
  • philipptempelphilipptempel
    Accepted Answer
    ORM deletes known (and loaded) relations when you have cascade_delete set to true.

    Thus, first question: how does your query to get an entity of Model_Tmgroup look like?

    And second question: how does your setup for $_has_many, $_belongs_to, $_many_many look like?

    Please post some code (or review the docs and the defaults in Orm\Model) so we can help you a bit better.
  • Here are the models.
    The error occurs when I try to delete the a model "Model_Tmgroup"

    The relationship is/should be as follows:
    tmuser 1 ----- M tmfriend
    tmuser 1 ----- M tmgroup (tmusers may own many groups)
    tmfriend M -- M tmgroup (tmgroups may have many friends, and each friend may belong to many groups) Note, group owners may only add friends to their groups


    //--------- tmuser -----------//
    class Model_Tmuser extends \Orm\Model
    {
    protected static $_properties = array(
    'id',
    'username',
    'password',
    'email',
    'type',
    'group',   
    'last_login',
    'login_hash',
    'profile_fields',
    'created_at',
    'updated_at',
    );

    protected static $_has_many = array(
    'tmfriends' => array(
    'key_from' => 'id',
    'model_to' => 'Model_Tmfriend',
    'key_to' => 'tmuser_id',
    'cascade_save' => true,
    'cascade_delete' => false,
    ),

    'tmgroups' => array(
    'key_from' => 'id',
    'model_to' => 'Model_Tmgroup',
    'key_to' => 'owner_id',
    'cascade_save' => true,
    'cascade_delete' => false,
    ),
    'tmtraces' => array(
    'key_from' => 'id',
    'model_to' => 'Model_Tmtrace',
    'key_to' => 'id',  //tmuser_id
    'cascade_save' => true,
    'cascade_delete' => false,
     ));
  • ...continued

    //------------- tmfriend ------------//
    class Model_Tmfriend extends \Orm\Model
    {
    protected static $_properties = array(
    'id',
    'tmuser_id',
                    'active',
    );

    protected static $_table_name = 'tmfriend';
            
            protected static $_belongs_to = array(
                'tmuser' => array(
                    'key_from' => 'tmuser_id',
                    'model_to' => 'Model_Tmuser', 
                    'key_to' => 'id',
                    'cascade_save' => true,
                    'cascade_delete' => false,
                ));
            
            protected static $_many_many = array(
                'tmgroups' => array(
                    'key_from' => 'id',
                    'key_through_from' => 'tmfriend_id',
                    'table_through' => 'tmfriends_tmgroups', // both models plural without prefix in alphabetical order
                    'key_through_to' => 'tmgroup_id', // column 2 from the table in between, should match a users.id
                    'model_to' => 'Model_Tmgroup',
                    'key_to' => 'id',
                    'cascade_save' => true,
                    'cascade_delete' => false,
            ));

    //------------ tmgroup -----------//

  • //--------- tmgroup -------//
    class Model_Tmgroup extends \Orm\Model
    {
            protected static $_table_name = 'tmgroup';
            
    protected static $_properties = array(
    'id',
    'owner_id',
    'group_name',
    'created_at',
    'updated_at',
    );

    protected static $_observers = array(
    'Orm\Observer_CreatedAt' => array(
    'events' => array('before_insert'),
    'mysql_timestamp' => false,
    ),
    'Orm\Observer_UpdatedAt' => array(
    'events' => array('before_update'),
    'mysql_timestamp' => false,
    ),
    );
            
            protected static $_primary_key = array('id');

            
    protected static $_belongs_to = array(
                'tmusers' => array(
                    'key_from' => 'owner_id',
                    'model_to' => 'Model_Tmuser',
                    'key_to'   => 'id',
                    'cascade_save' => true,
                    'cascade_delete' => true,
            ));
            

            protected static $_many_many = array(
                'tmfriends' => array(
                    'key_from' => 'id',
                    'key_through_from' => 'tmgroup_id',
                    'table_through' => 'tmfriends_tmgroups',
                    'key_through_to' => 'tmfriend_id',
                    'model_to' => 'Model_Tmfriend', 
                    'key_to' => 'id',
                    'cascade_save' => true,
                    'cascade_delete' => false,
                ));
  • HarroHarro
    Accepted Answer
    For next time, use http://bin.fuelphp.com for your code, and post the link instead.

    What happens is pretty obvious, you have configured cascade_delete on your belongs_to between group and users. Which basically means, that when you delete  a agroup, it will delete all associated users.

    Normally, you never use cascade_delete on a belongs_to, as that is a relation from child to parent, and enabling it would delete the parent when you delete the child. If you want cascading, you want it the other way around, delete a parent, then cascade and delete it's children.

    This is very dependent on the situation offcourse. For example, When deleting an order record, you would also want to delete all order lines belonging to that order. But when you delete a library member, you never want to delete all books borrowed by that member. They remain library books, even if no one has borrowed them at a given point in time.
  • Thank you.. I'm glad I know about bin.fuelphp.com now..
    Thanks so much for the help.


Howdy, Stranger!

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

In this Discussion