One-To-One Unidirectionnal


(Rastacool24) #1

Hi eveyrone,

I’m trying to make one to one unidirectionnal. The problem is, it does not work. Here is my entities:




class A extends CActiveRecord {


    public function relations() {

        return array(

            'logbook' => array(self::BELONGS_TO, 'Logbook', 'logbook_id'),

        );

    }


    public static function model($className = __CLASS__) {

        return parent::model($className);

    }

}


?>


class Logbook extends CActiveRecord {


    public static function model($className = __CLASS__) {

        return parent::model($className);

    }


}



I’m doing this:




$a = new A();

$b = new Logbook();


$a->logbook = $b;

$a->save();



But the foreign key ‘logbook_id’ is not set! And logbook has not been persisted.

Need some help please.


(Rastacool24) #2

please some help! By the way, is that possible to save in cascade?


(Softark) #3

[color="#008000"]/* Moved from "Bug Discussion" to "General Discussion" */[/color]

Hi Babas007,

AR of Yii doesn’t support the automatic saving of the related objects.




$a = new A();

$b = new Logbook();

$b->save();

$a->logbook_id = $b->id;

$a->save();



But there are some extensions to accomplish the automatic saving of the related objects.

For instance:

http://www.yiiframework.com/extension/activerecord-relation-behavior


(Rastacool24) #4

That’s too bad, is there any other ORM compatible with Yii?

By the way, how to make a join table with active record to make it unidirectionnal? Would be nice to write something like this:




class Post extends CActiveRecord

{

    ......

 

    public function relations()

    {

        return array(

            'author'=>array(self::BELONGS_TO, 'User', 'author_id'),

            'categories'=>array(self::HAS_MANY, 'Category',

                'tbl_post_category(post_id, category_id)'),

        );

    }

}




(Rastacool24) #5

up!


(Softark) #6

Someone may have some idea, but I don’t know.

I’m sorry, but I don’t understand what you mean by that.

‘categories’ should be a MANY_MANY relation in Yii’s terminology, and it is supported.




    'categories'=>array(self::MANY_MANY, 'Category', 'tbl_post_category(post_id, category_id)'),



But Yii doesn’t have a method to save/update MANY_MANY relation out of the box. You have to create/delete records in tbl_post_category manually, most probably using PostCategory AR model.

I’ve heard that Yii 2.0 will be different, but Yii 1.0 and 1.1 don’t support the saving of the related models. You have to do it by yourself, or have to use some extension to automate it.

It’s so-called by-design. I’m not very sure but it looked to me that the developer(s) avoided a complex (and possibly heavy and fragile) mechanism of saving relations. And personally I believe it was the right decision. Or, should I say, I’m already accustomed to it by now.


(Rastacool24) #7

Here is THE thing, i want a unidirectional one to many relation. Like this example:

$bag->getBooks()

But i have the feeling with Yii, the closest solution would be to do a relation HAS_MANY/BELONGS_TO, it gives this:

$bag->getBooks()

$book->getBag(), while I don’t want this! That’s why i said unidirectionnal.


(Softark) #8

You mean that you want $bag->books, but you don’t want $book->bag?

Then just don’t use/define the latter. You may remove/modify any relational declaration created by Gii. And you may define it on your own without the help of Gii. :)