Mysql
 sql >> Database >  >> RDS >> Mysql

Eliminazioni graduali a cascata di Laravel 4

Le eliminazioni a cascata funzionano utilizzando eventi modello , ad esempio in un modello Product mi lego all'evento eliminato in modo da poter eliminare tutte le relazioni:

    // Laravel's equivalent to calling the constructor on a model
    public static function boot()
    {
        // make the parent (Eloquent) boot method run
        parent::boot();    

        // cause a soft delete of a product to cascade to children so they are also soft deleted
        static::deleted(function($product)
        {
            $product->images()->delete();
            $product->descriptions()->delete();
            foreach($product->variants as $variant)
            {
                $variant->options()->delete();
                $variant->delete();
            }
        });
    }