Call spesific function in UsersTable from UsersController

I have create function time zone Asia/Indonesia/Jakarta

Entity\User.php

namespace App\Model\Entity;
use Cake\ORM\Entity;
use Cake\I18n\Time;

public static function waktuDibuat()
{
   Time::setToStringFormat('dd-MM-yyyy HH:mm:ss');
   $Dibuat = Time::now();
}

then I call function

waktuDibuat();

from

Controller\UsersController.php

namespace App\Controller;
use App\Controller\AppController;
use App\Model\Table\UsersTable;

27   public function add()
28   {
29	$setWaktu = UsersTable::waktuDibuat();
30	$user = $this->Users->newEntity();
31         if ($this->request->is('post')) {
32		$user = $this->Users->patchEntity($user, $this->request->getData()); 
33                 if ($this->Users->save($user)) {
34                   $this->Flash->success(__('Pengguna berhasil ditambahkan.'));
35                       return $this->redirect(['action' => 'index']);
36                  }
37             $this->Flash->error(__('Pengguna gagal disimpan, Coba lagi.'));
38           }
39        $this->set(compact('user'));
40    }

The messeage shown error is

Call to undefined method App\Model\Table\UsersTable::waktuDibuat()
Error in: ROOT\src\Controller\UsersController.php, line 29 

I hope someone help me to fix this line error, thanx

What does this $Dibuat value do?
like, it doesn’t get returned and neither is it a class property?

also, is your method waktuDibuat placed inside the class UsersTable, because it it’s not, then it’s not part of the class UsersTable thus doesn’t exists as UsersTable::waktuDibuat but as just waktuDiabuat

class UsersTable extends Table {
  public static function waktuDibuat() {
    // ... do stuff
  }
}

also, I don’t know what the method is supposed to do exactly (but it seems like just format the time for when a user is created), but instead of adding it to the UsersTable, maybe just add it as a value in your entity instead (even though, just using the Time::now will suffice)?
Ofcourse, don’t forget to add the appropriate column to your table and model :slight_smile:

public function add() {
  $user = $this->Users->newEntity();
  $user->dibuat = (Time::now())->i18nFormat('dd-MM-yyyy HH:mm:ss');
}

thanx FinlayDaG33k for you answer,…your answer is working successfull,…thanx ya

1 Like