Public Variables With Models?

#1
[eluser]ShoeLace1291[/eluser]
I was wondering if it is possible to set public variables with models. Example:
[code]
$this->news->articleInfo($id = 1);
echo $articleTitle;
[code]

The above code would be in a controller.

#2
[eluser]cahva[/eluser]
Why you want to do that? Whats wrong with returning the result row? Anyway, you can do that if you want. Just use extract function. And you have to return the row as an array(for example row_array())
Code:
$article = $this->news->articleInfo(1);
extract($article);
echo $articleTitle; // Prints articleTitle if thats the field name you have

But ask yourself is this really necessary as this would be better:
Code:
$article = $this->news->articleInfo(1);
echo $article->articleTitle;

#3
[eluser]ShoeLace1291[/eluser]
Ok, so if I were to use this method:
Code:
$article = $this->news->articleInfo(1);
echo $article->articleTitle;

How would I set the articleTitle variable in my model?

#4
[eluser]cahva[/eluser]
Example:
Code:
class News extends Model {

    function __construct()
    {
        parent::__construct();
    }
    
    function articleInfo($id)
    {
        return $this->db->where('id',$id)->get('news')->row();
    }

}

As you can see articleTitle is nowhere to be found in the model. Thats because it comes from db(the field in the news table is "articleTitle").

EDIT: Oh yeah, almost forgot. Dont use uppercase letters in your database. That can cause trouble later(exporting).


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.