Yii Framework Forum: model->with( 'relative' )->findByPk( $id ) - Yii Framework Forum

Jump to content

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

model->with( 'relative' )->findByPk( $id ) How to access $model->relative->attribute in the view Rate Topic: -----

#1 User is offline   DerekC 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 09-December 09
  • Location:Europe

Posted 16 December 2009 - 02:03 PM

Could someone please point me to the docs where it explains how to access the $model->relative->attribute in the view?

E.g. in the models there are relations
User HAS_ONE Status
and 
Status BELONGS_TO User


The Tables are:
User:
CREATE TABLE "user" (
	"id"		VARCHAR NOT NULL ,
	"password"	VARCHAR NOT NULL ,
	"email"		VARCHAR NOT NULL ,
	"first_name"	VARCHAR,
	"last_name"	VARCHAR,
	"status_id"	INTEGER,
	"group_id"	INTEGER,
	PRIMARY KEY ("id")
);


Status:
CREATE TABLE "status" (
	"id"		INTEGER NOT NULL,
	"name"		VARCHAR NOT NULL,
	PRIMARY KEY ("id")
);


In the controller:
User::model->with( 'status' )->findByPk( $id );


In the view I want to access the $model->status->name instead of the status code. But I get this error:

Trying to get property of non-object

00040: <td><?php echo $user->status->name; ?></td>

0

#2 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 16 December 2009 - 02:57 PM

View PostDerekC, on 16 December 2009 - 02:03 PM, said:

Could someone please point me to the docs where it explains how to access the $model->relative->attribute in the view?

E.g. in the models there are relations
User HAS_ONE Status
and 
Status BELONGS_TO User
...
In the controller:
[code]User::model->with( 'status' )->findByPk( $id );


In the view I want to access the $model->status->name instead of the status code. But I get this error:

Trying to get property of non-object

00040: <td><?php echo $user->status->name; ?></td>



I don't think this is documented. If you have a BELONGS_TO relationship you will get an object, if you have a HAS_MANY relationship you will get an array. I haven't used HAS_ONE but I would try
echo $user->status[0]->name;


/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#3 User is offline   DerekC 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 09-December 09
  • Location:Europe

Posted 16 December 2009 - 04:03 PM

@Tommy
Thanks for the suggestion. Unfortunately I still get the same error.
0

#4 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,689
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 16 December 2009 - 08:38 PM

@DerekC
If Status BELONGS_TO User then table status should have user_id field.
HAS_ONE is just a special case of HAS_MANY.

In your case, User BELONGS_TO Status.
God is real unless declared as integer
0

#5 User is offline   DerekC 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 09-December 09
  • Location:Europe

  Posted 17 December 2009 - 08:41 AM

@ Angel De La Noche

Thanks for your advice. This has solved the problem!
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