RepositoriesPattern icon indicating copy to clipboard operation
RepositoriesPattern copied to clipboard

Problem when use two criterias.

Open andersonrocha0 opened this issue 8 years ago • 1 comments

In the second criteria, the entity became a Illuminate\Database\Eloquent\Builder

See the image in this link: http://imgur.com/Bg9KCOY

My criteria was implemented bases on the FindUsingLikeCriteria.

See implementations bellow.

Criteria 1

<?php

namespace App\Repositories\Criteria;


use Andersonef\Repositories\Abstracts\CriteriaAbstract;
use Andersonef\Repositories\Contracts\RepositoryContract;
use Illuminate\Database\Eloquent\Model;

class StudentHistoryByStudentIdAndDateAndHour extends CriteriaAbstract
{

    private $studentId;
    private $date;
    private $hour;

    /**
     * StudentHistoryByClassIdAndStudentIdAndDateMoreThan constructor.
     * @param $studentId
     * @param $date
     * * @param $hour
     */
    public function __construct($studentId, $date, $hour)
    {
        $this->studentId = $studentId;
        $this->date = $date;
        $this->hour = $hour;
    }


    /** Implements select rules (where clausules) of the criteria, that will be used inside a service or repository to get custom database data.
     * @param Model $model
     * @param RepositoryContract $rep
     * @return mixed
     */
    public function apply(Model $model, RepositoryContract $rep)
    {

        $query = $model
            ->where('student_id', '=', $this->studentId)
            ->where('date', '=', $this->date)
            ->where('hour', '=', $this->hour);

        return $query;
    }
}

Criteria 2

<?php

namespace App\Repositories\Criteria;


use Andersonef\Repositories\Abstracts\CriteriaAbstract;
use Andersonef\Repositories\Contracts\RepositoryContract;
use Illuminate\Database\Eloquent\Model;

class StudentHistoryByClassIdAndStudentIdAndDateMoreThan extends CriteriaAbstract
{

    private $classId;
    private $studentId;
    private $date;

    /**
     * StudentHistoryByClassIdAndStudentIdAndDateMoreThan constructor.
     * @param $classId
     * @param $studentId
     * @param $date
     */
    public function __construct($classId, $studentId, $date)
    {
        $this->classId = $classId;
        $this->studentId = $studentId;
        $this->date = $date;
    }


    /** Implements select rules (where clausules) of the criteria, that will be used inside a service or repository to get custom database data.
     * @param Model $model
     * @param RepositoryContract $rep
     * @return mixed
     */
    public function apply(Model $model, RepositoryContract $rep)
    {

        $query = $model
            ->where('class_id', '=', $this->classId)
            ->where('student_id', '=', $this->studentId)
            ->where('date', '>=', $this->date);

        return $query;
    }
}

Service Layer

    private function removeSameClassFromHistory($classId, $studentId)
    {
        $now = new DateTime();
        $datePattern = config('constants.DATE_PATTERN');

        $studentHistoryByClassIdAndStudentIdAndDateMoreThan =
            new StudentHistoryByClassIdAndStudentIdAndDateMoreThan(
                $classId,
                $studentId,
                $now->format($datePattern)
            );

        $listOfStudentHistory = $this
            ->getRepository()
            ->findByCriteria($studentHistoryByClassIdAndStudentIdAndDateMoreThan)
            ->get();

        if ($listOfStudentHistory->count() > 0) {
            $this->customDestroy($listOfStudentHistory);
        }
    }

    public function removeClassOnSameDateAndAtSameHour($studentId, $date, $hour) {

        $studentHistoryByStudentIdAndDateAndHour = new StudentHistoryByStudentIdAndDateAndHour(
            $studentId,
            $date,
            $hour
        );

        $listOfStudentHistory = $this
            ->getRepository()
            ->findByCriteria($studentHistoryByStudentIdAndDateAndHour)
            ->get();

        if ($listOfStudentHistory->count() > 0) {
            $this->customDestroy($listOfStudentHistory);
        }
    }



    public function store(StudentHistory $studentHistory)
    {

        DB::transaction(function () use ($studentHistory) {

            // TODO verify if is not a complementary class.
            $classType = $this->classTypeService->getRepository()->find($studentHistory->class_id);

            if ($classType->complementary_class != 1) {
                $this->removeSameClassFromHistory($studentHistory->class_id, $studentHistory->student_id);
            }

            $this->removeClassOnSameDateAndAtSameHour($studentHistory->student_id, $studentHistory->date,
                $studentHistory->date);

            $numberOfStudents = $this
                ->getRepository()
                ->numberOfStudents(
                    $studentHistory->teacher_id,
                    $studentHistory->unit_id,
                    $studentHistory->class_id,
                    $studentHistory->date,
                    $studentHistory->hour
                );

            $classType = $this->classTypeService->getRepository()->find($studentHistory->class_id);
            $maxNumberOfStudents = $classType->number_of_students;

            if ($numberOfStudents < $maxNumberOfStudents) {
                $studentHistory->save();
            } else {
                throw new MaxOfStudentsException("Max of students to this class");
            }

        });

    }

andersonrocha0 avatar Feb 11 '17 15:02 andersonrocha0

Argument 1 passed to App\Repositories\Criteria\StudentHistoryByStudentIdAndDateAndHour::apply() must be an instance of Illuminate\Database\Eloquent\Model, instance of Illuminate\Database\Eloquent\Builder given, called in /home/anderson/Projetos/controller2-core/vendor/andersonef/repositories-pattern/src/Abstracts/RepositoryAbstract.php on line 83 and defined

andersonrocha0 avatar Feb 11 '17 15:02 andersonrocha0