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)