-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is deleting HAS_MANY related records necessary? #46
Comments
I am not sure I understand the problem. What code are you executing and what is the error message you get? |
Oh, sorry. For example. I have 2 models: class Question extends CActiveRecord{
....
public function relations(){
reutn array(
'variants' => array(self::HAS_MANY, 'Variant', 'question_id'),
'rightVariant' => array(self::BELONGS_TO, 'Variant', 'right_variant_id')
);
}
....
} class Variant extends CActiveRecord{
....
public function relations(){
reutn array(
'question' => array(self::BELONGS_TO, 'Question', 'question_id')
);
}
....
} So if I'm trying to update question with new variants: $question = Question::model()->findByPk($_POST['quesiton_id']);
//I already have $question->rightVariant with id=1, and also I have this entry in $_POST['Variant'] array
$variants = array();
foreach($_POST['Variant'] as $rawVariant){
$variant = Variant::model()->findByPk($rawVariant['id']);
$variant->attributes = $rawVariant;
$variants[] = $variant;
}
$question->variants = $variants;
$question->save(); //here i have foreign key constraint violation, because of deleting variant with id=1 by EActiveRecordBehavior |
Can you please post the exact error message? ideally with stacktrace? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, thanks for sucha good library. But I have some doubts about related records deleting in case of HAS_MANY relation. For example if I have model Question which has many questions and only one right question i would implement it in DB like such:
So, when i'm inserting it's okay all stuff save correctly. But when I'm trying to update relation, I'm getting an error, because of deleting records from variant which is dependency of question. So what do you think about this situation? My suggestion is to add a check for a changes in related records list and remove only deleted records.
The text was updated successfully, but these errors were encountered: