Laravel 5, почему сам добавляет is not null к запросу?

Помогите разобраться

// Model
	public function child(){
        return $this->hasMany('App\Models\Geographies\Geography',"parent_id","id");
    }
	
// Controller 
	$curGeo     =   Geography::find($_GET["geoID"]);
    $childs     =   $curGeo->child()->get();

DB Log:
select * from `geo_geographies` where `geo_geographies`.`parent_id` = ? and `geo_geographies`.`parent_id` is not null


Почему он в запрос цепляет and `parent_id` is not null ?
  • Вопрос задан
  • 445 просмотров
Решения вопроса 1
Sanasol
@Sanasol Куратор тега Laravel
нельзя просто так взять и загуглить ошибку
https://github.com/laravel/framework/commit/1e8016...
commit 1e80162e9a3a4c42a0079bd9f34e785197a66c07 refs/tags/v5.0.28
Author: Carsten Bleicker
Date: Tue Mar 17 12:59:10 2015 +0100

[BUGFIX] hasMany or hasOne neets to check for foreign_key != null

In case your model using HasOne, HasMany the query building needs additional
check for foreign_key != null

If don't you will receive any related item having foreign_key = null on a new object.
Example of wrong behaviour:

$foo = new Foo();
$foo->save();

$bar = new Bar();
$this_should_not_holding_any_relation = $bar->getFoos()->getResutls()->toArray()

In fact currenctly $bar->getFoos() finds any Foo() having foreign_key = null.
SQL is "where bar.id = foo.foreign_key"
wich is in fact "null = null" (any unrelated foo item)


https://github.com/laravel/framework/blob/master/s...
Просто баг закрыли.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы