Welcome Guest, Not a member yet? Register   Sign In
Using Relationship in Jamie Rumbelow MY_model
#1

[eluser]Chirag Chamoli[/eluser]
I am using MY_model as is Codeigniter-base-model. I am unable to get the basic relationship working. I searched a lot but could find solution.

---
To start with I have 2 classes item_model.php & user_model.php both extend MY_Model.

In item_model class I have defined

Code:
public $belongs_to = array( 'user' => array( 'model' => 'user_model') );

and in the user_model
Code:
public $has_many = array( 'items' => array( 'model' => 'item_model') );

Now, in the controller when I call

Code:
$item_details = $this->item_model->with('user')->find_by_id($item_id);

I am getting the correct data for
Code:
$item_details
but I am unable to get information from the 'user' relationship.
Code:
$item_details->user_id->username;
.

I am getting following error
Code:
Message: Trying to get property of non-object

Table are standard

users table has id, username, ...
item table has id, user_id, title...

user_id is is setUp as FK to users.id.

Please help.
#2

[eluser]Chirag Chamoli[/eluser]
Any help, Please.:-)
#3

[eluser]Aken[/eluser]
Wouldn't it be $this->user->username ?
#4

[eluser]Chirag Chamoli[/eluser]
I had tried $item_details->user->username; didn't work this gives two errors. Severity: Notice Message: Undefined property: stdClass::$user Line Number: 5 A PHP Error was encountered Severity: Notice Message: Trying to get property of non-object –




Theme © iAndrew 2016 - Forum software by © MyBB