Welcome Guest, Not a member yet? Register   Sign In
I want use Entity Method but encountered Error
#1
Sad 
(This post was last modified: 02-05-2022, 05:41 AM by yoshi.)

php8.1、codeigniter4.1.8

hi, there.
I interesting create an application for school.
Use the model from SchoolController.

PHP Code:
class SchoolController extends BaseController
{
    public function index(): string
    
{
        // from login information.
        $school_code_from_login 'NihonBunri';

        // using a model.
        $model = new SchoolModel();
        $school $model->where('school_code'$school_code_from_login)->first();
        
        dd
($school);        << (Debug here!)
    
        $data 
= [
            'school_name' => $school->getName(),
            'zipcode' => $school->getZipcode(),
            'address' => $school->getAddress(),
            'tel' => $school->getSchoolTel(),
            'prefecture' => $school->getSchoolPrefecture(),
            'school_code' => $school->getSchoolCode(),
            'periods' => $school->getPeriod(),
            'school_category_name' => $school->getSchoolCategoryName(),
        ];
        
        
return view('school/index'$data);
    }


OK, I got a entity.
[Image: view]
https://drive.google.com/file/d/1TCzPnIB...qE8U2/view

but I can't use method this entity.
PHP Code:
class SchoolController extends BaseController
{
    public function index(): string
    
{
        // from login information.
        $school_code_from_login 'NihonBunri';

        // using a model.
        $model = new SchoolModel();
        $school $model->where('school_code'$school_code_from_login)->first();
      dd($school);                           << (Debug here!)
     dd($school->getName());        << (Debug here!)
    }

[Image: view?usp=drivesdk]
https://drive.google.com/file/d/1bTYWUUN...p=drivesdk

I look at the following URL in the forum and I want an entity that contains the entity in the relation table
https://forum.codeigniter.com/thread-80298.html

Maybe I think it's fact due to php8.1.
Do you know the solution? How do you make a constructor for a entity?
https://codeigniter4.github.io/userguide...ities.html#
https://stackoverflow.com/questions/5926...e-initiali

for information
PHP Code:
<?php

namespace App\Models;

use 
App\Entities\SchoolEntity;
use 
CodeIgniter\Model;

class 
SchoolModel extends Model
{
    protected $DBGroup          'default';
    protected $table            'school';
    protected $primaryKey      'id';
    protected $useAutoIncrement true;
    protected $insertID        0;
    protected $returnType      SchoolEntity::class;
    protected $useSoftDeletes  false;
    protected $protectFields    true;
    protected $allowedFields    = [];

    // Dates
    protected $useTimestamps true;
    protected $dateFormat    'datetime';
    protected $createdField  'created_at';
    protected $updatedField  'updated_at';

    // Validation
    protected $validationRules      = [];
    protected $validationMessages  = [];
    protected $skipValidation      false;
    protected $cleanValidationRules true;

    // Callbacks
    protected $allowCallbacks true;
    protected $beforeInsert  = [];
    protected $afterInsert    = [];
    protected $beforeUpdate  = [];
    protected $afterUpdate    = [];
    protected $beforeFind    = [];
    protected $afterFind      = [];
    protected $beforeDelete  = [];
    protected $afterDelete    = [];


PHP Code:
<?php

namespace App\Entities;

use 
CodeIgniter\Entity\Entity;

class 
SchoolEntity extends Entity
{
    protected $datamap = [];
    protected $dates  = ['created_at''updated_at'];
    protected $casts  = [
        'id' => 'integer',
        'school_category_id' => 'integer'
    ];
    
    
protected ?int $id;
    protected ?string $name;
    protected ?string $zipcode;
    protected ?string $address;
    protected ?string $tel;
    protected ?string $prefecture;
    protected ?string $school_code;
    protected ?int $school_category_id;
    
    
/**
    * @return int|null
    */
    public function getId(): ?int
    
{
        return $this->id;
    }
    
    
/**
    * @return string|null
    */
    public function getName(): ?string
    
{
        return $this->name;
    }
    
    
/**
    * @return string
    */
    public function getZipcode(): string
    
{
        return $this->zipcode;
    }
    
    
/**
    * @return string
    */
    public function getAddress(): string
    
{
        return $this->address;
    }
    
    
/**
    * @return string
    */
    public function getTel(): string
    
{
        return $this->tel;
    }
    
    
/**
    * @return string
    */
    public function getPrefecture(): string
    
{
        return $this->prefecture;
    }
    
    
/**
    * @return string
    */
    public function getSchoolCode(): string
    
{
        return $this->school_code;
    }
    
    
/**
    * @return int
    */
    public function getSchoolCategoryId(): int
    
{
        return $this->school_category_id;
    }

Reply
#2

Entity uses the $attributes property to store data. This storage is accessed either directly or through getters and setters.
Since you declared the properties corresponding to the fields, no getter/setter is called when accessing them. This means there is no access to the data.

Remove the properties and it will work.
PHP Code:
protected ?int $id;
    protected ?
string $name;
    protected ?
string $zipcode;
    protected ?
string $address;
    protected ?
string $tel;
    protected ?
string $prefecture;
    protected ?
string $school_code;
    protected ?
int $school_category_id

You can also remove methods. They don't make sense unless you need to mutate/calculate the data.
For type casting, you can use this Property Casting
Reply
#3

(This post was last modified: 02-05-2022, 05:53 PM by yoshi.)

Quote:iReddsEntity uses the $attributes property to store data. This storage is accessed either directly or through getters and setters.
Since you declared the properties corresponding to the fields, no getter/setter is called when accessing them. This means there is no access to the data.

Remove the properties and it will work.

    protected ?int $id;
    protected ?string $name;
    protected ?string $zipcode;
    protected ?string $address;
    protected ?string $tel;
    protected ?string $prefecture;
    protected ?string $school_code;
    protected ?int $school_category_id;


You can also remove methods. They don't make sense unless you need to mutate/calculate the data.
For type casting, you can use this Property Casting

thank you for your reply.
there was unnecessary declare.

I will always be saved
Reply
#4

(This post was last modified: 03-16-2022, 11:09 PM by zaakee2022.)

(02-05-2022, 05:52 PM)yoshi Wrote:
Quote:iReddsEntity uses the $attributes property to store data. This storage is accessed either directly or through getters and setters.
Since you declared the properties midasbuy rent a car dubaiblue world cityzameen corresponding to the fields, no getter/setter is called when accessing them. This means there is no access to the data.

Remove the properties and it will work.

    protected ?int $id;
    protected ?string $name;
    protected ?string $zipcode;
    protected ?string $address;
    protected ?string $tel;
    protected ?string $prefecture;
    protected ?string $school_code;
    protected ?int $school_category_id;


You can also remove methods. They don't make sense unless you need to mutate/calculate the data.
For type casting, you can use this Property Casting

thank you for your reply.
there was unnecessary declare.

I will always be saved

This method also worked for me, and I spent hours of hard time to figure it out.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB