Question:
Hello All!
I 've search in API 4.0, but can’t find an answer…
i have a dB date field type in my SS_silverstripe data base using MariaDB.
i have un array and push the ‘mDates’=>$row[‘Date’]. That’s right and OK.
So, in the template, if i use $mDates, i have the good return, i see the good Date.
But, there is a but…
when i try to use in the template format expression like this (new (CLDR format), $mDates.Format(‘dd.MM.y’), i have un empty return… Please Doctor, where’s my problem?
Most likely, the reason you’re not getting anything is that in the course of the process, you’re pushing a string into the arraydata. In order to be able to use the formatting in a template you need a date object in there.
Can you expand on your code a little more? Is there a reason you’re manually creating the ArrayData? How are you getting the data from the database?
hey DorsetDigital! thank’s to you for you’r interest!
I understant what’s you said, and thinking like you. And i trying to test with the SetDateFormat.
So In my error.log:
[2018-12-24 11:47:38] error-log.ERROR: Uncaught Exception Error: “Call to a member function setDateFormat() on string” at /var/www/silverstripe/app/src/Page.php line 116 {“exception”:"[object] (Error(code: 0): Call to a member function setDateFormat() on string at /var/www/silverstripe/app/src/Page.php:116)"}
So i found this solution, but it’s not realy sexy…
in my dB this is the table ‘MediaPage’:
|Colonne|Type|
|ID|int(11)
|ExternalLink|varchar(255)
|Abstract|mediumtext
|Date|date
|MediaTypeID|int(11)
//News-Tag-Jeunes
public function RecentJeunes()
{
$list1 = ArrayList::create();
//
$sqlQuery = new SQLSelect();
$sqlQuery->setFrom('MediaPage');
$sqlQuery->selectField('*');
$sqlQuery->addInnerJoin('MediaPage_Categories','"MediaPage_Categories"."MediaPageID" = "MediaPage"."ID"');
$sqlQuery->addWhere('"MediaPage_Categories"."MediaTagID" = 1');
$sqlQuery->setOrderBy('Date', 'DESC');
// $sqlQuery->setGroupBy(...);
// $sqlQuery->setHaving(...);
$sqlQuery->setLimit(3);
//$sqlQuery->setDistinct(true);
//
// Execute and return a Query object
$results = $sqlQuery->execute();
//
// Iterate over results
//print_r($results);
//
foreach($results as $row) {
//
$type = SiteTree::get()->byID($row['MediaPageID']);
if ($type) {
$name = $type->title;
} else {
$name="";
}
//
$list1->push(
new ArrayData(array(
//'mDates'=>$row['Date'],
'mDatesD'=>date('d',strtotime($row['Date'])),
'mDatesM'=>date('m',strtotime($row['Date'])),
'mDatesY'=>date('Y',strtotime($row['Date'])),
'mAbstract'=>$row['Abstract'],
'mTitle'=>$name,
)
)
);
//
//print_r($list1);
return $list1;
}
Is there a reason you’re not using the ORM for these queries? Having queries nested inside a foreach loop has the potential for some serious performance issues.
In theory you should be able to replace all that code with a relatively simple one-liner (assuming you have the relation set up between MediaPage and MediaPage_Categories) :
public function getRecentJeunes() {
return MediaPage::get()->filter(['MediaPage_Categories.MediaTagID' => 1])->Limit(3);
}
Then in your template, you can use the format() methods, eg: $Date.Format('MM')