accessing data in a controller

#1
[eluser]richzilla[/eluser]
Hi all, just a bit of a question regarding data access. Im having trouble accessing returned data in a controller, im sure im just missing something very obvious but i cant see it at all.

if i return data from my model to my controller as follows:

Code:
return $query->result();

Does that mean that i can access the data in the controller as follows:

Code:
$variable = $whatever-the-model-returned;
$variable->db_col;

so far all ive done is pass data straight to a view, where it goes as an array, but model appears to return an object, however im now starting to do my own apps an im getting a bit confused by this. Any help would be appreciated, just to straighten things out for me.
cheers

#2
[eluser]Jay Logan[/eluser]
Just make a variable that gets your model data.
Code:
$variable = $this->whatever_model->whatever_function();

Am I missing something? If you are trying to return your data like this:

Code:
$some_column = $variable->column_name;

Then you need to return your model data as a row_array(). Don't know if that's a requirement but it works for me. And result_array() lets you get more than one row.

#3
[eluser]richzilla[/eluser]
sorry for the slow reply, yes i thought thats how it worked but in my situation its refusing to. Ive pared my code back to the very basics to see if i can work out what im doing wrong. Basically i have :

my model:

Code:
<?php
    class User_model extends Model {
        function checklog($data) {
            $res= $this->db->query("SELECT * FROM users WHERE userName = '".$data['userName']."' AND userPass ='". $data['userPass']."'");
            if ($res)
            {
                return $res->result();
            }
            else
            {
                return 0;
            }
        }
        
    }

my controller :

Code:
<?php
    class Site extends Controller {
    
        function index() {
            $this->load->view('site_view');
        }
        
        function login() {
            $this->form_validation->set_rules('username','User Name', 'required');
            $this->form_validation->set_rules('password','Password','required');
            
            $data = array (
            'userName' => $this->input->post('username'),
            'userPass' => $this->input->post('password')
            );
            
            if ($this->form_validation->run() == FALSE) {
                $this->index();
            }
            else {
                $res= $this->user_model->checklog($data);
                
                if ($res != 0) {
                    $this->session->set_userdata(array('loggedIn' => TRUE, 'userId' => $res->id, 'userName' => $res->userName));
                    $this->secure();
                }
                else {
                    $err = array (
                    'error' => "The entered credentials were incorrect"
                    );
                    $this->load->view('site_view', $err);
                }
            }
        
        }
        
        function secure() {
            $data = array (
            'uName' => $this->session->userdata('userName')
            );
            
            $this->load->view('debug_view', $data);
        
        }
    
    }

EDIT: adding the view would help me a bit aswell...

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

&lt;html &gt;
&lt;head&gt;
    &lt;title&gt;Debug&lt;/title&gt;
  
&lt;/head&gt;
&lt;body&gt;

    &lt;?php echo $uName; ?&gt;

&lt;/body&gt;
&lt;/html&gt;

but i am consistently getting the error:

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/debug_view.php

Line Number: 10


the SQL is ok, ive tried it directly and it works. I know its probably very simple, but i just cant see it. Any help would be appreciated.

Thanks

#4
[eluser]jedd[/eluser]
You appear to be having an error in the one file that you did not post.

How strange.

Anyhoo, in your model, test for ->num_rows rather than just testing ($result) ... it'll make you happier.

If you want to return 0 on failure, then you need to test (later) that you have an object to talk to, and not assume it's an object (because at the moment it's 0 .. which isn't very objecty).

#5
[eluser]jedd[/eluser]
Actually, you don't appear to be loading debug_view anywhere here.

#6
[eluser]richzilla[/eluser]
Thanks for the help. So im right in understanding that the line $res->result() returns an object? so accessing it (if theres anything in it) would be a case of $object->attribute? how would i check if something is actually object, is there isobject function?

apoplogies for the simplistic questions, im getting there slowly.

Thanks.

#7
[eluser]richzilla[/eluser]
wrong controller, it must be getting to me...

thanks for the help, its greatly appreciated.

#8
[eluser]jedd[/eluser]
[quote author="ricardino" date="1251915192"]Thanks for the help. So im right in understanding that the line $res->result() returns an object? so accessing it (if theres anything in it) would be a case of $object->attribute? how would i check if something is actually object, is there isobject function?[/quote]

You don't really care if it's an object, so much as whether your query returned any rows. Hence the num_rows() function and suggestion.

I never use objects as return, btw, instead using the result_array and row_array functions .. but I'm old school Wink

#9
[eluser]wiredesignz[/eluser]
@ricardino, CI does the number of rows checking for you already in the DB_result library. You don't need to do it again.

If the result() of your query is empty then an empty array() is returned, otherwise an object is returned as you see.

Yes PHP does have an is_object() function.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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