Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8546309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:59:15+00:00 2026-06-11T12:59:15+00:00

How to set parent_id=NULL in child table when record in parent table is deleted?

  • 0

How to set parent_id=NULL in child table when record in parent table is deleted?

It’s like ON DELETE = SET NULL in MySQL INNODB tables, but I would like to avoid using all this (cascading, ignoring, updating and setting null) functionality in INNODB level and move it to atk4 Model to keep all this in logic one place.

For example,

class Model_Parent extends Model_Table{
  public $table='parent';
  function init(){
    parent::init();
    $this->addField('name');
    $this->hasMany('Child');
    $this->addHook('beforeDelete',$this);
  }
  function beforeDelete($m){
    // I guess here I should somehow set parent_id=NULL in all related Model_Child
    // records, but when I do so, then it's again DB constraint violation of course
    $c = $m->ref('Child');
    foreach($c as $junk){
      $c->set('parent_id',NULL); // this and below is not working
      $c->save();
    }
  }
}

class Model_Child extends Model_Table{
  public $table='child';
  function init(){
    parent::init();
    $this->addField('name');
    $this->hasOne('Parent');
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:59:16+00:00Added an answer on June 11, 2026 at 12:59 pm

    It’s simple, you just need to go into DSQL from Model:

    $m->ref('Child')->dsql()->set('parent_id',null)->update();
    

    More info: http://www.youtube.com/watch?v=sSRaYpoJFHk&list=PL7CBF92AB03A1CA3B&index=3&feature=plpp_video

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have parent/child relationship set up via Node Reference. A Child record can have
I have a set of tables that look like: workflows = Table('workflows', Base.metadata, Column('id',
When I delete a record in a table, related record should be deleted in
If I have entities set up for Parent - Child - Grandchild and one
Trying to set up a dependency in C++ with a parent-child relationship. The parent
I've got a visits table that looks like this: id identity(1,1) not null, visit_date
I have a parents table looks like this CHILD_ID | PARENT_ID | NAME 1
I have two already-existing tables which look (in part) roughly like this: CREATE TABLE
I have a list of Child objects. I would like to be able to
I have the following table structure: parent(parentId) child(childId, parentId fk) Then, I have the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.