Yii Framework Forum: differnece between hasmany() and hasone() - Yii Framework Forum

Jump to content

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

differnece between hasmany() and hasone() [canceled]

#1 User is offline   tklustig 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 267
  • Joined: 20-April 17
  • Location:Hannover

Posted 06 December 2017 - 01:14 PM

Hi guys,
I'm al ittle bit confused about Gii generating relationships. In my database, all cardinalites are 1:n. This is,because no foreign key has been defined as unique.
Nevertheless, gii produced a relationship in table bewerber like this


    public function getPerson() {
        return $this->hasone(\common\modules\basis\models\Person::className(), ['id' => 'id_person']);
    }
    }


and in table person like this

    public function getBewerbers() {
        return $this->hasMany(\frontend\modules\bewerber\models\Bewerber::className(), ['aktualisiert_von' => 'id']);
    }


What is diffrence between these methods defining relationships. Why is Gii creating two different relationships?
Moigrationscript relationships are based on is like this for person:

  $this->createTable('person', [
            'id' => $this->primaryKey(),
.
.
.

                ], $tableOptions);


and this for bewerber

        $this->createTable('bewerber', [
            'id' => $this->primaryKey(),
            'id_person' => $this->integer(10),
.
.
            'FOREIGN KEY ([[id_person]]) REFERENCES person ([[id]]) ON DELETE CASCADE ON UPDATE CASCADE',
.
.
                ], $tableOptions);

0

#2 User is offline   alrazi 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,490
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 06 December 2017 - 04:05 PM

I think it make sense if you see it like a Bewer belongs to a person and a person could have many Bewers, so when you define that foreign key on bewers table gii will read the meta data and generate the required methods for you.

why gii generates 2 methods imagine you have a listview that displays bewers and the name of the person it belongs to, the relationship works other way around as well if you are displaying user you might wanna list all his bewers.

// Bewer model
public function getPerson()
{
// ...
}


// Person model
public function getBewerbers()
{
// ...
}

1

#3 User is offline   tklustig 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 267
  • Joined: 20-April 17
  • Location:Hannover

Posted 07 December 2017 - 02:19 PM

View Postalrazi, on 06 December 2017 - 04:05 PM, said:

I think it make sense if you see it like a Bewer belongs to a person and a person could have many Bewers, so when you define that foreign key on bewers table gii will read the meta data and generate the required methods for you.

why gii generates 2 methods imagine you have a listview that displays bewers and the name of the person it belongs to, the relationship works other way around as well if you are displaying user you might wanna list all his bewers.

// Bewer model
public function getPerson()
{
// ...
}


// Person model
public function getBewerbers()
{
// ...
}



Okay.Thx for this information. As i know now, Gii will implement at tabel having ForeignKey hasmany() and at table having PrimaryKey hasOne-method...
0

#4 User is offline   MetaCrawler 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 391
  • Joined: 12-February 11
  • Location:Germany

Posted 08 December 2017 - 02:30 AM

Hi,

Additionally I think it is pretty well explained here:
http://www.yiiframew...aring-relations

If you don't know the guide already I would suggest to keep the link open in a tab. ;)
It's a pretty good ressource to get an idea how things work and should be done.

Best Regards
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