Open mbaeuerle opened 7 years ago
I just experienced this bug as well. It is not a php bug, this is the intended behaviour of Laravel.
boot()
method of the Model will not be calledboot()
method of the Model has not been called the bootBelongsToTenants()
method in BelongsToTenants.php
will also not be calledInstantiating the model before calling the newQueryWithoutTenants
does indeed fix this, because this will also cause the boot method to be called. :smile:
Yes it's up to Laravel to boot the trait after an instance of a model was created for the first time but the problem can also arise without Laravel:
<?php
class A {
public static $instance;
public function __construct() {
static::$instance = new B();
}
public static function do() {
static::$instance->print(new static());
}
}
class B {
public function print($instance) {
print("hello");
}
}
A::do(); // Fails with "Fatal error: Uncaught Error: Call to a member function print() on null"
?>
which is working when using the same fix:
$i = new static();
static::$instance->print($i);
Yep true. Do you think it is beneficial to write an if statement around it. Like first testing if static::$landlord
is defined, and otherwise instatializing it?
Also one test is wining about a static call to faker. Should this be fixed or is this fine like it is now?
static::$landlord
instance is still null at the moment when it should callnewQueryWithoutTenants
. I don’t know if this is a PHP bug but it works when moving the creation of the model to the previous lineallTenants
.static::$landlord
stays always initialized.