Welcome Guest, Not a member yet? Register   Sign In
Foreign keys and delete cascade
#1

[eluser]Unknown[/eluser]
Hi,

first, please excuse my english as this is not my native language.


Here is my problem :
I have an existing database with foreign keys and everything needed, it's a professional DB.
I just tried to use it in CodeIgniter as i'm trying to learn it.

Everything is fine up to now, i've defined my model, made functions, used them, get my results, everything's working.

But, to go further with CodeIgniter, i'd like to know how it deals with foreign keys and delete cascade. Up to now, i've not defined any foreign keys, and don't know how to do it since i've just defined my Db in my config and then defined some protected variables to bind table names.

How does CI knows if I have foreign keys ? Does it check the metadata of the database ? If so, how does it deal with the cascade deleting ?


Thanks in advance to those who'll answer me.

Cheers.
#2

[eluser]Otemu[/eluser]
Hi,

Defining foreign keys and delete cascade is something you would do on the database side Codeigniter doesn't need to be aware of this, foreign keys and cascade delete help in terms of data integrity.

For example when using the delete cascade, this will be set on the table, so if you have a model that deletes a user record, Codeigniter connects to database runs your delete function, then the database is aware that there is a cascade delete set on that table and will delete that user and all other references within other tables, Codeigniter doesn't need to do this or worry about this unless you write your own model to handle this manually.

Another example is primary keys, Codeigniter is not aware if this, if you try to add a record to your database and the same key exists, you will receive a error. You can write your models to handle this, either checking if a user already exists or returning false if the record doesn't exist.

Hope that helps.





Theme © iAndrew 2016 - Forum software by © MyBB