[EXTENSION] CSaveRelationsBehavior


I have the same problem as Bitmatrix above (post dated Posted 20 November 2010 - 07:52 AM).

Do you have any ideas how I can get around the problem?


Hey @juban. How are you? I’m using your extension and it’s working very well. I want to know how to ajax validate my model with relations? Have you had this issue? Can you help me?

Thanks in advance.


Just tried out the demo 1.1 and it works fantastic! Amazing extension!

Hi, and thanks for posting this excellent extension, it’s been really useful. I’ve found a bug in the code when using it with relations that are declared with ‘through’. I’ve submitted a bug report on Google code, but I wasn’t sure if you get those, so I’m taking the libery of posting here as well. My apologies if I’m duplicating issues!

Copy of the bug report:

What steps will reproduce the problem?

Set-up: three tables;

Book - A list of books

Author - A list of authors of those books. A book can have many authors, and authors can write many books, so a joining table is used.

BookAuthors - composite primary key consisting of author_id and book_id

1.Declare relations as follows:

In model ‘Book’

public function relations() {

    // NOTE: you may need to adjust the relation name and the related

    // class name for the relations automatically generated below.

    return array(

        'bookAuthors' => array(self::HAS_MANY, 'BookAuthors', 'book_id'),

        'authors' => array(self::HAS_MANY, 'Author', 'author_id', 'through' => 'bookAuthors'),



2.Try to delete a book

What is the expected output? What do you see instead?

I would expect the related records in BookAuthors to be deleted only. I don’t want authors to be deleted, as they may have other books they have written.

Instead, the behavior goes through each relation, and throws an error on finding the authors relation. It reports that it cannot find the column ‘author_id’ in table ‘author’

What version of the product are you using? On what operating system?

v1.0.3 on Linux Ubuntu

Please provide any additional information below.

I’ve been able to make a temporary fix by altering the following lines in CSaveRElationsBehavior.php:

Lines 385 - 391 are now

if (!$activeRelation->through) {

                            // HasMany & HasOne relation : delete related records

                            $relatedRecord = new $relationClassName;

                            $criteria = new CDbCriteria;

                            $criteria->addColumnCondition(array($relationForeignKey => $model->primaryKey));



Is it possible to save multi level related models on one form? For example I want to save Model A (for example Library) together with Many Models B (for example Books) and Model B with Many Models C (for example Book’s authors) on one form using this behavior ?

Or - is this possible to save Has Many relation (with through) - because when i try to save relation with through i got error: preg_split() expects parameter 2 to be string, array given.

Thanks for answer