how to trace back deleted id

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oneyani
Forum Newbie
Posts: 10
Joined: Mon Aug 11, 2008 11:52 pm

how to trace back deleted id

Post by oneyani »

hye all...
i want to ask how to trace back deleted id

for example:
(page1)
i have register company(idcompny)
for each compny have thier own category(idcategory) and group(idgroup)

(page 2)
i want to use the compny(idcompny) to add contact person
but the company can be search only by category(idactegory) and group(idgroup)

let say idcategory and idgroup have been deleted(page 1)
so how can i will trace that compny which doesn't no have any idcategory and idgroup?in page 2

its is posibble to do this?or any idea to solve this prob...
really need your idea.

thank you in advance :?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: how to trace back deleted id

Post by josh »

innodb lets you cascade delete queries I believe, otherwise you would need to find all the sub items of your record, and recursively see if those sub items have sub items, and issue a delete query for each item you are deleting
oneyani
Forum Newbie
Posts: 10
Joined: Mon Aug 11, 2008 11:52 pm

Re: how to trace back deleted id

Post by oneyani »

jshpro2 wrote:innodb lets you cascade delete queries I believe, otherwise you would need to find all the sub items of your record, and recursively see if those sub items have sub items, and issue a delete query for each item you are deleting
sorry..i don't get ur explaination..
could u give an example? :D
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: how to trace back deleted id

Post by josh »

lets say you have categories and products,

your products table has a relational field called category_id, so in other words each product belongs to 1 category. Categories have multiple products. Then lets say products also have multiple stock levels in an inventory level table, since our products exists at more then one warehouse

so categories have many products, products have many levels of stock levels. In innodb you can tell the database this, by setting up your relational fields. You can then tell the database that if a category is deleted all its products, and inventory level entries should be deleted. In your code you just delete the category and innodb handles the rest. This is called a cascade

Otherwise your code will need to first select all the products, loop through the products and select all the stock levels, then delete all the stock levels, delete all the products and finally delete the category.

Instead of deleting the sub-products you could re-assign them to the default category_id, or give the user a choice of action

http://dev.mysql.com/doc/refman/5.0/en/ ... aints.html

Edit: phpmyadmin also provides a useful GUI for setting up cascade deletes.
oneyani
Forum Newbie
Posts: 10
Joined: Mon Aug 11, 2008 11:52 pm

Re: how to trace back deleted id

Post by oneyani »

jshpro2 wrote:lets say you have categories and products,

your products table has a relational field called category_id, so in other words each product belongs to 1 category. Categories have multiple products. Then lets say products also have multiple stock levels in an inventory level table, since our products exists at more then one warehouse

so categories have many products, products have many levels of stock levels. In innodb you can tell the database this, by setting up your relational fields. You can then tell the database that if a category is deleted all its products, and inventory level entries should be deleted. In your code you just delete the category and innodb handles the rest. This is called a cascade

Otherwise your code will need to first select all the products, loop through the products and select all the stock levels, then delete all the stock levels, delete all the products and finally delete the category.

Instead of deleting the sub-products you could re-assign them to the default category_id, or give the user a choice of action

http://dev.mysql.com/doc/refman/5.0/en/ ... aints.html

Edit: phpmyadmin also provides a useful GUI for setting up cascade deletes.
ok..thanx for the info..
really appreciate it! :)

but..i still don't have idea to make it:cry:
sorry im new in php..just a beginner :banghead:
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: how to trace back deleted id

Post by Bill H »

I think you have what's called an "orphan" situation, which means you have a larger problem. You are allowing something to be deleted (category and group), when something else (company) is linked to it as a dependent item. Sort of letting the parent get killed when the kid is still a baby.

What jshpro2 is saying is that before you allow something to be deleted, you need to be sure that nothing is linked below that thing, otherwise you are leaving that linked thing as an "orphan" and unreachable. So before you allow a category to be deleted you should be sure it has no groups belonging to it and before you allow a group to be deleted you should assure the no companies belong to it.
oneyani
Forum Newbie
Posts: 10
Joined: Mon Aug 11, 2008 11:52 pm

Re: how to trace back deleted id

Post by oneyani »

Bill H wrote:I think you have what's called an "orphan" situation, which means you have a larger problem. You are allowing something to be deleted (category and group), when something else (company) is linked to it as a dependent item. Sort of letting the parent get killed when the kid is still a baby.

What jshpro2 is saying is that before you allow something to be deleted, you need to be sure that nothing is linked below that thing, otherwise you are leaving that linked thing as an "orphan" and unreachable. So before you allow a category to be deleted you should be sure it has no groups belonging to it and before you allow a group to be deleted you should assure the no companies belong to it.

ok..now im bit more clearer..
so before deleted the category..it should be a pop up menu(warning)
or else the compny colud not saved if there's no category because its a mandatory field..
it is?
Post Reply