Page 1 of 1

Foreign Key

Posted: Fri Feb 26, 2010 10:13 am
by qadeer_ahmad
I want to handle foreign key using php script.
Can i handle this run time using php script?

Thanks

Re: Foreign Key

Posted: Fri Feb 26, 2010 11:07 am
by alex.barylski
Sure instead of relying on RDBMS constraints you implement those constraints in your business logic, which I personally prefer anyways. You might do that in a DAL as well, or wherever suits your needs I suppose.

The answer, yes you can do it in PHP it's just more work.

Cheers,
Alex

Re: Foreign Key

Posted: Fri Feb 26, 2010 11:11 am
by qadeer_ahmad
Would you please give me any example how to handle these?

Re: Foreign Key

Posted: Fri Feb 26, 2010 11:21 am
by alex.barylski
I can't really, without giving a specific example.

They are simple constraints. Lets say you have two tables:

Code: Select all

users:
pkid, fname, lname, address
 
numbers:
pkid, user, type, value
users has a one to many relationship with numbers, that is, every user can have zero or more numbers associated with to it, via numbers.user.

If you have a user with 5 numbers, when you delete the user, all numbers associated with it should also be deleted.

You might have two models:

- Users
- Numbers

Users model would have a deleteUser() method. Inside this method you would have to call a deleteNumber() method iteratively to remove each associated number:

Code: Select all

function deleteUser($id)
{
  $model = new Numbers();
  $numbers = $this->selectAllNumbers($id);
  foreach($numbers as $number){
    $model->deleteNumber($number['id']);
  }
}
Not tested but should demonstrate the point of what I sam saying

Cheers,
Alex

Re: Foreign Key

Posted: Fri Feb 26, 2010 12:17 pm
by qadeer_ahmad
Thanks for help but i need when i delete a key record then all record on other table when are bind a foreign key automatically deleted so that every thing handle under the database another issue if i set foreign key locally using mysql youg or any other system it is hard to apply on phpmyadmin so i am asking if i got any script to deploying foreign key using php script or some thing else.

Thanks again for taking interest and helping me.

Re: Foreign Key

Posted: Sat Feb 27, 2010 12:07 am
by josh
qadeer_ahmad wrote:Thanks for help but i need when i delete a key record then all record on other table when are bind a foreign key automatically deleted so that every thing handle under the database another issue if i set foreign key locally using mysql youg or any other system it is hard to apply on phpmyadmin so i am asking if i got any script to deploying foreign key using php script or some thing else.

Thanks again for taking interest and helping me.
On delete = cascade. Set up innodb foreign key.

You could implement it in the business logic, but the foreign key makes it so if your script crashes (a failed query) the transaction would be rolled back/not committed.

Re: Foreign Key

Posted: Sun Feb 28, 2010 3:32 am
by Benjamin
:arrow: Moved to Databases