How to get data after saving the model - Seva - 01-27-2022
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();
RE: How to get data after saving the model - castle - 01-27-2022
I may be too newbie, but what's the case for that? I'm wondering why would you save the data and retrieve it right back. If the data hasn't changed, can't you just save the $data to another variable to use it later? Unless, of course, you need the ID of that entry. If that is the case, you can return the ID when you add $data to DB. See if this is what you need https://stackoverflow.com/questions/61144337/how-to-get-insert-id-after-save-to-database-in-codeigniter-4
RE: How to get data after saving the model - davis.lasis - 01-27-2022
You should read about entities: https://codeigniter4.github.io/CodeIgniter4/models/entities.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'; }
}
RE: How to get data after saving the model - Seva - 01-27-2022
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.
RE: How to get data after saving the model - Seva - 01-27-2022
(01-27-2022, 05:15 AM)davis.lasis Wrote: You should read about entities: https://codeigniter4.github.io/CodeIgniter4/models/entities.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?
RE: How to get data after saving the model - davis.lasis - 01-28-2022
(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/CodeIgniter4/models/entities.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.
RE: How to get data after saving the model - Seva - 01-28-2022
(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/CodeIgniter4/models/entities.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
|