Welcome Guest, Not a member yet? Register   Sign In
date auto getter in Entity ignore my date format
#3

(This post was last modified: 09-18-2020, 02:35 AM by tomasoma.)

(09-18-2020, 01:04 AM)InsiteFX Wrote: You need to add them to the models allow fields array.

PHP Code:
$allowedFields = ['created_at''updated_at'// etc; 

Thanks for trying but It doesn't change anything.

I added a log_message() to check if my own function was called and it is, with or without the add in the allowedFields array.

PHP Code:
    public function getCreatedAt(string $format 'd/m/Y H:i:s')
    {
        
$this->attributes['created_at'] = $this->mutateDate($this->attributes['created_at']) ;
        
$timezone $this->timezone ?? app_timezone() ;
        
$this->attributes['created_at']->setTimezone($timezone) ; 
        
log_message('error',__CLASS__.'::'.__FUNCTION__.'() l:'.__LINE__' I’m done') ;
        return 
$this->attributes['created_at']->format($format) ;
    } 
# /getCreatedAt() 

I also change the return line with the string format :

PHP Code:
return $this->attributes['created_at']->format('d/m/Y H:i:s') ; 

and it still doesn't work

but when i include a mistake in the date format string :
PHP Code:
return $this->attributes['created_at']->format('d/m/Y H:i:xs') ; 
then Exception error ...

so I'm trying to find this native ...->format() function to understand why it 'partially' ignore the argument....

From the logs of the error generated by the 'mistake' in the date format, it looks like my own getCreatedAt() is called and then overridden by ??? the default one ???

Code:
CRITICAL - 2020-09-18 11:01:00 --> DateTime::__construct(): Failed to parse time string (12:/04/1981 01:15:00) at position 0 (1): Unexpected character
#0 /var/www/clients/client0/common-libraries/codeigniter.versions/framework-4.0.4/system/I18n/Time.php(138): DateTime->__construct('12:/04/1981 01:...', Object(DateTimeZone))
#1 /var/www/clients/client0/common-libraries/codeigniter.versions/framework-4.0.4/system/I18n/Time.php(174): CodeIgniter\I18n\Time->__construct('12:/04/1981 01:...', 'Europe/Berlin', NULL)
#2 /var/www/clients/client0/common-libraries/codeigniter.versions/framework-4.0.4/system/Entity.php(500): CodeIgniter\I18n\Time::parse('12:/04/1981 01:...')
#3 /var/www/clients/client0/common-libraries/codeigniter.versions/framework-4.0.4/system/Entity.php(301): CodeIgniter\Entity->mutateDate('12:/04/1981 01:...')
#4 /var/www/clients/client0/web22/private/app/Controllers/Home.php(59): CodeIgniter\Entity->__get('created_at')
#5 /var/www/clients/client0/common-libraries/codeigniter.versions/framework-4.0.4/system/CodeIgniter.php(918): App\Controllers\Home->test()
#6 /var/www/clients/client0/common-libraries/codeigniter.versions/framework-4.0.4/system/CodeIgniter.php(404): CodeIgniter\CodeIgniter->runController(Object(App\Controllers\Home))
#7 /var/www/clients/client0/common-libraries/codeigniter.versions/framework-4.0.4/system/CodeIgniter.php(312): CodeIgniter\CodeIgniter->handleRequest(NULL, Object(Config\Cache), false)
#8 /var/www/clients/client0/web22/web/index.php(45): CodeIgniter\CodeIgniter->run()
#9 {main}

when i call
PHP Code:
$user->getCreatedAt() ; 
it works fine, but I think it was what the "auto getter" was done to avoid .?.?.

So I checked the System/Entity->__get() :

PHP Code:
public function __get(string $key)
{
    
$key    $this->mapProperty($key);
    
$result null;
    
// Convert to CamelCase for the method
    
$method 'get' str_replace(' '''ucwords(str_replace(['-''_'], ' '$key)));

    
// if a set* method exists for this key,
    // use that method to insert this value.
    
if (method_exists($this$method))
    {
        
$result $this->$method();
    }
    
// Otherwise return the protected property
    // if it exists.
    
else if (array_key_exists($key$this->attributes))
    {   
        
$result $this->attributes[$key];
    }
    
// Do we need to mutate this into a date?
    
if (in_array($key$this->dates))
    {
        
$result $this->mutateDate($result);
    }
    
// Or cast it as something?
    
else if ($this->_cast && ! empty($this->casts[$key]))
    {    
        
$result $this->castAs($result$this->casts[$key]);
    }    
    return 
$result;


it calls my method and then if the $key is in the $date array it mutates my result into a date one more time.
So I removed my $date array
PHP Code:
// protected $dates = ['created_at', 'updated_at', 'deleted_at']; 
... still doesn't work

So I empty my date array :
PHP Code:
protected $dates = [] ; 

WOOOW ok finally .... I got my result !!!!

So I guess that the $useTimestamps automatically fills the $date array with the $createdField , $updatedField and the $deletedFields.

Solved.

CI4 is great but its documentation is not yet.
Reply


Messages In This Thread
RE: date auto getter in Entity ignore my date format - by tomasoma - 09-18-2020, 02:01 AM



Theme © iAndrew 2016 - Forum software by © MyBB