Jump to:

22976 Posts in 11702 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Pushing a field value into a DataObject::get array?

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: 297 Views
  • Valorez
    Avatar
    Community Member
    11 Posts

    Pushing a field value into a DataObject::get array? Link to this post

    Hello there, I recently started using Silverstripe as a part of my new job. It's going pretty good so far, seems like a really powerful tool and I like it alot.

    Anyhow, I bumped into a problem which I can not solve (the Silverstripe way, I dont wanna do too much custom coding).

    class ProductOutclickReport extends SS_Report {

    ...

       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
       //   $Products = DataObject::get("ProductOutclicks", "UNIX_TIMESTAMP(Created) >= {$timeFrom} AND UNIX_TIMESTAMP(Created) < {$timeTo}");
          $allProducts = DataObject::get("ProductOutclicks", "UNIX_TIMESTAMP(Created) >= {$timeFrom} AND UNIX_TIMESTAMP(Created) < {$timeTo}");
          foreach($allProducts as $Product) {
             $Product['Name'] = DataObject::get_one("Product","ID = '{$Product->ProductID}'");
             $Products = $Product;
          }
          return $Products;
       }

    ...

    This is a custom report. It records outclicks on products, to be presented in the Site Report section of the admin.

    My question is:
    How do I get a single row/field using SS functionality, and pushing it to the Product array?

    At the moment, when print_r'ing the $Product variable, I get so much information I've never seen before, I dont know how it all works. I guess it's an image if some kind of array-object? Containing [protected]/[private] and alot of stuff... simply not a "normal" array.

    Btw, using the commented line, "// $Products = DataObject ..." it works, returning all the affected rows, but the Name is in the Product object, not ProductOutclicks (which has a ProductID).

    Thanks in advance,
    Andreas Saarva.

  • Valorez
    Avatar
    Community Member
    11 Posts

    Re: Pushing a field value into a DataObject::get array? Link to this post

    I'm afraid this forum is not too active or atleast well populated with SS developers, does anyone have any suggestions for good forums with other SS developers?

    Regards,
    Andreas.

  • martimiz
    Avatar
    Forum Moderator
    1035 Posts

    Re: Pushing a field value into a DataObject::get array? Link to this post

    ... and the fact that not everyone lives in the same timezone and lots of people may be on vacation

    But sure, these forums are sometimes quiet, but then again not many questions remain unanswered. But there are other options: check out the irc channel, stackoverflow.com or just google

    The tutorials in the documentation are really helpful in understanding the SilverStripe data structure - don't skip them!. if you're on 2.4.x, check out: http://doc.silverstripe.org/framework/en/2.4/tutorials/ (in 3.x things are a bit different)

    As to your problem:

    $Product['Name'] = DataObject::get_one("Product","ID = '{$Product->ProductID}'");

    DataObkject::get_one() returns a DataObject with all its properties and DataFields, in this case an instance of your Product class, representing one record in the Product datatable. If you want just the Name (if Name is in fact a DataField in Product), you can for instance:

    $Product['Name'] = DataObject::get_one("Product","ID = '{$Product->ProductID}'")->Name;

    Also: I find that in SilverStripe, you hardly ever have to turn to arrays to display data: templates, for instance, work with Literals, DataObjects and DataObjectSets only!

    Martine

  • Valorez
    Avatar
    Community Member
    11 Posts

    Re: Pushing a field value into a DataObject::get array? Link to this post

    Hello!

    Yes, you're probably right about people being on vacation and stuff. I've spent like 6 hours googling and reading the manuals.

    I managed to solve it, and I think my question wasn't well formulated. What I wanted to do, is retrieving a DataObject and populate each object with a custom field, kinda like JOIN.

    ...

       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");
          
          foreach($dataObject as $do) {
             $do->setField("Name","test");
          }

          return $dataObject;
       }

    ...

    Now it adds a name to each data object.

    Thanks for your answer!

    297 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.