0
votes

I am programming a timemanagement extension in Extbase (TYPO3 8). In my model I have the two properties startdate and enddate. Now the duration should be calculated from these properties and stored in the database. After creating the new property duration with the extension builder, I changed my model like this:

/**
* duration
*
* @var int
*/
protected $duration = 0;


/**
* Returns the duration
*
* @return int $duration
*/
public function getDuration()
{
    return $this->duration;
}

/**
* Sets the duration
*
* @return void
*/
public function setDuration($duration)
{
return $this->duration = intval($this->enddate->format('U')) - intval($this->startdate->format('U'));
}

But the calculated time is not inserted into the database.

What am I doing wrong?

5

5 Answers

1
votes

I think your approach to store the duration into the DB is already wrong. Don't store redundant information!

You should store the fields startdate and enddate into the database, but not the duration as it can be calculated on the fly.

Redundant information is a typical source of trouble and bugs.

0
votes

There is a field "duration" in the database (or if it is called in a different way - is there a mapping from duration to this name?). In addition: Is there a TCA for field duration? Is everything else stored as expected? If not, please also post your code where the model is called and persisted (e.g. the controller action). What if you debug your object just before persisting? Is the property duration filled correctly?

0
votes

Did you clear the red cache in TYPO3 (Clear all caches)? This is required after changing your model.

Also the setter methods are not used automaticly in extbase. So if you dont call ->setDuration() in your code, its not called at all. The data mapper of extbase use the method _setProperty to set the value for each property.

You can call the method ->setDuration() in your code befor writing into database (update/add):

$model->setDuration(0);
$repository->add($model);

$model->setDuration(0);
$repository->update($model);
0
votes

What you actually want to do is not store this redundant information at all.

/**
* Returns the duration
*
* @return int $duration
*/
public function getDuration()
{
    return $this->getStartdate()->diff($this->getEnddate())->seconds;
}
-1
votes

You do not need to store such values in database. You have to configure duration in TCA as "passthrough". In your model you define getter and setter as usual. Now you can calculate the duration in your controller, something like this:

$start = $this->yourRepository->getStartdate();
$end = $this->yourRepository->getEnddate();
$duration = $end->format('U') - $start->format('U');
$this->yourRepository->setDuration( $duration);

You can do this in your initializeAction to have the value calculated in every action.