Welcome Guest, Not a member yet? Register   Sign In
very easy, but i am fool
#1

[eluser]nirbhab[/eluser]
hello frndz, i am new to MVC and CodeIgniter, i am pretty impressed with its working but i am facing lil problem i think u MVC gurus can easily help me out.

So, lets begin:

Controller:
<?php
class Blog extends Controller
{
function __construct()
{
parent::Controller();
}
function index()
{
$this->load->model('tut/blogmodel','name');
$this->name->hello();
}
}
?>

Model:
<?php
class Blogmodel extends Model
{
function __construct()
{
parent::Model();
}
function hello()
{
echo 'hello';
}
}
?>
ERROR:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Blog::$name

Filename: tut/blog.php

Please help me out from this simple problem who has made me idiot.
#2

[eluser]cinewbie81[/eluser]
the problem is because of the statment :
$this->name->hello();

what's that statement for ??
#3

[eluser]nirbhab[/eluser]
Following statement has three parameters
1. model name
2. reference name by which we can refer the object
3. TRUE-database access to the model

$this->load->model('tut/blogmodel','name');

this statement calls the function in model having function 'hello()'
and 'name' refers to the object of loaded.
$this->name->hello();
#4

[eluser]Armchair Samurai[/eluser]
Your file name is incorrect: it should be blogmodel.php.

Take a look at the naming conventions for models in the user guide
#5

[eluser]nirbhab[/eluser]
I got a solution after a long brainhits,

Changes:
<?php
class Blog extends Controller
{
function __construct()
{
parent::Controller();
$this->load->model('tut/blogmodel','name');
}
function index()
{
//moved to constructor
//$this->load->model('tut/blogmodel','name');
$this->name->hello();
}
}
?>

n its working perfectly fine.
#6

[eluser]cinewbie81[/eluser]
Now i dont get it !!
the function index() will be loaded everytime u load Blog module ... By moving that statement to constructor will solved ur problem is still UNBELIEVABLE !!
#7

[eluser]Gyorgy Fekete[/eluser]
It was the same problem with my model, but i don't know why. If you place the load model in the index function it should work but it isn't, but if you moved the model loading function to the constructor it worked.

Why?

Is there a solution? I like to load models conditionally (inside if-else) in my controller, not to load every model that i will or will not use in that particular controller, it's just a waste of memory and performance.
#8

[eluser]tonanbarbarian[/eluser]
Actually your problem is not what you think it is

Because you are using __construct you are going to have this problem and a lot more I think.

The fact that $this->load->model did not work unless it was in the constructor suggests that the object has not been instantiated correctly. I suspect that loading a library, help etc will also not work correctly from inside a method, only your controller.

Try changing your code to this
Controller:
Code:
<?php
class Blog extends Controller
{
function Blog()
{
parent::Controller();
}
function index()
{
$this->load->model('tut/blogmodel','name');
$this->name->hello();
}
}
?>

Model:
Code:
<?php
class Blogmodel extends Model
{
function Blogmodel()
{
parent::Model();
}
function hello()
{
echo 'hello';
}
}
?>

CI used the old PHP 4 method of constructors where the constructor method for the object is the name of the object
If you use __construct things may not load and in particular reference properly.

So give this a try and see if it works properly now
#9

[eluser]Gyorgy Fekete[/eluser]
No. I changed the __construct functions, but the problem is still the same. I don't know why and it bugs me out... somehow the load->model method not loading the class properly if it's inside a function different from the class' constructor (regardless if the name of the constructor is __construct)
#10

[eluser]tonanbarbarian[/eluser]
The problems is exactly what Armchair Samurai suggested before

You have to the the filename the same as the classname in the model.
You have the filename blogmodel but you indicated in your first post that the filename is blog.php

I also suspect that renaming the model to 'name' might be an issue
here is what I would suggest trying, and not that I have renamed your Controller because you cannot have 2 classes with the same name, and pluralising the controller is a standard I like to use to avoid confusion

application/controllers/blogs.php
Code:
<?php
class Blogs extends Controller {

  function Blogs() {
    parent::Controller();
  } // Blogs()

  function index() {
    $this->load->model('tut/blog');
    $this->blog->hello();
  } // index()

} // class Blogs
?>

application/models/tut/blog.php
Code:
<?php
class Blog extends Model {

  function Blog() {
    parent::Model();
  } // Blog()

  function hello() {
    echo 'hello';
  } // hello()

} // class Blog
?>

so you will now need to access it via http://localhost/blogs/index or similar




Theme © iAndrew 2016 - Forum software by © MyBB