-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: Model can not insert Entity #3368
Comments
I couldn't reproduce your error. In the linked forum thread you're saying something about custom Entity class. Could you please provide us your Model and Entity? |
Hello @michalsn ! Here is the model: <?php namespace App\Models;
use CodeIgniter\Model;
use Faker\Generator;
class Users extends Model
{
protected $table = 'users';
protected $allowedFields = [
'name',
'email',
'password',
];
protected $useTimestamps = true;
protected $returnType = User::class;
public function fake(Generator &$faker)
{
return [
'name' => $faker->name,
'email' => $faker->email,
'password' => password_hash('password', PASSWORD_DEFAULT),
];
}
} And here is the entity, return type of the model: <?php namespace App\Models;
use CodeIgniter\Entity;
class User extends Entity
{
protected $id;
protected $name;
protected $email;
protected $password;
} And the controller, where the <?php namespace App\Controllers;
use App\Models\Users as UsersModel;
class Home extends BaseController
{
public function index()
{
helper('test');
$user = fake(UsersModel::class);
var_dump($user);
}
} |
Actually, the following code example is not how it should work for quite a while:
You should not specify them as class properties. All you need is:
The rest will be handled by the class, as they are put in the |
Hello, I've encountered a similar issue. Basically, the Handling Business Logic section in the docs doesn't work anymore in 4.0.4. If I understood well, it's a regression as it was working fine in previous versions. I've updated CodeIgniter to the latest develop changes after seeing that it was fixed in #3377 and it works fine now. Anyways, thanks for the fix @michalsn! |
Describe the bug
Model is not inserting entity values.
The following code:
throws the exception:
CodeIgniter\Database\Exceptions\DataException
There is no data to insert.
CodeIgniter 4 version
4.0.4
Affected module(s)
Model/Entity
Expected behavior, and steps to reproduce if appropriate
Expect the data to be inserted.
Context
The text was updated successfully, but these errors were encountered: