Support » Plugin: WP MVC » [Plugin: WP MVC] Deleting models with has_and_belongs_to_many association

  • Resolved Seth

    (@sdw3489)


    So I have a category model which has the association of has_and_belongs_to_many with a product model and they have a join table ‘products_categories’ linking their primary keys.

    A category can have many products associated with it and I built a custom interface similar to the has_many_dropdown form element to do so. Adding and removing products to the category works great, but deleting the category from the main category index page when it has products associated with it wont delete the category. It deletes fine when there are no linked products. Im assuming this has something to do with foreign key relationships in mysql.

    Is this the intended behavior or does the wpmvc framework have a function which will loop through the join table and remove all associations before deleting the main model or do I need to do that myself? Its a little confusing that when I click delete and there are no errors and the flash message says deleted successfully but its still there in the table.

    Ive been lookin in the core mvc_model class and I see the delete_dependent_association() function which gets called from the delete_all() function but it looks as if that only works for has_many associations, which my models down work unless they are has_and_belongs_to_many.

    Any Help is appreciated.

    Thanks!

    http://wordpress.org/extend/plugins/wp-mvc/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Seth

    (@sdw3489)

    Checked out the events-calendar example plugin and the events and speakers has a similar setup to what I have in mine and deletes events just fine when there are linked speakers. The only difference I notice is that the example plugin has an ‘id’ column in the join table along with the 2 foreign key columns, whereas mine only has the 2 foreign key columns. Is it a requirement that the join table have its own id field?

    Thread Starter Seth

    (@sdw3489)

    figured it out. My foreign key relationships were set to restrict on delete actions in the database so it wouldnt allow a delete. Updated to cascade and it solved the issue.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Plugin: WP MVC] Deleting models with has_and_belongs_to_many association’ is closed to new replies.