i have a problem im trying to deal with for a long time,
i have a codeigniter webapp that im building and i have a problem with the $this of codeigniter
i will show you with so examples:
this is in my controller
$this->site_model->blog_post('title');
this one work perfectly /\
if im loading an other / different model it will load it all good, but if im calling a function in the new model like this:
$this->Admin_model->blog_post('title');
it will give me this
Undefined property: Idx::$Admin_model
Call to a member function get_categories() on a non-object in
C:\dev\wamp\www\francebeautycoil\application\controllers\idx.php on line 22
if im doing the next code
public $ci = '';
$this->ci =& get_instance();
$this->load->model('Admin_model');
$this->ci->Admin_model->blog_post('title');
it will work.
ive tryed all the catital lettes, lowercase letters.
the thing is that its append not only on this model but on few libraries too.
please help, im stuck.
Every time you use an model you need to load it first , on second snipped you need to load Admin_model first
if im loading an other / different model
On second snppiet you are calling a method on Admin_model not loading it on line below
$this->Admin_model->blog_post('title');
so load it first
$this->load->model('Admin_model');
so it becomes
$this->load->model('Admin_model');
$this->Admin_model->blog_post('title');
Related
I am trying to create my own library so i can handle custom areas in my application. i have a small library located at application/libraries which i called randomizer. it looks like this:
Class Randomizer {
public function __construct()
{
parent::_construct();
$CI =& get_instance();
$CI->load->library('session');
$CI->load->database();
}
function test_function($name)
{
return 'Hi dear' . $name . 'welcome back!';
}
}
In my Controller i tried to test out the simple test_function:
public function index()
{
echo($this->Randomizer->test_function('John'));
exit;
}
And I am getting the following error
Call to a member function test_function() on a non-object
There are couple of possible errors i can see. first you are not loading the library you created inside your controller. in case you don't use $this->load->library('randomizer'); right before you call the library funcion. if you are going to use the library all over the controller then load it via the controller __consturct. Also i guess you are not extending an existing Class so the parent::_construct() is not needed. make sure you understand why you are using a library and when you should use helpers. Read more about Codeigniter Libraries
Please look closely to the documentation. There are a few problems.
First of all it seems that you have either not loaded the library ($this->load->library('randomizer')), otherwise you would get a syntax error because your are calling parent::_construct(), instead of parent::__construct. And you do not have to call that function anyway, because your class doesn't have a parent (it doesn't extend a class...).
Furthermore, although you declare your class with a capital, the variable will be lowercase. So you should call $this->randomizer->test_function()
In codeIgniter I auto load the url_helper.php
In my site I also have a phpbb forum and so within codeigniter im trying to include a script from the forum.
The problem is, phpbb tries to declare a function redirect() but its already declared in the url_helper.php so i get the following error
Cannot redeclare redirect() (previously declared in
C:\Apache24\htdocs\system\helpers\url_helper.php:531) in
C:\Apache24\htdocs\forum\includes\functions.php on line
2562
What can I do go go around this? Can I unset the function or remove the url_helper entirly in my controller function?
Still a bit of a hack, but see: http://php.net/manual/en/function.rename-function.php
You could create your own url_helper, include the CI url_helper, and call after include:
rename_function('redirect', 'ci_redirect');
Ok, I got a work around. In the codeigniter's helper library, before declaring a function, it first checks if it has been declared before or not. So....
In my controller class's constructor method, I load all the phpbb files I need. this way it declares the phpbb redirection function and codeigniter goes "ohh there is already a redirect function" and so it doesn't declare the redirect function... Problem solved
Something like this:
class Register extends CI_Controller{
public function __construct()
{
/* START phpbb */
.
.
.
require_once('forum/common.php');
require_once('forum/includes/functions_user.php');
require_once('forum/includes/functions_module.php');
/* END phpbb */
//Continue as normal
parent::__construct();
}
public function index(){
//Your stuff works as normal now
}
}
I am new to codeigniter, and I have developed a code to carry out queries on the database. I load the database using$this->load->database(); and perform a query, but when I run the code, the browser gives me the following error message:
A PHP Error was encountered Severity: Notice Message: Undefined property: Tutorial::$load.
Fatal error: Call to a member function database() on a non-object
This is the code I am using:
class Tutorial extends CI_Controller {
public function tutorial() {
$this->load->database();
$query = $this->db->query('SELECT user,pass,email FROM tablex');
foreach ($query->result() as $row) {
echo $row->title;
echo $row->name;
}
I am sure the $db variables in my database configuration file are properly set and I have even tried autoloading the database for all pages in the autoload.php config file; still having the same problem. Any ideas how to go about this?
Change
$this->load->database();
to
$this->load->library('database');
database is not a direct method. It is a library in codeigniter and you have to load it as a library.
You can also autoload database library in autoload.php.
UPDATE:
You are using the same name for your class and method. In PHP4, a method which has the same name as class name was treated as constructor, but if you are using codeigniter 2+, you have to use PHP5 constructor which is
function __construct()
{
parent::__construct();
/*Additional code which you want to run automatically in every function call */
}
You cannot give a method same name as class name in Codeigniter 2+. Change the method to anything else. You can name the method index if you want it to load by default.
This should solve your problem.
CodeIgniter User Guide, Creating Libraries Section:
To access CodeIgniter's native resources within your library use the
get_instance() function. This function returns the CodeIgniter super
object.
Normally from within your controller functions you will call any of
the available CodeIgniter functions using the $this construct.
$this, however, only works directly within your controllers, your
models, or your views. If you would like to use CodeIgniter's classes
from within your own custom classes you can do so as follows:
First, assign the CodeIgniter object to a variable:
$CI =& get_instance();
Once you've assigned the object to a variable, you'll use that
variable instead of $this:
$CI =& get_instance();
$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');
etc.
Hope this helps. You could also put the $CI in a constructor.
Your code would look something like this:
class Tutorial
{
public $CI;
/**
* Constructor.
*/
public function __construct()
{
if (!isset($this->CI))
{
$this->CI =& get_instance();
}
$this->CI->load->database();
}
}
I am new to zend framework. I found code in bootstrap file as below.
protected function _initDoctype()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');
}
can anyone explain me what "$this->bootstrap('view');" this mean?
From the same page where your snippet comes:
Now that we have a view, let's flesh
out our _initDoctype() method. In it,
we will first ensure the View resource
has run, fetch the view object, and
then configure it.
It sets up the view resource so that you can access it. Without it the following line would not return anything to put in the $view variable and you'd get the error:
Fatal error: Call to a member function doctype() on a non-object.
It's pretty straightforward, it literally bootstraps the View object.
Bootstraping is a step where you set up (configure and instantiate) your object, resolve dependencies, etc.
It is done because the View Object must be set before to be able to set a Doctype.
Ok so I have some gaps in my understanding of PHP OOP, classes and functions specifically, this whole constructor class deal. I use both Zend and CI but right now Im trying to figure this out in CI as it is less complicated.
So all Im trying to do is understand how to call a function from a view page in code igniter. I understand that might go against MVC but Im working with an api and search results not from my database, so basically, I want to define a function in my class that I am able to call in one of my view pages.. and I keep getting "Fatal error: Call to undefined function functionname" error no matter what I try.
I thought I just had to declare
public function testing() {
echo "testing testing 123;
}
but calling that from the view I get that error. Then I read something about having to go
parent::Controller();
in the index of the class where the testing function also resides? But that didnt work either. Anyways, ya, can someone explain what I need to do in order to call the "testing()" function on one of my view pages? and clarification on the constructor class and what exactly parent::Controller() even does, would be much appreciated as well.
Like you said, that goes against the concept of MVC, so a better bet would be to use a helper function instead of declaring it as a controller method. Or, even better, let your controller deal with all the search API stuff, then pass the search results from your controller to your view.
I agree with both those points, but there are instances where you don't get the data you need until the view has been loaded (e.g. like some php data inside inline javascript or something).
If that's the case, I'd use an ajax call (within the view) to hit a function in the controller (since you just need a url to call them) and send along post data if the function needs to be fed anything. Does that make sense?
Yeah, like you said, that goes against the concept of MVC, but nothing is impossible. We can make it simply and follow the CI concept. You just need to pass a Controller object to the view:
class Test extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function sayhi() {
echo "Hi from controller...";
}
public function index() {
$this->load->view('test', array('controller' => $this));
}
}
Then you can call function defined at the controller, for example I called sayhi() from my view page:
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h2>
<?= $controller->sayhi() ?>
</h2>
</body>
</html>