Skip to content
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

Open
vollossy opened this issue Dec 17, 2013 · 3 comments
Open

Is deleting HAS_MANY related records necessary? #46

vollossy opened this issue Dec 17, 2013 · 3 comments

Comments

@vollossy
Copy link

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:

Question table: id(pk), text, right_variant_id
Variant table: id(pk),text,question_id

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.

@cebe
Copy link
Member

cebe commented Dec 17, 2013

I am not sure I understand the problem. What code are you executing and what is the error message you get?

@vollossy
Copy link
Author

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

@cebe
Copy link
Member

cebe commented Dec 18, 2013

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants