These was generated from gii, I suppose that all the rules thus
generated are right
Yes the rules are correct, and they are good in most use cases.
that does not mean that they will work in every situation;
i assume from the comments that your structure is something like this:
(and if i'm wrong please update your question with the appropiate details):
i'm gonna call them Order and OrderDetail for the sake of simplicity
generated models: these contain the existance
rule you mentioned
common\models\Order
common\models\OrderDetail
models with custom db: these contain a different definition of getDb()
and extend the two generated modules above
common\modules\samplemodule\models\Order
common\modules\samplemodule\models\OrderDetail
now the models in samplemodule
will inherit the rules of the generated models.
note the targetClass
of this generated rule in common\models\OrderDetail
:
[['id_order'], 'exist', 'skipOnError' => true, 'targetClass' => Order::className(), 'targetAttribute' => ['id_order' => 'id']]
Order::className()
means common\models\Order::className()
that means that all child classes (regardless of namespace) will have a existance rule that refferences common\models\Order
.
and in your case: modules\samplemodule\models\OrderDetail
(which uses a different db) will validate against the existence of a common\models\Order
(an order from the default database)
so here's my proposed solution:
for common\models\OrderDetail
(the generated class) remove the existence
rules, and define them in a separate method
namespace common\models;
class OrderDetail extends ActiveRecord {
//..
public function rules(){
return ArrayHelper::merge([
// ..
// all the default generated rules except the existance ones
], static::existenceRules());
}
protected static function existenceRules(){
return [
[['id_order'], 'exist', 'skipOnError' => true,
// fqn not required it's just here to highlight the difference
'targetClass' => common\models\Order::className(),
'targetAttribute' => ['id_order' => 'id']]
];
}
// ..
}
for common\modules\samplemodule\models\OrderDetail
overwrite the existanceRules()
method we created previously an link the correct target class
namespace common\modules\samplemodule\models;
class OrderDetail extends common\models\OrderDetail {
//..
// custom db:
public static function getDb(){
return Yii::$app->moduleDatabase;
}
// optional (if you need more rules here):
public function rules(){
return ArrayHelper::merge( parent::rules(), [
// rules that apply only in this context (this db)
]);
}
// this is required if to reference the correct `targetClass`
protected static function existenceRules(){
return [
[['id_order'], 'exist', 'skipOnError' => true,
'targetClass' => common\modules\samplemodule\models\Order::className(),
'targetAttribute' => ['id_order' => 'id']]
];
}
// ..
}
in both cases, i've used the full name of the target class to help highlight the difference, since they use different databases the same rule cannot work in both parent and child class.
hope this is helpfull to you. best of luck.