1

Is there no __constructor() to run on an entity class i doctrine (symfony 5)?

I'm trying to initialize some default values for my entity, and then override them with parameters from the services.yaml file if they are present there. But commenting out the entire __construct makes no difference at all...

<?php

namespace App\Entity\Poslog;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

/**
 * Operator
 *
 * @ORM\Entity(repositoryClass="App\Repository\OperatorRepository")
 * @ORM\Table(name="operator")
 * @ORM\Entity
 */
class Operator
{

    private $DateEndorsementWarningDefault = 60;
    private $DateOjtiWarningDefault = 60;
    private $DateAssessorWarningDefault = 60;
    private $DateOtherLanguageWarningDefault = 60;
    private $DateEnglishWarningDefault = 60;
    private $DateMedicalWarningDefault = 60;
    private $LastXDaysDefault = 90; //Default for the constraint which checks whether an operator has had any time in position at all during X days
    
    public function __construct(ParameterBagInterface $params) 
    {

        //set the default parameters (if nothing defined in services.yaml)
        $this->globalParameters = $params;
        $this->globalParameters->has('poslog_DateEndorsementWarningDefault') ? $this->DateEndorsementWarningDefault = $this->globalParameters->get('poslog_DateEndorsementWarningDefault') : $this->DateEndorsementWarningDefault = 60;
        $this->globalParameters->has('poslog_DateOjtiWarningDefault') ? $this->DateOjtiWarningDefault = $this->globalParameters->get('DateOjtiWarningDefault') : $this->DateEndorsementWarningDefault = 60;
        $this->globalParameters->has('poslog_DateAssessorWarningDefault') ? $this->DateAssessorWarningDefault = $this->globalParameters->get('poslog_DateAssessorWarningDefault') : $this->DateAssessorWarningDefault = 60;
        $this->globalParameters->has('poslog_DateOtherLanguageWarningDefault') ? $this->DateOtherLanguageWarningDefault = $this->globalParameters->get('poslog_DateOtherLanguageWarningDefault') : $this->DateOtherLanguageWarningDefault = 60;
        $this->globalParameters->has('poslog_DateEnglishWarningDefault') ? $this->DateEnglishWarningDefault = $this->globalParameters->get('poslog_DateEnglishWarningDefault') : $this->DateEnglishWarningDefault = 60;
        $this->globalParameters->has('poslog_DateMedicalWarningDefault') ? $this->DateMedicalWarningDefault = $this->globalParameters->get('poslog_DateMedicalWarningDefault') : $this->DateMedicalWarningDefault = 60;
        $this->globalParameters->has('poslog_LastXDaysDefault') ? $this->LastXDaysDefault = $this->globalParameters->get('poslog_LastXDaysDefault') : $this->LastXDaysDefault = 90;

    }
Matt Welander
  • 8,234
  • 24
  • 88
  • 138
  • do you call `new Operator($params)` anywhere? If not, then why would the constructor be called? – craigh Sep 03 '22 at 20:40
  • Yeah, keep in mind that as stated above, entities are excluded from autowiring. If you need something like this you'd have to resort to factories that can be autowired. – msg Sep 04 '22 at 10:18
  • And Sending the entire ParameterBag to your entity seems kind of odd to me. – Julien B. Sep 05 '22 at 13:50
  • And just for info, Doctrine itself uses reflection to directly set the property values when loading from the database. Neither the constructor nor the setters are ever called. So if you were hoping for some sort of merge process while fetching then the hope would have been in vain. – Cerad Sep 05 '22 at 15:50

0 Answers0