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

(This post was last modified: 09-21-2020, 01:23 AM by tomasoma.)

Ok sorry for the delay here is my working code :

Model :
PHP Code:
<?php namespace App\Models ;

use 
CodeIgniter\Model ;

class 
UserModel extends Model {

    protected 
$table 'user';
    protected 
$primaryKey 'id';
    protected 
$returnType 'App\Entities\User';
    protected 
$useSoftDeletes true ;
    protected 
$allowedFields = ['email','password','level','civilite','prenom','nom','sign','tel','add1','add2','cp','ville','pays'] ;
    protected 
$dateFormat 'int' ;
    protected 
$useTimestamps true ;
    protected 
$createdField 'created_at' ;
    protected 
$updatedField 'updated_at' ;
    protected 
$deletedField 'deleted_at' ;
    protected 
$validationRules = [] ;
    protected 
$validationMessages = [] ;
    protected 
$skipValidation false ;



Controller :
PHP Code:
<?php namespace App\Controllers;

class 
Users extends BaseController {

    private 
$UM # pour le user model pas être obligé de le rappeler à chaque fonction 

    /*****************************************************
        __construct
    *****************************************************/
    
public function __construct(){
        
$this->UM model('App\Models\UserModel') ; 
    }

    
/*****************************************************
        index
    *****************************************************/
    
public function index(){
        
$vars = [
            
'dom_id' => random_string('alpha',13) ,
            
'table' => '' ,
            
'pagination' => ''
        
] ;
        
$users $this->UM->findAll() ;
        if(
$users) {
            
$table = new \CodeIgniter\View\Table() ;
            
$table->setTemplate(['table_open' => '<table class="table table-hover table-striped">']) ;
            
$table->setHeading(['','id','email','level','identite','adresse','tel','created_at','updated_at','deleted_at']) ;
            foreach(
$users as $user){
                
$table->addRow([
                    
wrap(
                        
admin_show_btn('users',$user->id)
                        .
admin_edit_btn('users',$user->id)
                        .
admin_supprim_btn('users',$user->id)
                        ,[
'class' => 'btn-group btn-group-sm']
                    ) ,
                    
$user->id ,
                    
$user->email ,
                    
$user->level ,
                    
$user->identite() ,
                    
$user->adresse() ,
                    
$user->tel ,
                    
$user->created_at ,
                    
$user->updated_at ,
                    
$user->deleted_at
                
]) ; 
            }
            
$vars['table'] = $table->generate() ; 
        }
        else 
$vars['table'] = alert('warning','Aucun utilisateur trouvé') ; 

        return 
$this->tpl('users/index',$vars,'content')
            ->
set_var('title',__CLASS__.'::'.__FUNCTION__)
            ->
html() ; 
    } 
# /index 

Entity :
PHP Code:
<?php namespace App\Entities ;

use 
CodeIgniter\Entity ;
use 
CodeIgniter\I18n\Time;

class 
User extends Entity {
    
    protected 
$attributes = [
        
'id' => null ,
        
'email' => null ,
        
'password' => null ,
        
'level' => null ,
        
'civilite' => null ,
        
'prenom' => null ,
        
'nom' => null ,
        
'sign' => null ,
        
'tel' => null ,
        
'add1' => null ,
        
'add2' => null ,
        
'cp' => null ,
        
'ville' => null ,
        
'pays' => null ,
        
'created_at' => null ,
        
'updated_at' => null ,
        
'deleted_at' => null
    
] ; 

    protected 
$dates = [] ; # !! declare a empty array

    
public function identite(){
        return 
trim($this->civilite.' '.ucwords($this->prenom)).' '.strtoUpper($this->nom) ;
    } 
# /identite() 

    
public function adresse()
    {
        
$t $this->add1 ;
        if(
trim($this->add2) != ''$t .= '<br >/'.$this->add2 ;
        
$t .= '<br />'.$this->cp.' '.strtoupper($this->ville) ;
        if(
trim(strtolower($this->pays)) != 'france'$t .= '<br />'.$this->pays ;
        return 
$t ;
    } 
# /adresse() 

    
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()

    
public function getUpdatedAt(string $format 'd/m/Y H:i:s')
    {
        
$this->attributes['updated_at'] = $this->mutateDate($this->attributes['updated_at']) ;
        
$timezone $this->timezone ?? app_timezone() ;
        
$this->attributes['updated_at']->setTimezone($timezone) ; 
        return 
$this->attributes['updated_at']->format($format) ;
    } 
# /getUpdatedAt

    
public function getDeletedAt(string $format 'd/m/Y H:i:s')
    {
        if(
$this->attributes['deleted_at'] == null) return false ;
        
$this->attributes['deleted_at'] = $this->mutateDate($this->attributes['deleted_at']) ;
        
$timezone $this->timezone ?? app_timezone() ;
        
$this->attributes['deleted_at']->setTimezone($timezone) ; 
        return 
$this->attributes['deleted_at']->format($format) ;
    } 
# /getDeletedAt

}    #    EOC Entity\User 

(09-18-2020, 01:27 PM)nicojmb Wrote:
(09-18-2020, 01:10 PM)InsiteFX Wrote:
(09-18-2020, 11:06 AM)nicojmb Wrote: hi, i try to do the same but does't work, can you share you working code?

Regards!

PHP Code:
The $useTimestamps automatically fills the $date array with the $createdField $updatedField and the $deletedFields

So set this to true.


PHP Code:
/**
 * If true, will set created_at, and updated_at
 * values during insert and update routines.
 *
 * @var boolean
 */
 
protected $useTimestamps false

Not work, i use custom model extended from MyTh.

I have tried everything, $useTimestamps to "true", $useTimestamps to "false", with $dates array, etc...

PHP Code:
namespace App\Models;

use 
Myth\Auth\Models\UserModel as BaseModel;

class 
UserModel extends BaseModel
{  

    
protected $dates = [];

    protected $returnType = \App\Entities\User::class;

    protected $useTimestamps true;


It is not in the model that you declare $date array empty, it is in the Entity class Wink

It works for display but i didn't get further yet, I hope that the empty $date array will not prevent from CI to automatically update the value at creation, update and delete action as it is supposed to do ... I'll check it when I write my create,edit,delete function in my controller...

I think our problem is more from the __get() function of the system Entity class ... but noone reply on this point.

Sorry for the delay, I'm busy on many projects and I'm just working on CI4 when I have time to learn how to deal with it for the futur.
Reply


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



Theme © iAndrew 2016 - Forum software by © MyBB