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

fixing entity after remap #4950

Closed
yasinpalizban opened this issue Jul 19, 2021 · 5 comments
Closed

fixing entity after remap #4950

yasinpalizban opened this issue Jul 19, 2021 · 5 comments

Comments

@yasinpalizban
Copy link

yasinpalizban commented Jul 19, 2021

after $datamap filed

'created_at' => null,
'updated_at' => null,
'deleted_at' => null,

to

'createdAt' => 'created_at',
'updatedAt' => 'updated_at' ,
'deletedAt' => 'deleted_at'

each time create new data to table filed set value for both them
but only should do for created_at not for all
'createdAt' => 'created_at',
'updatedAt' => 'updated_at' ,

here table after insert
id= 38
key =xxoyox
value= yyyy
description=jhdfjshfjsdf
status=0
craeted_at=2021-07-19 13:25:18
updated_at=2021-07-19 13:25:18
deleted_at=NULL
heres code

<?php namespace Modules\Common\Entities;

use \CodeIgniter\Entity;
use CodeIgniter\I18n\Time;

class  SettingEntity extends Entity
{
    protected $id;
    protected $key;
    protected $value;
    protected $description;
    protected $status;
    protected $createdAt;
    protected $updatedAt;
    protected $deletedAt;

    protected $attributes = [
        'id' => null,
        'key' => null,
        'value' => null,
        'description' => null,
        'status' => null,
        'created_at' => null,
        'updated_at' => null,
        'deleted_at' => null,
    ];

    protected $datamap = [
        'createdAt' => 'created_at',
        'updatedAt' => 'updated_at' ,
        'deletedAt' => 'deleted_at'
    ];

    protected $dates = ['created_at', 'updated_at', 'deleted_at'];

    protected $casts = [
        'status' => 'boolean',
    ];

    protected $permissions = [];

    protected $roles = [];

    public function activate()
    {
        $this->attributes['status'] = 1;
        return $this;

    }

    public function deactivate()
    {
        $this->attributes['status'] = 0;

        return $this;
    }

    public function isActivated(): bool
    {
        return isset($this->attributes['status']) && $this->attributes['status'] == true;
    }

    public function createdAt()
    {
       // date_default_timezone_set('Asia/Tehran');
        $this->attributes['created_at'] = date('Y-m-d H:i:s', time());
        // $this->attributes['created_at'] = new  Time(date('Y-m-d H:i:s', time()), 'UTC');
        return $this;
    }

    public function updatedAt()
    {

      //  date_default_timezone_set('Asia/Tehran');
        $this->attributes['updated_at'] = date('Y-m-d H:i:s', time());

        return $this;
    }
}
@paulbalandan
Copy link
Member

I don't understand what's the issue here nor the code is not readable. Please format your code using markdown.

@yasinpalizban
Copy link
Author

yasinpalizban commented Jul 21, 2021

see it again
create model entity and controller like me use ci4.1.3 tooo
change $datamap[] like me and try add and update in tbl
checks data base table you ll see the issue
okay man :)

@paulbalandan
Copy link
Member

If I understood correctly, your remapping of date fields added in the $datamap property is not working?

Try flipping the property mapping:

protected $datamap = [
    'created_at' => 'createdAt',
    'updated_at' => 'updatedAt',
    'deleted_at' => 'deletedAt',
];

@yasinpalizban
Copy link
Author

yasinpalizban commented Jul 22, 2021

no you are wrong
i m not beginner thisn is not the first time i use Entitiy for Model
look at ci documentation
it has to be like that
[
'userId =>'user_id',
'createdAt'=>'created_at'
]

@paulbalandan
Copy link
Member

paulbalandan commented Jul 22, 2021

Well, I'm not suggesting you're a beginner whatsoever. I'm just trying to let you try some options to debug your issue.

And mind you, the docs recently got changed with this PR #4937

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants