Welcome Guest, Not a member yet? Register   Sign In
how to create generic array for entities
#1
Smile 
(This post was last modified: 02-19-2022, 05:27 PM by yoshi.)

Hi, there.
I trying create generic array for entities.

Look at the following sourse.
I got school entity at 'point A'.
https://drive.google.com/file/d/1dOQO7_O...p=drivesdk

Next, I try create a school domain.

and then I want to get a school semester.
good. I got 3 semester.
https://drive.google.com/file/d/1SsPrbwe...p=drivesdk

Extract the entity from the 'period' table using the school's id and 'where'.
Here the entity becomes an array at 'Point B'.
Is there a way to return these as an entity array?(Remaining with entity type constraints)
I know that using 'find()' returns an entity, but it doesn't seem to be possible with 'where()'.
https://drive.google.com/file/d/1fi7EMHt...p=drivesdk

Next, I would like to find out semester for today using 'for each' at 'Point C'.

PHP Code:
class SchoolController extends BaseController
{
    const SCHOOL_PK_LIST = ['School1' => 1'School2' => 2];
    
    
public function index(): string
    
{
        $school_code_from_login 'School1';
        
        $model 
= new SchoolModel();
        $school $model->find(self::SCHOOL_PK_LIST[$school_code_from_login]);
        dd($school);                  // point A

        $schoolDomain = new SchoolDomain($school);
    }
}

class 
SchoolDomain
{
    public function __construct(
        SchoolEntity $schoolEntity
    
) {
        $this->schoolEntity $schoolEntity;

        // Perhaps there are three semesters in American schools.
        $this->periods $this->getPeriods($schoolEntity->id);

        dd($this->getCurrentPeriod());      // point C
    }
    
    
/**
    * @param int $school_id
    * @return PeriodEntity[]|null
    */
    private function getPeriods(int $school_id): ?PeriodEntity
    
{
        $model service('periodModel');
        dd($model->where('school_id'$school_id)->findAll());              // point B
        return $model->where('school_id'$school_id)->findAll();
    }

    /**
    * @return PeriodEntity|null
    */
    public function getCurrentPeriod(): ?PeriodEntity
    
{
        // TODO: return a period entity
        $today = new DateTime();
        foreach ($this->periods as $period) {
            $from $period->from_date;
            $to $period->to_date;
            
            
if ($from <= $today and $today <= $to) {
                return $period;
            }
        }
        
        
return null;
    }

Reply




Theme © iAndrew 2016 - Forum software by © MyBB