Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM Delete
  • Hi,
    when I want to delete parent object with child relation (has_many), child's rows are not delete but the key is set to 0.

    I don't unterstand why :(

    This is the context :

    I use Fuelphp 1.8.2

    -------------------
    Model Cart
    -------------------
    <?php

    namespace Cart;

    class Model_Cart extends \Orm\Model 
    {    
        protected static $_table_name = 'cart';
        protected static $_primary_key = array('id');

        protected static $_properties = array(
    'id',
    'customer_id',
            'session_id',
    'created_at',
    'updated_at',
        );
        

        protected static $_observers = array(
           
            'Orm\\Observer_CreatedAt' => array(
                'events' => array('before_insert'),
                'mysql_timestamp' => true,
            ),

            'Orm\\Observer_UpdatedAt' => array(
                'events' => array('before_update'),
                'mysql_timestamp' => true,
            ),
        );

        protected static $_has_many = array(
            'details' => array(
                'key_from' => 'id',
                'model_to' => '\\Cart\\Model_Details',
                'key_to' => 'cart_id',
            ),

        );

    }

    -------------------
    Model Cart Details
    -------------------
    <?php

    namespace Cart;

    class Model_Details extends \Orm\Model 
    {    
        protected static $_table_name = 'cart_details';
        protected static $_primary_key = array('id');

        protected static $_properties = array(
    'id',
    'cart_id',
            'game_id',
    'room_id',
            'players',
    'date',
            'hour',
            'created_at',
            'updated_at',
        );
        

        protected static $_observers = array(
            'Orm\\Observer_CreatedAt' => array(
                'events' => array('before_insert'),
                'mysql_timestamp' => true,
            ),

            'Orm\\Observer_UpdatedAt' => array(
                'events' => array('before_update'),
                'mysql_timestamp' => true,
            ),
        );

        protected static $_belongs_to = array(
            
            'cart' => array(
                'key_from' => 'cart_id',
                'model_to' => '\\Cart\\Model_Cart',
                'key_to' => 'id',
            ),

            'game' => array(
                'key_from' => 'game_id',
                'model_to' => '\\Games\\Model_Game',
                'key_to' => 'id_jeu',
            ),

            'room' => array(
                'key_from' => 'room_id',
                'model_to' => '\\Games\\Model_Room',
                'key_to' => 'id_salle',
            ),

        );

    }

    ---------------------------------
    Query
    ---------------------------------
    $customer_cart = \Cart\Model_Cart::query()
                                        ->where('id', $cart_id)
                                        ->get_one();

    $customer_cart->delete();

    This, remove the row in the table 'cart' but don't remove rows in the table 'cart_details' but updated rows and set column 'cart_id' = 0
  • HarroHarro
    Accepted Answer
    That is because by default, "cascade_delete" on the relation is false. 

    The ORM doesn't know that the client record has no meaning without a parent, so you have to define that. It could for example very well be that the child has other parent objects, or that it itself is a parent in a relation.

  • Thanks Harro, it works!

Howdy, Stranger!

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

In this Discussion