Welcome Guest, Not a member yet? Register   Sign In
Using Model inside another model
#1

[eluser]ThiagoPHX[/eluser]
Hey, i'm trying to use a model inside another model without success.

I'm doing like this

The structure of my model folder is the follow

/model
/vo
/UserVO.php
/dao
/UserDAO.php

In the controller i call the UserDAO.php, and UserDAO receives a UserVO object to execute the functions, or in a function that list the user in the database, the UserDAO sends to controller an array of UserVO object.

The UserDAO
Code:
class UserDAO extends Model
{
    const TABLE = 'users';
    
    public function __construct()
    {
        parent::Model();
    }
    
    public function listUsers($limit, $offset)
    {
        $this->load->model('vo/UserVO', 'UserVO');
        
        $result = $this->db->get(self::TABLE, $limit, $offset);
        
        $users = array();                
        foreach ($result->result() as $row) {
            $user = $this->UserVO;
            
            $user->setId($row->user_id);
            $user->setNome($row->user_nome);
            $user->setSenha($row->user_senha);
            
            $users[] = $user;
        }
        return $users;
    }
        
}

Thanks
#2

[eluser]Thorpe Obazee[/eluser]
As long as the model is already loaded(via autoload), you can for example do this.

Code:
$status = new Users_model;
$status->get_section();
#3

[eluser]Phil Sturgeon[/eluser]
Or you can use:

Code:
$CI = get_instance();
$CI->foo_model->get();
#4

[eluser]TheFuzzy0ne[/eluser]
Or:
Code:
$CI =& get_instance();
$this->UserVO =& $CI->UserVO;

Then you can access the model as you'd expect to.
#5

[eluser]ThiagoPHX[/eluser]
Thanks. Now is working!

My UserDAO Class
Code:
class UserDAO extends Model
{
    const TABLE = 'users';
    
    function __construct()
    {
        parent::Model();
        $this->load->model('UserVO');
    }
    
    function listUsers($limit, $offset)
    {
        $result = $this->db->get(self::TABLE, $limit, $offset);
        
        $users = array();                
        foreach ($result->result() as $row) {
            $user = new UserVO();
            
            $user->setId($row->user_id);
            $user->setNome($row->user_nome);
            $user->setSenha($row->user_senha);
            
            $users[] = $user;
        }
        return $users;
    }
}

But i'm not sure if using value objects is the best way to work with CI.

Someone has a better way ???

Thanks

PS: Sorry for my english :roll:
#6

[eluser]wiredesignz[/eluser]
[quote author="ThiagoPHX" date="1247816391"]Thanks. Now is working!

Code:
... $this->load->model('UserVO'); ...
... $user = new UserVO(); ...

[/quote]

You now have 2 UserVO model objects in memory.

But hey this is CodeIgniter forums, nobody seems to care about efficient coding anymore, just so long as it works.
#7

[eluser]Phil Sturgeon[/eluser]
I agree with wiredesignz here.

bargainph's method works but should only be used if you want multiple instances of the same model. This is unlikely.

My method works if you don't care about syntax.

TheFuzzy0ne's method is pure brilliance. It will keep CI syntax and will use the same model instance, in other words, just want you want.

Don't just use the first suggestion you see that works. You need to weigh up your options, try a few things out and see which works the best.
#8

[eluser]ThiagoPHX[/eluser]
OK, i'll try another things.

Thanks
#9

[eluser]OliverHR[/eluser]
Why not use:

Code:
$result->result_array();
#10

[eluser]Phil Sturgeon[/eluser]
He is passing them through another model to affect the results he has returned within this model. I assume that means the data would be different, and not just converting to an array.




Theme © iAndrew 2016 - Forum software by © MyBB