-
Seva
Newbie
-
Posts: 9
Threads: 3
Joined: Nov 2020
Reputation:
0
Run
Now I want to get all the fields and values of the record of this model that was saved/updated.
Query the database again?
PHP Code: $lastInsertId = $userModel->getLastInsertID();
$user = $userModel->where('id', $lastInsertId)->first();
-
davis.lasis
Member
-
Posts: 57
Threads: 3
Joined: Oct 2018
Reputation:
4
You should read about entities: https://codeigniter4.github.io/CodeIgnit...ities.html
database table users:
id - integer
username - string
email - string
password - string
created_at - datetime
PHP Code: <?php
namespace App\Models;
use CodeIgniter\Model; use App\Entities\UserEntity;
class UserModel extends Model { protected $table = 'users'; protected $allowedFields = [ 'username', 'email', 'password', ]; protected $returnType = UserEntity::class; protected $useTimestamps = true; }
PHP Code: <?php
namespace App\Entities;
use CodeIgniter\Entity\Entity;
class UserEntity extends Entity { // ... }
PHP Code: <?php
namespace App\Controllers;
use CodeIgniter\Controller; use App\Entities\UserEntity; use App\Models\UserModel;
class UserController extends Controller { public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { // Do Not Edit This Line parent::initController($request, $response, $logger);
}
public function insertUser() { $insertData = [ 'username' => 'John', 'email' => '[email protected]', 'password' => 'top_secret', ];
$userEntity = new UserEntity(); $userModel = new UserModel();
$userEntity->fill($insertData);
$userModel->save($userEntity); // insert as ID is not provided
// result is inserted userEntity object $userEntity->id = 1; $userEntity->username = 'John'; $userEntity->email = '[email protected]'; $userEntity->password = 'top_secret';
// as UserModel has $useTimestamps = true $userEntity->created_at = '2022-01-27 14:06:52'; }
public function updateUser(int $id = 1) { $updateData = [ 'username' => 'Jane', 'email' => '[email protected]', 'password' => 'qwerty', ];
$userModel = new UserModel();
$userEntity = $userModel->find($id);
$userEntity->fill($updateData);
$userModel->save($userEntity); // update as ID is set
// result is updated userEntity object $userEntity->id = 1; $userEntity->username = 'Jane'; $userEntity->email = '[email protected]'; $userEntity->password = 'qwerty';
// as UserModel has $useTimestamps = true $userEntity->created_at = '2022-01-27 14:06:52'; }
}
-
Seva
Newbie
-
Posts: 9
Threads: 3
Joined: Nov 2020
Reputation:
0
01-27-2022, 07:41 AM
(This post was last modified: 01-27-2022, 07:51 AM by Seva.)
Created_at, updated_at this is what i want to get.
With the use of entity these fields are - null.
I see only the data that I directly transfer in the array for saving.
Model class automatically sets values for fields created_at, updated_at.
With the use of the entity class, these fields are empty, but maybe I'm doing something wrong.
-
Seva
Newbie
-
Posts: 9
Threads: 3
Joined: Nov 2020
Reputation:
0
01-27-2022, 11:29 AM
(This post was last modified: 01-27-2022, 11:29 AM by Seva.)
(01-27-2022, 05:15 AM)davis.lasis Wrote: You should read about entities: https://codeigniter4.github.io/CodeIgnit...ities.html
database table users:
id - integer
username - string
email - string
password - string
created_at - datetime
PHP Code: <?php
namespace App\Models;
use CodeIgniter\Model; use App\Entities\UserEntity;
class UserModel extends Model { protected $table = 'users'; protected $allowedFields = [ 'username', 'email', 'password', ]; protected $returnType = UserEntity::class; protected $useTimestamps = true; }
PHP Code: <?php
namespace App\Entities;
use CodeIgniter\Entity\Entity;
class UserEntity extends Entity { // ... }
PHP Code: <?php
namespace App\Controllers;
use CodeIgniter\Controller; use App\Entities\UserEntity; use App\Models\UserModel;
class UserController extends Controller { public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { // Do Not Edit This Line parent::initController($request, $response, $logger);
}
public function insertUser() { $insertData = [ 'username' => 'John', 'email' => '[email protected]', 'password' => 'top_secret', ];
$userEntity = new UserEntity(); $userModel = new UserModel();
$userEntity->fill($insertData);
$userModel->save($userEntity); // insert as ID is not provided
// result is inserted userEntity object $userEntity->id = 1; $userEntity->username = 'John'; $userEntity->email = '[email protected]'; $userEntity->password = 'top_secret';
// as UserModel has $useTimestamps = true $userEntity->created_at = '2022-01-27 14:06:52'; }
public function updateUser(int $id = 1) { $updateData = [ 'username' => 'Jane', 'email' => '[email protected]', 'password' => 'qwerty', ];
$userModel = new UserModel();
$userEntity = $userModel->find($id);
$userEntity->fill($updateData);
$userModel->save($userEntity); // update as ID is set
// result is updated userEntity object $userEntity->id = 1; $userEntity->username = 'Jane'; $userEntity->email = '[email protected]'; $userEntity->password = 'qwerty';
// as UserModel has $useTimestamps = true $userEntity->created_at = '2022-01-27 14:06:52'; }
}
Couldn't implement it.
I can't really get created_at
Can anyone help?
-
davis.lasis
Member
-
Posts: 57
Threads: 3
Joined: Oct 2018
Reputation:
4
(01-27-2022, 07:41 AM)Seva Wrote: Created_at, updated_at this is what i want to get.
With the use of entity these fields are - null.
I see only the data that I directly transfer in the array for saving.
Model class automatically sets values for fields created_at, updated_at.
With the use of the entity class, these fields are empty, but maybe I'm doing something wrong. You should read about entities: https://codeigniter4.github.io/CodeIgnit...ities.html
add new field to database table 'updated_at'
database table users:
id - integer
username - string
email - string
password - string
updated_at - datetime
created_at - datetime
PHP Code: <?php
namespace App\Models;
use CodeIgniter\Model; use App\Entities\UserEntity;
class UserModel extends Model { protected $table = 'users'; protected $allowedFields = [ 'username', 'email', 'password', 'updated_at', ]; protected $returnType = UserEntity::class; protected $useTimestamps = true; }
The model uses the users table in the database for all of its activities. We’ve set the $allowedFields property to include all of the fields that we want outside classes to change. The id, created_at, and updated_at fields are handled automatically by the class or the database, so we don’t want to change those. Finally, we’ve set our Entity class as the $returnType. This ensures that all methods on the model that return rows from the database will return instances of our User Entity class instead of an object or array like normal.
PHP Code: <?php
namespace App\Entities;
use CodeIgniter\Entity\Entity;
class UserEntity extends Entity { protected $dates = ['created_at', 'updated_at']; }
Read manuals and documentation carefully.
-
Seva
Newbie
-
Posts: 9
Threads: 3
Joined: Nov 2020
Reputation:
0
(01-28-2022, 10:53 AM)davis.lasis Wrote: (01-27-2022, 07:41 AM)Seva Wrote: Created_at, updated_at this is what i want to get.
With the use of entity these fields are - null.
I see only the data that I directly transfer in the array for saving.
Model class automatically sets values for fields created_at, updated_at.
With the use of the entity class, these fields are empty, but maybe I'm doing something wrong. You should read about entities: https://codeigniter4.github.io/CodeIgnit...ities.html
add new field to database table 'updated_at'
database table users:
id - integer
username - string
email - string
password - string
updated_at - datetime
created_at - datetime
PHP Code: <?php
namespace App\Models;
use CodeIgniter\Model; use App\Entities\UserEntity;
class UserModel extends Model { protected $table = 'users'; protected $allowedFields = [ 'username', 'email', 'password', 'updated_at', ]; protected $returnType = UserEntity::class; protected $useTimestamps = true; }
The model uses the users table in the database for all of its activities. We’ve set the $allowedFields property to include all of the fields that we want outside classes to change. The id, created_at, and updated_at fields are handled automatically by the class or the database, so we don’t want to change those. Finally, we’ve set our Entity class as the $returnType. This ensures that all methods on the model that return rows from the database will return instances of our User Entity class instead of an object or array like normal.
PHP Code: <?php
namespace App\Entities;
use CodeIgniter\Entity\Entity;
class UserEntity extends Entity { protected $dates = ['created_at', 'updated_at']; }
Read manuals and documentation carefully
I do everything as you wrote, but fields id, created_at, updated_at of entity return is empty when saving the model (insert/update)
PHP Code: $userModel = new UserModel(); $user = new UserEntity();
$user->fill([ 'name' => 'someName', 'email' => 'someEmail', 'password' => 'qwerty' ]);
$userModel->save($user); // insert
echo $user->id; // null echo $user->name; // someName echo $user->email; // someEmail echo $user->password; // qwerty echo $user->created_at; // null echo $user->updated_at; // null
|