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

__set in \App\Entities\Sample doesnt works #499

Closed
nowackipawel opened this issue May 17, 2017 · 2 comments
Closed

__set in \App\Entities\Sample doesnt works #499

nowackipawel opened this issue May 17, 2017 · 2 comments

Comments

@nowackipawel
Copy link
Contributor

nowackipawel commented May 17, 2017

I'm trying to use Entities.
In my model SampleModel.php I've got:

    protected $table = 't_sample';
    protected $primaryKey = 'smp_id';
    protected $returnType = '\App\Entities\Sample';


    protected $allowedFields = [
        'all_fields_from_t_sample... without id ;)'
    ];

and in Entity Sample.php

<?php

namespace App\Entities;

class Sample
{
    protected $all_fields_1;
    protected $all_fields_2;
    protected $all_fields_3; ...


    public function __get($key)
    {...}

    public function __set($key, $value)
    {
                ddd($this->$key);
        if (isset($this->$key))
        {
            if($key == 'all_fields_1') {
                $this->$key = sthdifferent;
            }
            else
                $this->$key = $value;
        }
    }
}

but that __set is never used by BASEPATH.'Model.php'.
I can also define __set in my SampleModel instead od Entity which called Sample... but why?

And why when I change method __set in SampleModel like that:

    public function __set($key, $value)
    {
        d($key, $value);
}

it prints only salt and their value ?
Is it a bug?

@nowackipawel
Copy link
Contributor Author

nowackipawel commented May 17, 2017

Ok that issue is in documentation :).
https://bcit-ci.github.io/CodeIgniter4/database/model.html

instead of: class Job shoud beclass Job extends \CodeIgniter\Entity

instead of

public function __get($key)
        {
                if (isset($this->$key))
                {
                        return $this->$key;
                }
        }

        public function __set($key, $value)
        {
                if (isset($this->$key))
                {
                        $this->$key = $value;
                }
        }

should be:

public function __get(string $key)
        {
                if (isset($this->$key))
                {
                        return $this->$key;
                }
        }

        public function __set(string $key, $value = null)
        {
                if (isset($this->$key))
                {
                        $this->$key = $value;
                }
        }

@lonnieezell
Copy link
Member

Good catch. The Entity class didn't exist when those docs were written up. I'll need to adjust those now.

However, there are docs for the Entity class, but it looks like they're not showing up in the index. You can find them, here, though.

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