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

  • Home
  • SEARCH
  • 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 8676223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:08:25+00:00 2026-06-12T20:08:25+00:00

I am using dataobjectmanager with a many_many relationship (I can’t use manymanydataobjectmanager for this)

  • 0

I am using dataobjectmanager with a many_many relationship (I can’t use manymanydataobjectmanager for this) between owner and car. Whenever a new car is created I iterate through all instances of owner and add it’s id to a linked table along with the new car ID.

My problem is that the code for doing this is within the onafterwrite method and is called twice. I’m not sure why. I’ve also noticed that for my three owners it is creating rows in the linked table oddly. The first two IDs will be in order then it will stick one. So it’ll be rows 1, 2 and 4 with no 3.

This is my onAfterWrite method

public function onAfterWrite() {

    if(Permission::check('ADMIN')){
        $Pages = DataObject::get('Owner');              

        foreach($Pages as $owner) {

        DB::query("INSERT INTO Owner_Cars (OwnerID, CarID) VALUES ('". $owner->ID . "', '" . $this->ID . "')");
        }
    }
    else {
        echo "Failure";
        return false;
    }
    return parent::onAfterWrite();
}

I’d appreciate any advice you could give me.

Thanks

  • 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-12T20:08:26+00:00Added an answer on June 12, 2026 at 8:08 pm

    The onAfterWrite() method is probably called twice because write() is called twice. The most common reason that this happens is that:

    • A write() happens to generate the ID
    • Then another write() happens as part of saving related records that rely on that ID.

    In general, I don’t think you can rely on onAfterWrite() being called once: write() is supposed to be designed so that it can be called any old number of times and will only actually affect the database if there are changes to be made.

    You would need need to make your code call the necessary DELETE, INSERT, and/or UPDATE statements to be compatible with this. You might, for example:

    • Select all the Owner_Cars records where OwnerID = $owner->ID
    • Delete any not in the $Pages list
    • Insert any from the $Page list that aren’t already in Owner_Cars

    One other suggestion I would make, if you can, is to try out SilverStripe 3. SilverStripe 3’s GridField handles this kind of stuff more robustly out of the box and you might find it easier to build your app on that.

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

Sidebar

Related Questions

using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Firstly I'm using version 2.4.7 of SilverStripe. I have a has_one relationship between a
Using the new mvc4 now gives us access to use facebook oauth otb. I
Using PyObjC , you can use Python to write Cocoa applications for OS X.
using this code I can send one texture to the shader: devcon->PSSetShaderResources(0, 1, &pTexture);
Using CI for the first time and i'm smashing my head with this seemingly
Using Location.getBearing(); I seem to get randomly changing bearings. Aka, I can turn the
Using SilverStripe 2.4.7. I've done some searching but I can't seem to be able
Using Yii Framework, how can I access Model Constant in Controller? Model.php ... const
Using Matlab, I have to transform the intensity of an image using this given

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.