Eloquent 在第二次调用中复制绑定
Eloquent duplicating bindings in the second call
我在 Laravel 项目中使用 Reposity 模式。问题是我看到它在复制第二个调用查询中的绑定!
这是我的代码:
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");
}
}
调试,$departure_airport
获取记录,$destiny_airport
失败。你会认为id: 10
有问题。没有。如果我先交换并放置 $destiny_airport
,它会得到一条记录,但随后 $departure_airport
会失败。然后,我考虑按照建议 here 打印原始 SQL 查询。
这是结果:
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"]
为什么在第三个查询中(在星号之后)复制带有参数 6 和 10 的列 "id" 而我在第二个查询中仅将 10 作为参数传递?! 而不是第三个查询,我想这样:
INFO: "select * from `airports` where `airports`.`id` = ? limit 1"
INFO: ["10"]
这是实现:
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);
}
...
我Repository.php
...
public function find($id, $columns = array('*'));
...
给出的错误是:
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)
尝试在存储库的查找方法中调用 newModelInstance
。
return $this->model->newModelInstance()->find($id, $columns);
我在 Laravel 项目中使用 Reposity 模式。问题是我看到它在复制第二个调用查询中的绑定!
这是我的代码:
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");
}
}
调试,$departure_airport
获取记录,$destiny_airport
失败。你会认为id: 10
有问题。没有。如果我先交换并放置 $destiny_airport
,它会得到一条记录,但随后 $departure_airport
会失败。然后,我考虑按照建议 here 打印原始 SQL 查询。
这是结果:
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"]
为什么在第三个查询中(在星号之后)复制带有参数 6 和 10 的列 "id" 而我在第二个查询中仅将 10 作为参数传递?! 而不是第三个查询,我想这样:
INFO: "select * from `airports` where `airports`.`id` = ? limit 1"
INFO: ["10"]
这是实现:
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);
}
...
我Repository.php
...
public function find($id, $columns = array('*'));
...
给出的错误是:
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)
尝试在存储库的查找方法中调用 newModelInstance
。
return $this->model->newModelInstance()->find($id, $columns);