Page 1 of 1

Delete Child records with parent

Posted: 2020-11-14 19:01
by SkayyHH
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

Re: Delete Child records with parent

Posted: 2020-11-14 19:24
by jsetzer
You have misspelled the variable name $selectedID in your SQL command.

Re: Delete Child records with parent

Posted: 2020-11-14 21:06
by SkayyHH
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?

Re: Delete Child records with parent

Posted: 2020-11-15 08:58
by pbottcher
Hi,

just as a guess, are you sure the selectedID is the correct ID for the Todo_de table to be deleted?

Re: Delete Child records with parent

Posted: 2020-11-15 14:21
by SkayyHH
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

Re: Delete Child records with parent

Posted: 2020-11-15 15:24
by pbottcher
Well,

try

Code: Select all

[code]sql("DELETE FROM `Todo_de` WHERE `employee`='{$selectedID}'", $eo);
[/code]

Re: Delete Child records with parent

Posted: 2020-11-15 19:18
by SkayyHH
Thanks you very much!

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

Re: Delete Child records with parent

Posted: 2020-11-15 19:43
by pbottcher
maybe you can post the appgini definitions you have set up

Re: Delete Child records with parent

Posted: 2020-11-15 21:31
by SkayyHH
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

Re: Delete Child records with parent

Posted: 2020-11-15 21:46
by jsetzer
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.

Re: Delete Child records with parent

Posted: 2020-11-15 22:53
by SkayyHH
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 ...

Re: Delete Child records with parent

Posted: 2020-11-15 23:04
by SkayyHH
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.

Re: Delete Child records with parent

Posted: 2023-11-28 17:49
by rpierce
Hi,

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