-
tomasoma Junior Member
 
-
Posts: 16
Threads: 6
Joined: Feb 2015
Reputation:
0
Hello,
I just followed the documentation about the Entity getCreatedAt() function (on this page https://codeigniter4.github.io/userguide...ness-logic) but it don't understand why the date format i set is ignored :
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 ;
}
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 = ['created_at', 'updated_at', 'deleted_at'];
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) ; 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
}
Controller :
PHP Code: <?php namespace App\Controllers;
class Home extends BaseController { public function index() {
$vars = [ 'tac' => 'blabla' ] ; return view('welcome_message',$vars); }
public function test() { $dom_id = random_string('alpha',13) ;
$config = config('Config\Database') ;
$content = '' ;
$user_model = model('App\Models\UserModel') ; $users = $user_model->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([ $user->id , $user->email , $user->level , $user->identite() , $user->adresse() , $user->tel , $user->created_at , $user->updated_at , $user->deleted_at ]) ; } $content .= $table->generate() ; } else $content .= wrap('Aucun user trouvé par le findAll',['balise' => 'code']) ;
$vars = [ 'title' => 'MON BLABLA '.__CLASS__.'::'.__FUNCTION__ , 'content' => wrap($content ,['id' => $dom_id]) ] ; $params = (object)[ 'dom_id' => $dom_id ] ;
$this->scripts->addPath(base_url('boot/not_compiled/home-test-0.0.1.js')) ; $this->scripts->init('home_test',$params) ; return $this->html($vars) ; }
}
and in the HTML table, the dates created_at and updated_at still display in the 'Y-d-m H:i:s' format.
Where did I make a mistake ?
|