Welcome Guest, Not a member yet? Register   Sign In
I want the find() method to explicitly return entity class
#1
Tongue 

hello. Is it possible to get rid of the following warnings?
  • point A, The return value is actually an entity named 'SchoolEntity'.
  • point B, However, find () returns'array | object | null'. Therefore, if I specify School Domain class as an argument, I will get a warning.
PHP Code:
// return SchoolEntity
class SchoolModel extends Model
{
    protected $DBGroup 'default';
    protected $table 'school';
    protected $primaryKey 'id';
    protected $useAutoIncrement true;
    protected $insertID 0;
    protected $returnType SchoolEntity::class;
}

class 
SchoolController extends BaseController
{
    const SCHOOL_LIST = ['Demo1' => 1'Demo2' => 2];
    public function index(): string
    
{
        $school_code_from_login 'Demo1';
        
        $model 
= new SchoolModel();
        $school $model->find(self::SCHOOL_LIST[$school_code_from_login]);    <- point A

        $schoolDomain 
= new SchoolDomain($school);    <- point B
    
}
}

class 
SchoolDomain
{
    private SchoolEntity $schoolEntity;
    
    
/**
    * @param SchoolEntity $schoolEntity
    */
    public function __construct(
        SchoolEntity $schoolEntity
    
) {
        $this->schoolEntity $schoolEntity;
    }


The figure below shows a warning. The screen display itself is successful.
https://drive.google.com/file/d/1yhsQWjz...p=drivesdk
Reply
#2

You should not forget that the find() method may return null if the id is not found.

I don't know what kind of IDE you have, but try specifying the @method tag in the model's docBlock

PHP Code:
/*
* @method SchoolEntity find($id = null)
*/
class SchoolModel extends Model 
Reply
#3

(This post was last modified: 03-04-2022, 07:03 PM by yoshi.)

(03-04-2022, 10:11 AM)iRedds Wrote: You should not forget that the find() method may return null if the id is not found.

I don't know what kind of IDE you have, but try specifying the @method tag in the model's docBlock

PHP Code:
/*
* @method SchoolEntity find($id = null)
*/
class SchoolModel extends Model 

Hello, iRedds. Thank you as always.
I'm using PHPStorm. When I put it on, it was definitely solved. I feel refreshed!

It seems better to attach it to all models. Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB