Delete Child records with parent

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
SkayyHH
Veteran Member
Posts: 425
Joined: 2015-04-27 21:18

Delete Child records with parent

Post by SkayyHH » 2020-11-14 19:01

Hello,

I try to delete child records with when I delete the parent records.

This code somehow doesn't work in the tablename.php.

Code: Select all

	function Employees_de_before_delete($selectedID, &$skipChecks, $memberInfo, &$args) {

 	sql("DELETE FROM `Todo_de` WHERE `ID`='{$selected_id}'", $eo);

	 return TRUE; 
	};
	
	

Does anyone have any idea what could be wrong?

Thanks very much, Kai

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1792
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Delete Child records with parent

Post by jsetzer » 2020-11-14 19:24

You have misspelled the variable name $selectedID in your SQL command.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

SkayyHH
Veteran Member
Posts: 425
Joined: 2015-04-27 21:18

Re: Delete Child records with parent

Post by SkayyHH » 2020-11-14 21:06

A heartfelt thank you. I took the code from tablename_dml.php files and didn't pay attention to it ...

Code: Select all

sql("DELETE FROM `Todo_de` WHERE `ID`='{$selectedID}'", $eo);
it still doesn't work. or did you mean something else?

pböttcher
AppGini Super Hero
AppGini Super Hero
Posts: 1633
Joined: 2018-04-01 10:12

Re: Delete Child records with parent

Post by pböttcher » 2020-11-15 08:58

Hi,

just as a guess, are you sure the selectedID is the correct ID for the Todo_de table to be deleted?
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

SkayyHH
Veteran Member
Posts: 425
Joined: 2015-04-27 21:18

Re: Delete Child records with parent

Post by SkayyHH » 2020-11-15 14:21

Hi,

no, I'm definitely not :-)

I have 2 tables.

1.) Employee, Primary Key is ID
2.) Todo, Primary key is also ID

I have a parent child relationship. "Employee" is parent, "todo" is child.

"Todo table" is connected to the "employee table" via a field "employee" in "Todo table".

In hooks / employee.php i use the code above.

Thanks very much, Kai

pböttcher
AppGini Super Hero
AppGini Super Hero
Posts: 1633
Joined: 2018-04-01 10:12

Re: Delete Child records with parent

Post by pböttcher » 2020-11-15 15:24

Well,

try

Code: Select all

[code]sql("DELETE FROM `Todo_de` WHERE `employee`='{$selectedID}'", $eo);
[/code]
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

SkayyHH
Veteran Member
Posts: 425
Joined: 2015-04-27 21:18

Re: Delete Child records with parent

Post by SkayyHH » 2020-11-15 19:18

Thanks you very much!

I had tried that too. Unfortunately, it doesn't work that way either.

pböttcher
AppGini Super Hero
AppGini Super Hero
Posts: 1633
Joined: 2018-04-01 10:12

Re: Delete Child records with parent

Post by pböttcher » 2020-11-15 19:43

maybe you can post the appgini definitions you have set up
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

SkayyHH
Veteran Member
Posts: 425
Joined: 2015-04-27 21:18

Re: Delete Child records with parent

Post by SkayyHH » 2020-11-15 21:31

Hi,
do you mean the database definition?

Parent table:

Code: Select all

CREATE TABLE IF NOT EXISTS `employees_de` (
  `ID` int(10) unsigned NOT NULL,
  `last_name` varchar(40) NOT NULL,
  `first_name` varchar(40) NOT NULL,
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
Child Table:

Code: Select all

CREATE TABLE IF NOT EXISTS `todo_de` (
  `ID` int(10) unsigned NOT NULL,
  `employee` int(10) unsigned NOT NULL,			> Lookup field to parent Employees.de
  `title` varchar(40) NOT NULL,
) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=utf8;
Thanks much, Kai

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1792
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Delete Child records with parent

Post by jsetzer » 2020-11-15 21:46

What is the value of $eo after executing sql() function?

Did you check it?

Did you write table names and field names 100% correct? Depending on your server settings they may be case sensitive. Dumping the sql command itself and dumping $eo should give a clear error message - if there is an error in sql command.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

SkayyHH
Veteran Member
Posts: 425
Joined: 2015-04-27 21:18

Re: Delete Child records with parent

Post by SkayyHH » 2020-11-15 22:53

Thanks for your help

Unfortunately I don't know how I can output the content of the variable.

The spelling should match. Even the upper and lower case ...

SkayyHH
Veteran Member
Posts: 425
Joined: 2015-04-27 21:18

Re: Delete Child records with parent

Post by SkayyHH » 2020-11-15 23:04

I got it...

I have to switch on "delete records even if they have children records" in the parent table.

Somehow the function is now the other way around with the code to delete the childs.

if the function is switched off, the parent record can be deleted although a child is present. only the child is not deleted.

strange

Thanks so much for you time.

rpierce
Veteran Member
Posts: 251
Joined: 2018-11-26 13:55
Location: Washington State

Re: Delete Child records with parent

Post by rpierce » 2023-11-28 17:49

Hi,

Doesn't this method create orphaned child records for you?

Post Reply