dcolley
(Derek)
December 16, 2009, 7:03pm
1
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>
tri
(tri - Tommy Riboe)
December 16, 2009, 7:57pm
2
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
dcolley
(Derek)
December 16, 2009, 9:03pm
3
@Tommy
Thanks for the suggestion. Unfortunately I still get the same error.
@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.
dcolley
(Derek)
December 17, 2009, 1:41pm
5
@ Angel De La Noche
Thanks for your advice. This has solved the problem!