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

[eluser]Unknown[/eluser]
Hello my friends ^^

I'm new in codeigniter, and today i get some erros with it about model inside model (does not work). My solution was like this:

$dados = array();
while(list($id) = mysql_fetch_row($sql))
{
$this->load->model('Noticia');
$this->Noticia = new Noticia($id);
$dados[] = $this->Noticia;
}

So, i dont need to call initialize to pass the ID of notice to the object, and now model inside another model work fine ^^

it's all sure? i dont will have problems in the future?

thanks (...english)
#2

[eluser]jedd[/eluser]
Hello Henrique and welcome to the CI forums.


[quote author="Henrique Mascarenhas" date="1258232121"]
... about model inside model (does not work). My solution was like this:
[/quote]

You didn't say whether you did that code in your Controller or your Model .. ?

In general terms, CI doesn't use a Model like a traditional Object - we usually load each Model only once , and treat it as a kind of library or collection of related database interactions.

With that in mind, there's a couple of ways you can solve your problem.

First, if you have two models that need to call functions in each other's code you can use the get_instance() function to gain access to one from the other.

Alternatively you can merge the functionality of the two models into one bigger model. This might be easier - a model can provide an interface to multiple tables, and this is an OK thing to do.

Looking at the code you've provided, I think you might be better getting all your rows in one fell swoop - using the result_array() function for example.

If you want to treat your Models as Objects - in the classic definition of an Object - then you should search through the forums for the word 'object' in thread titles, as the subject comes up fairly regularly.
#3

[eluser]Unknown[/eluser]
t's in my model:

Code:
function seleciona($num)
    {
        $dia = date('Y-m-d');
        $hora = date('H');
        $minuto = date('i');
        $segundo = date('s');
        
        $sql = "SELECT id FROM noticias WHERE hora < '$hora:$minuto:$segundo' OR dia < '$dia' ORDER BY dia DESC LIMIT $num";
        $sql = mysql_query($sql);
        
        if(!$sql) {
            echo $this->Estilo->htmlErro('Não consegui buscar notícias');
            return false;
        }
        
        $dados = array();
        while(list($id) = mysql_fetch_row($sql))
        {
            $this->load->model('Noticia');
            $this->Noticia = new Noticia($id);
            $dados[] = $this->Noticia;
        }
        
        return $dados;
    }

i like to return the results in objects, so, i have just to foreach the result and use it in view, as in this example:

Code:
&lt;?php
class Inicial extends Controller
{
    function index()
    {
        $this->load->model('Noticia');
        $dados = $this->Noticia->seleciona(100);
        foreach($dados as $N)
        {
            echo $N->titulo . '<br />';    
        }
        exit();
        $this->load->view('index',$dados);    
    }
}
?&gt;

using get_instance i cannot pass parameters to the constructor of class...
my fear is get future erros with this code.... but, maybe it is just more one way to solve the problem...

each object need a ID, example:

Code:
class Noticia extends Model
{
    public $id = NULL;
    public $titulo = NULL;
    public $chamada = NULL;
    public $conteudo = NULL;
    public $dia = NULL;
    public $hora = NULL;
    
    public $Estilo = NULL;
    
    function __construct($id = NULL)
    {
        parent::Model();
        $this->load->database();
        
        $this->load->model('Estilo');
        $this->Estilo = new Estilo;
        
        if(!empty($id))
        {
            $this->id = $id;    
            
            $sql = "SELECT titulo,chamada,conteudo,dia,hora,galeria_id FROM noticias WHERE id = $id";
            $sql = mysql_query($sql);
            
            list($titulo,$chamada,$conteudo,$dia,$hora,$galeria_id) = mysql_fetch_row($sql);
            
            $this->titulo = $titulo;
            $this->chamada = $chamada;
            $this->conteudo = $conteudo;
            $this->dia = $dia;
            $this->hora = $hora;
            
            if($galeria_id)
            {
                $this->load->model('Galeria');
                $this->Galeria = new Galeria($galeria_id);
            }
        }
    }
    
    function seleciona($num)
    {
        $dia = date('Y-m-d');
        $hora = date('H');
        $minuto = date('i');
        $segundo = date('s');
        
        $sql = "SELECT id FROM noticias WHERE hora < '$hora:$minuto:$segundo' OR dia < '$dia' ORDER BY dia DESC LIMIT $num";
        $sql = mysql_query($sql);
        
        if(!$sql) {
            echo $this->Estilo->htmlErro('Não consegui buscar notícias');
            return false;
        }
        
        $dados = array();
        while(list($id) = mysql_fetch_row($sql))
        {
            $this->load->model('Noticia');
            $this->Noticia = new Noticia($id);
            $dados[] = $this->Noticia;
        }
        
        return $dados;
    }
    
    
    function associa($galeria_id)
    {
        $sql = "UPDATE noticias SET galeria_id = $galeria_id WHERE id = $this->id";
        $sql = mysql_query($sql);
        
        if($sql) return true;
        else return false;
    }
    
    
}


so, i cannot make a biggest model...
the advantage i see solving this problem like this, is that you can pass parameters, and return models as objects =)




Theme © iAndrew 2016 - Forum software by © MyBB