CodeIgniter Forums
$this->blog_model->func() and $this->blog->func() - 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: $this->blog_model->func() and $this->blog->func() (/showthread.php?tid=23010)



$this->blog_model->func() and $this->blog->func() - El Forum - 09-26-2009

[eluser]luffy[/eluser]
I want to use $this->blog->func() instead of $this->blog_model->func()

How should I do

If I use $this->blog->func(), it will shows :
Unable to locate the model you have specified: blog


$this->blog_model->func() and $this->blog->func() - El Forum - 09-27-2009

[eluser]überfuzz[/eluser]
[quote author="luffy" date="1254022315"]...
If I use $this->blog->func();
...
[/quote]
You might wanna read up on that first lesson in programing... ;-)
Code:
$this->blog->func();

//First you need to load the model, if it is a model, in the models folder.
$this->load->model('model_name'); //If theres a model called model_name it's now loaded.

//If you have created a method/function called method_get_stuff_from_db() you can use it, or maybe we could say reach, it like this.
$this->model_name->method_get_stuff_from_db();



$this->blog_model->func() and $this->blog->func() - El Forum - 09-27-2009

[eluser]pistolPete[/eluser]
Have a look at the user guide: http://ellislab.com/codeigniter/user-guide/libraries/loader.html

Quote:If you would like your model assigned to a different object name you can specify it via the second parameter of the loading function:
Code:
$this->load->model('Model_name', 'fubar');

$this->fubar->function();



$this->blog_model->func() and $this->blog->func() - El Forum - 09-27-2009

[eluser]steelaz[/eluser]
Don't forget that if you use $this->blog for a model, you can't name your controller "Blog".


$this->blog_model->func() and $this->blog->func() - El Forum - 09-27-2009

[eluser]wiredesignz[/eluser]
[quote author="steelaz" date="1254100228"]Don't forget that if you use $this->blog for a model, you can't name your controller "Blog".[/quote]

You can use blog as a model alias name and have a blog controller class, as PistolPete suggests above. (but it may be confusing)


$this->blog_model->func() and $this->blog->func() - El Forum - 09-27-2009

[eluser]steelaz[/eluser]
[quote author="wiredesignz" date="1254101053"][quote author="steelaz" date="1254100228"]Don't forget that if you use $this->blog for a model, you can't name your controller "Blog".[/quote]

You can use blog as a model alias name and have a blog controller class, as PistolPete suggests above. (but it may be confusing)[/quote]

Thanks for correcting me, I tried it some time ago and got an error (PHP4 fault?), will try again.