Yii Framework Forum: One-To-One Unidirectionnal - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

One-To-One Unidirectionnal Rate Topic: -----

#1 User is offline   Babas007 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 03-January 13

Posted 03 January 2013 - 03:32 PM

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.
0

#2 User is offline   Babas007 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 03-January 13

Posted 04 January 2013 - 05:59 PM

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

#3 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,082
  • Joined: 16-February 11
  • Location:Japan

Posted 05 January 2013 - 03:15 AM

/* Moved from "Bug Discussion" to "General Discussion" */

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.yiiframew...lation-behavior
0

#4 User is offline   Babas007 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 03-January 13

Posted 05 January 2013 - 05:58 AM

Quote

"You can not save a record that has new related records!" ΒΆ

You have assigned a record to a relation which has not been saved (it is not in the database yet). Since ActiveRecord Relation Behavior needs its primary key to save it to a relation table, this will not work. You have to call ->save() on all new records before saving the related record.


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)'),
        );
    }
}

0

#5 User is offline   Babas007 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 03-January 13

Posted 05 January 2013 - 10:10 AM

up!
0

#6 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,082
  • Joined: 16-February 11
  • Location:Japan

Posted 05 January 2013 - 10:57 AM

View PostBabas007, on 05 January 2013 - 05:58 AM, said:

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

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

Quote

By the way, how to make a join table with active record to make it unidirectionnal?

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

Quote

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)'),
        );
    }
}


'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.
0

#7 User is offline   Babas007 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 03-January 13

Posted 05 January 2013 - 11:34 AM

Quote

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


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.
0

#8 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,082
  • Joined: 16-February 11
  • Location:Japan

Posted 05 January 2013 - 05:52 PM

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. :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users