0

I'm using the Reposity Pattern in a Laravel project. The problem is that I see that it's duplicating the bindings in the second call query!

This is my code:

class AirController extends Controller
{
    private $airportService;

    public function __construct(AirportService $airportService) {
        $this->airportService = $airportService;
    }

    public function getAirports(){
        $departure_airport = $this->airportService->getCityFromAirport("6");
        $destiny_airport = $this->airportService->getCityFromAirport("10");
    }
}

Debugging, $departure_airport gets a record, but $destiny_airport fails. You will think that id: 10 has a problem. Nope. If I swap and put $destiny_airport first, it gets a record, but then $departure_airport fails. Then, I thought about printing the raw SQL queries as suggested here.

This is the result:

INFO: "select * from `airports` where `airports`.`id` = ? limit 1"  
INFO: ["6"]  
INFO: "select * from `cities` where `cities`.`id` = ? limit 1"  
INFO: [441]  
*****************************************************************************************
INFO: "select * from `airports` where `airports`.`id` = ? and `airports`.`id` = ? limit 1"  
INFO: ["6","10"]

Why in the third query (after asterisks) is duplicating the column "id" with parameters 6 and 10 when I pass as parameter only the 10 in the second query?! Instead of, third query I would like it like this:

INFO: "select * from `airports` where `airports`.`id` = ? limit 1"  
INFO: ["10"]

This is the implementation:

AirportService.php:

use App\Repositories\AirportRepository as Airport;

class AirportService {

    private $airport;

    public function __construct(Airport $airport){
        $this->airport = $airport;
    }

    public function getCityFromAirport($airportId){
        $airport = $this->airport->find($airportId);
        return $airport->City;
    }
}

Repository.php

...

public function find($id, $columns = array('*')) {
    return $this->model->find($id, $columns);
}

...

IRepository.php

...

public function find($id, $columns = array('*'));

...

The error given is:

local.ERROR: Trying to get property of non-object {"userId":41,"email":"...@...","exception":"[object] (ErrorException(code: 0): Trying to get property of non-object at C:\\wamp\\www\\project\\API\\app\\Services\\AirportService.php:21)
Sergio
  • 317
  • 1
  • 7
  • 18

1 Answers1

1

Try calling newModelInstance in the find method of the repository.

return $this->model->newModelInstance()->find($id, $columns);
Jason Grim
  • 413
  • 4
  • 7
  • It worked!! Any explanation about why that error is produced? I can't believe that something so simple like that produces a weird error – Sergio Aug 03 '18 at 06:52
  • @Sergio I didn't look too deeply into the cause. It just seemed like you were loading one object model then trying to use that same model to attempt to load new object data. Instead of loading the ORM it might make more since to use the query builder to pull the single column so not to load a model to throw away. – Jason Grim Aug 03 '18 at 08:17