Skip to content
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

Closed
natanfelles opened this issue Jul 20, 2020 · 5 comments
Closed

Bug: Model can not insert Entity #3368

natanfelles opened this issue Jul 20, 2020 · 5 comments
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@natanfelles
Copy link
Contributor

natanfelles commented Jul 20, 2020

Describe the bug

Model is not inserting entity values.

The following code:

<?php namespace App\Controllers;

use App\Entities\User;
use App\Models\Users;

class Home extends BaseController
{
	public function index()
	{
		$user = new User([
			'name' => 'John Doe',
			'email' => 'jd@mail.com',
			'password' => 'secret',
		]);

		$users = new Users();
		$users->insert($user);
	}
}

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

  • OS: Ubuntu
  • Web server: PHP
  • PHP version: 7.4.8
@natanfelles natanfelles added the bug Verified issues on the current code behavior or pull requests that will fix them label Jul 20, 2020
@natanfelles
Copy link
Contributor Author

@michalsn
Copy link
Member

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?

@natanfelles
Copy link
Contributor Author

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 fake is called to test:

<?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);
    }
} 

@lonnieezell
Copy link
Member

Actually, the following code example is not how it should work for quite a while:

<?php namespace App\Models;

use CodeIgniter\Entity;

class User extends Entity
{
    protected $id;
    protected $name;
    protected $email;
    protected $password;
} 

You should not specify them as class properties. All you need is:


use CodeIgniter\Entity;

class User extends Entity
{

} 

The rest will be handled by the class, as they are put in the $attributes array.

@yassinedoghri
Copy link
Contributor

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.
So, correct me if I'm wrong, but should there be a new stable version to "restore" the docs?

Anyways, thanks for the fix @michalsn!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

4 participants