CodeIgniter Forums
Using Model inside another model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Using Model inside another model (/showthread.php?tid=20622)

Pages: 1 2


Using Model inside another model - El Forum - 07-15-2009

[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


Using Model inside another model - El Forum - 07-15-2009

[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();



Using Model inside another model - El Forum - 07-16-2009

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

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



Using Model inside another model - El Forum - 07-16-2009

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

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


Using Model inside another model - El Forum - 07-16-2009

[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:


Using Model inside another model - El Forum - 07-17-2009

[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.


Using Model inside another model - El Forum - 07-17-2009

[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.


Using Model inside another model - El Forum - 07-17-2009

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

Thanks


Using Model inside another model - El Forum - 07-17-2009

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

Code:
$result->result_array();



Using Model inside another model - El Forum - 07-20-2009

[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.