私が持っているモデルのコンストラクターに問題があります。Eloquent ORM で Laravel-3 を使用しています。
私が期待する動作は次のとおりです。
Class A{
$dat_var = 1;
public function __construct{
$this->dat_var = 2;
}
}
Class B extends A{
public function __construct{
echo 'before : '.$var;
parent::__construct();
echo 'after: '.$var;
}
}
Class Test{
public function myTestMethod2(){
$b = new B();
}
}
myTestMethod1() の結果:
before : 1
after : 2
しかし、Eloquent を拡張する私の Laravel オブジェクトでは、オブジェクトの構築が完了するまで変数は設定されません。これは、Eloquent モデルで必要な変数が DB からロードされるという事実と関係があると思います。とにかく、これに対するエレガントな回避策はありますか、それとも何か間違っていますか?
ここに私のDBテーブルがあります:
--------------
-table_c -
--------------
-id | my_var-
--------------
-1 | 5 -
--------------
これが私のlaravelコードです:
Class C extends Eloquent{
public static $table = 'table_c';
public function __construct{
//Usefull things here
parent::__construct();
echo 'In construct : '.$this->my_var;
}
}
Class Test{
public function myTestMethod2(){
$c = C::find('1');
echo 'Construct finish : '.$c->my_var
}
}
myTestMethod2() の結果:
In construct :
Construct finish : 5
私が myTestMethod2() に期待したこと:
In construct : 5
Construct finish : 5