Reputation: 415
I am attempting to find out how to understand the use of *Jamie Rumbelow's MY_Model* in specific of how to implement this idea.
I'm putting a game app together. There are many users and many characters. Each user can have many characters but only each character can be controlled by one user. I have a user's table that has a field called default_character
. This is the foreign key that is represented by the id field of the character.
In my code I am using the following function call to get the current logged in user.
$user_data = $this->user->get_by('username', $post_username);
What I would like to do is when I do this and it adds in the integer value from the default_character field instead I would like for it to go get the name of that character. What would this look like?
EDIT :
For some reason after attempting swatkins answer below its still only showing the number string value from the database. What I"m wanting to do is use it to get the character_name from the characters table.
tables
user - user_id, username, default_character_id
characters - id, character_name
object(stdClass)#23 (12) {
["user_id"]=>
string(5) "10003"
["first_name"]=>
string(3) "tom"
["last_name"]=>
string(8) "williams"
["username"]=>
string(9) "twilliams"
["password"]=>
string(40) "431db25cf8fe0ccf374365ae9c644c7bfdeb7399"
["password_hash"]=>
string(11) "ed95d918116"
["email_address"]=>
string(19) "[email protected]"
["status_id"]=>
string(1) "2"
["lock_date"]=>
string(19) "0000-00-00 00:00:00"
["default_character_id"]=>
string(1) "1"
["created_at"]=>
string(19) "2013-07-30 10:00:00"
["updated_at"]=>
string(19) "0000-00-00 00:00:00"
}
User Model(application/modules/user/models/user_model.php) (Loaded in MY_Controller)
<?php
class User_model extends MY_Model
{
public $primary_key = 'user_id';
public $has_many = array('characters' => array('model' => 'character_model' ));
public function __construct()
{
parent::__construct();
}
}
Character Model(application/modules/character/models/character_model.php)(Loaded in control panel controller)
<?php
class Character_model extends MY_Model
{
public $belongs_to = array('user' => array('primary_key' => 'default_character_id'));
public function __construct()
{
parent::__construct();
}
}
I'm wondering if this is a database structure issue. Any more thoughts on this topic? Any help would be great.
I'm still having some concerns on what I'm doing wrong. Anybody able to help me out?
Upvotes: 0
Views: 794
Reputation: 13630
Assuming you've set up your relationships correctly, you access children of the relationship with the with
method:
https://github.com/jamierumbelow/codeigniter-base-model
// from the docs
$post = $this->post_model->with('author')
->with('comments')
->get(1);
echo $post->author->name;
foreach ($post->comments as $comment)
{
echo $message;
}
So, this blog grabs the post
with an id of 1
, then also grabs the author
and array of comments
. You access them as properties of the parent object.
UPDATE
You need to set up your relationships in your models. So, you would set up your user
and character
entities like this:
class User_model extends MY_Model
{
public $has_many = array('characters');
}
class Character_model extends MY_Model
{
public $belongs_to = array('user' => array('primary_key' => 'default_character'));
}
Upvotes: 2