Welcome Guest, Not a member yet? Register   Sign In
Entity add custom properties
#3

(This post was last modified: 09-05-2022, 10:03 AM by ozornick.)

PHP Code:
 
    
protected $returnType = \App\Models\Entity\Image::class;
    protected 
$afterFind = ['_addExtendProps'];

 
  private function _addExtendProps(Image $image): Image
    
{
        $groups    = \Config\Services::groups();
        $users     = \Config\Services::users();
        // .. code select other Entity
        $image->group  $group;
        $image->user   $user;
        $image->syncOriginal();

        return $image;
    
If you set the $user or $group properties via the magic $this->var - the attribute is available via $image->toArray()

How to initialize an empty attribute $this ->attributes['user'] = null ? To avoid warning.
Use only in the constructor?

And today there was a question with this: $image ->toRawArray()  return virtual $image->user
This breaks $this->builder()->insertBatch($images->toRawArray()); because $image->user doesn't exist in the table (Object of class App\Models\Entity\User could not be converted to string )
Simple CI 4 project for beginners codeigniter-expenses
Reply


Messages In This Thread
Entity add custom properties - by ozornick - 09-04-2022, 12:35 AM
RE: Entity add custom properties - by iRedds - 09-05-2022, 12:03 AM
RE: Entity add custom properties - by ozornick - 09-05-2022, 10:02 AM
RE: Entity add custom properties - by iRedds - 09-05-2022, 05:37 PM
RE: Entity add custom properties - by ozornick - 09-06-2022, 12:19 AM



Theme © iAndrew 2016 - Forum software by © MyBB