Jump to:

3450 Posts in 1063 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » How to pass custom array to dataObject without updating database?

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

Page: 1
Go to End
Author Topic: 424 Views
  • Valorez
    Avatar
    Community Member
    11 Posts

    How to pass custom array to dataObject without updating database? Link to this post

    Hello!

    I have a problem. I'm saving outclicks on products, to show them on the site reports section in the CMS. The only problem is, I can't seem to figure out how to pass the array to the sourceRecords() in extended SS_Report, without using a DataObject.

    I tried to create a DataObject for the results, but it keeps creating new ones.

    I have 2 objects/databases, 1 is the clicks where there are many ProductID's, the other one is the object/database where I pull the counted clicks, so the ProductID's there should be unique.

    How can I UPDATE an object, with a specific ID, rather then it adding a new one?

    I've tried using the ->write(), but that only creates a new one. Also using ->push() doesn't work. What should I do?

    PS. To make it even more clear:

    ProductOutclicks.php

       static $db = array(
          );
       
       public static $has_one = array(
             "Product" => "Product"
          );

    ----------

    In between here, I collect all the above ProductOutclicks, and merge them into their respective ProductID, counting the total amount of clicks.

    ---------

    ProductOutclickReportObject.php

    static $db = array(
          "Name" => "Text",
          "Clicks" => "Int",
          "ProductsID" => "Int"
       );


    Now I want to pull a specific ProductID from this object and UPDATE it, if the ProductID does not work, we add it to the DataObject.

    I've tried all kinds of stuff,
    ->write()
    ->push()
    custom code

    Thanks in advance,
    Andreas Saarva.

  • martimiz
    Avatar
    Forum Moderator
    1078 Posts

    Re: How to pass custom array to dataObject without updating database? Link to this post

    $myObject>write() should update the object if you make sure $myObject->ID is set to the right ID first. If the ID is left empty, a new object is inserted.

  • Valorez
    Avatar
    Community Member
    11 Posts

    Re: How to pass custom array to dataObject without updating database? Link to this post

    These are my 2 functions:

       static function CalculateTime($limit) {
          $period = 0;
          $temp = explode(" ",trim($limit));
          foreach($temp as $time) {
             if(strstr($time,"m")) {
                $timeMultiplier = 60; // 1 minute
             }
             else if(strstr($time,"h")) {
                $timeMultiplier = 60*60; // 1 hour
             }
             else if(strstr($time,"d")) {
                $timeMultiplier = 60*60*24; // 1 day
             }
             $period = $period + (intval($time)*$timeMultiplier);
          }
          return $period;
       }

       function sourceRecords($params, $sort, $limit) {
          $calculatedLimit = $this->CalculateTime($params['TimeRange']);
          $time = time();
          $timeFrom = $time - $calculatedLimit;
          $timeTo = $time; // Support for date ranges in later updates
          
          $dataObject = DataObject::get("ProductOutclicks","","ProductID");
          $items = array();
          
          foreach($dataObject as $do) {
             $Name = DataObject::get_by_id("Product",$do->ProductID);
             $do->setField("Name", $Name->Name);
             if(!isset($items['items'][$do->ProductID]['Clicks']) && $items['items'][$do->ProductID]['Clicks'] <= 0) {
                $items['items'][$do->ProductID]['ProductID'] = $do->ProductID;
                $items['items'][$do->ProductID]['Clicks'] = 1;
                $items['items'][$do->ProductID]['Name'] = $Name->Name;
             }
             else {
                $items['items'][$do->ProductID]['Clicks']++;
             }
          }
          
          
          // Then we save all the new statistics
          foreach($items['items'] as $id => $item) {
             $check = DataObject::get_by_id("ProductOutclickReportObject",$id);
             $check->Name = $item['Name'];
             $check->Clicks = $item['Clicks'];
             $check->ProductID = $item['ProductID'];
             $check->write();
          }

          return DataObject::get("ProductOutclickReportObject");
       }

  • Valorez
    Avatar
    Community Member
    11 Posts

    Re: How to pass custom array to dataObject without updating database? Link to this post

    Fatal error: Call to undefined method stdClass::write() in /wwwsf/exam/mysite/code/ProductOutclickReport.php on line 63

    It doesn't seem to like the ->write()

  • Valorez
    Avatar
    Community Member
    11 Posts
    424 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.