Jump to:

23358 Posts in 17874 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » Comparing Columns in DataObjects

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 353 Views
  • woodb2
    Avatar
    Community Member
    48 Posts

    Comparing Columns in DataObjects Link to this post

    I've been wrestling with this problem for a while now and can't see how to go about it. I have 2 dataobjects (Project, Enhancement). Project has many Enhancements, Enhancement has one Project.

    I was using the ProjectsID column in Enhancements to get Enhancements that haven't been assigned to a project. I also only want to get enhancements that are at certain status levels (I don't want completed enhancements to show in the list)

    $enhancements = new DataObjectSet();
    $enhancements->merge(DataObject::get('Enhancement', "StatusLevelsID = 1 AND ProjectsID = 0")); // Approved
    $enhancements->merge(DataObject::get('Enhancement', "StatusLevelsID = 4 AND ProjectsID = 0")); // In Progress

    if ($enhancements) {
          $map = $enhancements->toDropdownMap('ID', 'Title');
       }
       $fields->addFieldToTab("Root.ProjectEnhancements", new AsmselectField('Enhances', 'Enhancement', $map));

    This was working great until I realized that if I delete a project, there will be enhancements with a ProjectsID equal to a deleted Project ID and I won't be able to add them to another project.

    So instead of just getting ProjectsID = 0, I need to get ProjectsID that don't match an existing ID for my Project class. I'm not sure how to find these.

    Brian

  • jak
    Avatar
    Community Member
    46 Posts

    Re: Comparing Columns in DataObjects Link to this post

    Just to check: Enhancement actually "has one or zero" Projects?

    If that is the case, you can either remove the Link from the Enhancements before a Project is deleted, or execute an SQL query that will set them to zero. Use onBeforeDelete for that:

    class Project extends ... {
    onBeforeDelete(){
    //Alternative 1
    //loop over $this->Enhancements and unset the link. Should work similar to this
    foreach($this->Enhancements() as $enhancement){
    $enhancement->Project = null;
    }
    //Alternative 2
    //Execute SQL: UPDATE Enhancements SET ProjectID = 0 WHERE ProjectID = $this->ID;
    //in both cases make sure to call the original method, there might be important stuff in it.
    parent::onBeforeDelete();
    }
    }

  • woodb2
    Avatar
    Community Member
    48 Posts

    Re: Comparing Columns in DataObjects Link to this post

    Thanks for the help jak. I was able to get it to work using alternative 2 like:

    function OnBeforeDelete() {

       DB::query("UPDATE Enhancement SET ProjectsID=0 WHERE ProjectsID={$this->ID}");

       parent::onBeforeDelete();
    }

    353 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.