Welcome Guest, Not a member yet? Register   Sign In
Entity fill bug
#1

(This post was last modified: 03-02-2022, 08:31 AM by gt99.)

I seem to have noticed another bug in the fill method.
I apologize in advance for my English, it's all a translator Wink
The thing is that there are some fields in the API that are not required for the Entity to work and I was hoping that fill would just discard them.
But it's not.
One of the fields that comes in json "attributes" is also a setting with the same name in Etity. And if I call fill - then everything is fine until I try to create a record in the database.

This is what my controller looks like
PHP Code:
public function create()
{
        // Validate info
        //Check that the name is longer than 3 letters
        //Check for the presence of the manufacturer in the database
        //Check if the model exists in the database
        //Check if there are attributes in the database
        //Check if there are photos in the database
        
        
//Connecting everything
        $response = new ResponseApi($this->response);
        $validation =  Services::validation();
        $requestData $this->request->getJSON(true);
        
        
//Validate according to the rules
        $validation->reset();
        $validation->setRuleGroup('productCreate');
        
        
if(!$validation->run($requestData))
        {
            return $response->bad($validation->getErrors());
        }
        
        
//I create a product
        $productModel = new Product_Model();
        $productId $productModel->create($requestData);
        ...



Model
PHP Code:
<?php

namespace App\Models;

//use App\Models\ProductAttribute as ProductAttribute_Model;
//use App\Models\ProductPhoto as ProductPhoto_Model;
use CodeIgniter\Model;
use 
App\Entities\Product as Product_Entity;

class 
Product extends Model
{
    protected $table      'products';
    protected $primaryKey 'id';

    protected $useAutoIncrement true;

    protected $returnType    'App\Entities\Product';
    protected $useSoftDeletes false;

    protected $allowedFields = ['name''manufacturer_id''model_id''type_id'];

    protected $useTimestamps false;
    protected $createdField  'created_at';
    protected $updatedField  'updated_at';
    protected $deletedField  'deleted_at';

    public function create($data)
    {
        $productEntity = new Product_Entity($data);
        return $this->insert($productEntity);
    }

  ...




Entity
PHP Code:
<?php namespace App\Entities;

use 
CodeIgniter\Entity\Entity;
use 
App\Helpers\EntityModify;

class 
Product extends EntityModify
{
    protected $dates = ['created_at''updated_at''deleted_at'];

    protected $attributes = [
        'id'                => null,
        'name'              => null,
        'manufacturer_id'  => null,
        'model_id'          => null,
        'type_id'          => null
    
];

    protected $casts = [
        'id'                => 'integer',
        'name'              => 'string',
        'manufacturer_id'  => 'integer',
        'model_id'          => 'integer',
        'type_id'          => 'integer'
    ];

  protected $datamap = [
        'manufacturerId'  => 'manufacturer_id',
        'modelId'          => 'model_id',
        'typeId'          => 'type_id'
    ];



While I was using "Entity" I got the following error
Code:
    "title": "CodeIgniter\\Database\\Exceptions\\DataException",
    "type": "CodeIgniter\\Database\\Exceptions\\DataException",
    "code": 500,
    "message": "There is no data to insert.",
    "file": "/home/a0272576/domains/bunker-vape.ru/public_html/api/system/BaseModel.php",
    "line": 716


But this error occurred only if the incoming data contained a field 'attributes'
Code:
{
    "manufacturerId": 1,
    "modelId": 1,
    "name": "product",
    "typeId": 1,
    "attributes": [
        {
            "nameId": 1,
            "valueId": 2
        }
    ],
    "photoIds": [1,2]
}

It turns out that the "fill" method allows you to break the "Entity" construction, for this reason I had to create a "EntityModify" class
in which I have overridden the "fill" method, below is my hasty solution to the problem.

PHP Code:
<?php


namespace App\Helpers;

use 
CodeIgniter\Entity\Entity;

class 
EntityModify extends Entity
{
    public function fill(?array $data null)
    {
        foreach (array_keys($data) as $key) {
            if (in_array($key, ["attributes","casts","datamap"], true)) {
                unset($data[$key]);
            }
        }

        return parent::fill($data);
    }



Quote:The question is, am I doing something wrong or is this really a bug?
Reply
#2

(This post was last modified: 03-02-2022, 08:12 PM by iRedds.)

This is a bug.
https://github.com/codeigniter4/CodeIgni...ssues/5762
Reply
#3

Thanks for the find, otherwise I'm already tired of looking for what I did wrongWink
Reply
#4

After discussion, it is concluded as a reserved word.

> attributes is a reserved word for internal use. If you use it as a column name, the Entity does not work correctly.
https://codeigniter4.github.io/CodeIgnit...tity-usage
Reply




Theme © iAndrew 2016 - Forum software by © MyBB