Yii Framework Forum: Check if relation exists? - Yii Framework Forum

Jump to content

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

Check if relation exists?

#1 User is offline   twisted1919 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 700
  • Joined: 23-October 10
  • Location:Romania

Posted 12 November 2017 - 12:34 PM

How do you properly check if a model has certain relation?
For now, i am doing:
public function testRelations()
    {
        $model = new Property();
        expect('model has user relation', method_exists($model, 'getUser'))->true();
    }

I have no idea if this is correct or if this is the right way to do it.
If not, how should i go about it ?
0

#2 User is offline   alrazi 

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

Posted 12 November 2017 - 12:55 PM

I think that is a reasonable approach to use method_exists to test if a model has certain relation since relationships are declared as methods or you could call the function and check if it returns a result and check that result is valid.
0

#3 User is offline   twisted1919 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 700
  • Joined: 23-October 10
  • Location:Romania

Posted 12 November 2017 - 02:03 PM

Thank you :)
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