Welcome Guest, Not a member yet? Register   Sign In
MY Model Joins
#1

[eluser]xtremer360[/eluser]
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.

Code:
$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?

Code:
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)

Code:
<?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)
Code:
<?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?




Theme © iAndrew 2016 - Forum software by © MyBB