Date database field gets casted to string

Silverstripe Version: 4.13

Question: Database Date field is a string?

I’m currently trying to format a date with the →format function on my date object but I get an Call to a member function format() on string error

<?php

use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\DateField;
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\DataObject;

class EventDate extends DataObject {
    private static $singular_name = "Veranstaltungsdatum";
    private static $plural_name = "Veranstaltungsdaten";

    private static $db = [
        'city' => 'Text',
        'from_date' => 'Date',
        'to_date' => 'Date',
        'is_online' => 'Boolean'
    ];

    private static $has_one = [
        "Page" => Page::class
    ];

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab(
            "Root.Main",
            TextField::create('city', 'Stadt')
        );
        $fields->addFieldToTab(
            "Root.Main",
            DateField::create('from_date', 'Von')
        );
        $fields->addFieldToTab(
            "Root.Main",
            DateField::create('to_date', 'Bis')
        );
        $fields->addFieldToTab(
            "Root.Main",
            CheckboxField::create('is_online', 'Online Veranstaltung')
        );
        return $fields;
    }

    // Custom title funktion für die formulare
    public function getTitle() {
        if($this->is_online) {
            return $this->dbObject("from_date")->format("dd.MM.YYYY") . " bis " . $this->to_date->format("dd.MM.YYYY") . " | Online";
        }
        return $this->getField("from_date")->format("dd.MM.YYYY") . " bis " . $this->to_date->format("dd.MM.YYYY") . " | " . $this->city;
    }
}

Is there something missing here or is this a but with 4.13? I know its out of support but the version is locked until development on this feature is complete

Which line is throwing the error? You’re calling format() on a few different things there (including getField() which could be returning a string)

I would have thought something like this should work:

return sprintf("%s bis %s | Online", 
  $this->from_date->format("dd.MM.YYY"), 
  $this->to_date->format("dd.MM.YYYY")
);

I tried your solution before, it fails on $this->from_date->format(“d.m.Y“) and $this->to_date->format(“d.m.Y“)

I now have found workaround but I’d still like to know why my date object isn’t beeing casted corrently

$from = DateTime::createFromFormat("Y-m-d", $this->from_date);
$to = DateTime::createFromFormat("Y-m-d", $this->to_date);

The code above uses fancy quotes. You must use " instead of .

That is the formatting of this forum, not my code.