Welcome Guest, Not a member yet? Register   Sign In
Need help with a model
#1

[eluser]zeedy2k[/eluser]
I have tried to stay away from models because I dont quiet understand the function of them yet but now I have got to the point where I need to use it.

Im having problems getting it to display the information I need... I have listed the view and model below, the controller loads the model and I call the info from the view.

MODEL
======
Code:
class Cars extends Model {

    function Cars()
    {
        // Call the Model constructor
        parent::Model();
    }
    
    function getMake($id)
    {
        $this->db->select('name');
        $this->db->where("id",$id);
        $query = $this->db->get('manufacturers');
        return $query->result();
    }
    function getModel($id)
    {
        $this->db->select('model');
        $this->db->where("id",$id);
        $query = $this->db->get('models');
        return $query->row();
    }
}

How my view calls the info
=======
Code:
$make = $this->Cars->getMake($row->manufacturer);
echo $make->name;
$model = $this->Cars->getModel($row->model);
echo $model->model;

What this returns
==================
Code:
Array
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/searchresults.php

Line Number: 120
Array
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/searchresults.php

Line Number: 122

Any help will be appreciated.

Thanks
Robert
#2

[eluser]mck9235[/eluser]
My only guess would be to ensure you're loading the model before you try to make any calls to it.

Ex: $this->load->model("cars");

Sorry if this wasn't of any help, but you never know! Smile
#3

[eluser]zeedy2k[/eluser]
Figured it out but thanks for your reply mck9235
#4

[eluser]Unknown[/eluser]
did you try
Code:
echo $make[0]->name;
?
#5

[eluser]John Fuller[/eluser]
1. You never need models but they are helpful for organizing your application. At first CI did not even come with modules. The community prompted Rick to add them. There is nothing the models can do that the controller cannot do.

2. If you really want to understand models, don't put them in your view file. The model generally should be the gateway to the database. It sits between the DB and the controller. The controller tells the model what data is required, the model returns that data and then the controller passes the data to the view via the loader.

Put the second snippet into the controller, pass the data via the load viewer and then ask us again if you run into problems from there. Also make sure that you are actually loading your module.

Read the docs for examples of how the models are used.
#6

[eluser]wiredesignz[/eluser]
Quote: There is nothing the models can do that the controller cannot do.
You may as well go back to procedural coding.
#7

[eluser]John Fuller[/eluser]
[quote author="wiredesignz" date="1201437958"]
Quote: There is nothing the models can do that the controller cannot do.
You may as well go back to procedural coding.[/quote]

I think you missed the mark of my explanation. He said he needs to use it but clearly he is misunderstanding their usage. I believe that with CI you should at least have the controller to view relationship down strong before attempting to mess with models and I don't think dude is there yet.

Again, models were never a core feature in CI. Originally models weren't even a thought in the original code base of CI. Rick bolted models in after the community made the request. You would have a hard time getting by in CI without using Controllers, but the same is not true of Models.

So again, I was addressing the idea of need. First learn how to properly use the controller and view relationship. Learn how to use CI well, read through the documentation. Tackle models when you feel you have everything else down well.

Actually, some PHP noobs would probably be better off with procedural programming without a framework than going straight into a framework and creating a lot of bad habits. The framework abstracts so much that there is a lot of learning that may be missed. Just like Rails enables a developer to do a lot with basic web apps without really learning Ruby.

Just my 2 cents. I use models extensively. I suppose he can write his apps however he wants though.
#8

[eluser]wiredesignz[/eluser]
I didn't miss anything, the fact that CI has grown into a MVC framework is great, if it hadn't I wouldn't be using it. I was writing procedural Controller - View type applications in 1997 with vbscript (everthing was in the controller except the html).

My point is that here, on this forum, there's no point in helping `noobs` write code that doesn't conform to MVC principles.
#9

[eluser]John Fuller[/eluser]
[quote author="wiredesignz" date="1201442399"]I didn't miss anything, the fact that CI has grown into a MVC framework is great, if it hadn't I wouldn't be using it. I was writing procedural Controller - View type applications in 1997 with vbscript.

My point is that here, on this forum, there's no point in helping `noobs` write code that doesn't conform to MVC principles.[/quote]

Are you saying my post suggested something that does not conform to MVC principles?
#10

[eluser]wiredesignz[/eluser]
I'm saying models are an integral part of any MVC framework. But you're right of course, CodeIgniter doesn't claim to be an MVC framework.

Apologies for hijacking your thread guys.




Theme © iAndrew 2016 - Forum software by © MyBB