2

Possible Duplicate:
Call to a member function on a non-object

I receive this error : Trying to get property of non-object [from Model] When i try to retrieve id from database by inputting a string which in this case is 'something'

This is my Model

   function getRoleId($role_name='')
         {

            $conditions = array('role_name'=> $role_name);
            $this->db->where($conditions);
            $this->db->select('id');
            $query = $this->db->get('roles');       
            $row   = $query->row();
            return $row->id;
         }

Controller ($radioRole here is = something) but when it reaches the model it becomes something else.

$radioRole  = 'something';
$insertData['role_id']            = $this->user_model->getRoleId($radioRole);

UPDATE

print_r($radioRole);
$insertData['role_id']            = $this->user_model->getRoleId($radioRole);

Shows $radioRole which is in this case 'something'

but when it reaches user_model and when i do a print_r it shows something else, 'buyer' , it doesnt even show the print_r done on controller. Feels like its being over written :O but i got no idea :O

print_r($query)

  CI_DB_mysql_result Object ( [conn_id] => Resource id #37 [result_id] => Resource id #64 [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 0 [row_data] => )
Community
  • 1
  • 1
CodeGuru
  • 3,645
  • 14
  • 55
  • 99
  • You should rewrite your question to be much more specific about what it is that you are **asking**. Also display the exact error message that you get when you run your code. – regulatethis Dec 20 '12 at 02:30
  • 1
    You're going to have to show more code. – Darius Dec 20 '12 at 02:31
  • Are you sure your query is correct? Are you sure there is a result from the database? – Supericy Dec 20 '12 at 02:42
  • Do a `print_r($query->row());die();` right after your `$query=` line and see if your getting any results. – Jeemusu Dec 20 '12 at 02:45
  • The problem is that your query is either returning 0 results, or its returning lots of results (more than one). If it's the latter you'll need a foreach loop to access the result object. `foreach ($query->result() as $row) { echo $row->id; }` – Jeemusu Dec 20 '12 at 02:49
  • @Jeemusu Nope! :x & its suppose to return 1 result only :x – CodeGuru Dec 20 '12 at 02:49
  • Ok, well now we know the problem is with the query. Try doing another `print_r` only this time `print_r($query);die();` And see what output if any the query is giving us. Are you sure there is a row with role_name = 'something' in your database? – Jeemusu Dec 20 '12 at 02:55
  • @Jeemusu i have updated my question, realise it was some other error , not model – CodeGuru Dec 20 '12 at 03:12
  • Update your question with what Jeemusu is asking. A `print_r($query);`, without this we don't know what's being returned – Bankzilla Dec 20 '12 at 03:18

1 Answers1

0

Your where is wrong:

$conditions = array('role_name'=> $role_name);
$this->db->where($conditions);

should be

$conditions = array('role_name', $role_name);
$this->db->where($conditions);

EDIT:

as mentioned in the comments, the comma is necessary if the condition is directly within the where as such:

$this->db->where('role_name',$role_name);
stormdrain
  • 7,915
  • 4
  • 37
  • 76
  • 1
    His `where` was perfectly fine... Your conditions are a flat array with two items, non of which have keys. His was an array of key => values . – Jeemusu Dec 21 '12 at 08:10
  • @Jeemusu, i've already pushed the data into the model function but something is overwriting it. :( Any idea? MVC's Trigger? if there is such a thing?... – CodeGuru Dec 21 '12 at 08:30
  • @RainbowHat, I'm guessing this isn't a site you built yourself? I can't think of anyway it could be getting overwritten between calling the function and the model. What happens if you write the param directly? I.E. `$insertData['role_id'] = $this->user_model->getRoleId('something');` – Jeemusu Dec 21 '12 at 08:39
  • @Jeemusu, i found the problem -.- , my other function on the same page is overwriting it, i'm not sure why. I only use that function to call for "Check" $this->form_validation->set_rules('email','required|trim|valid_email|xss_clean|callback__check_email'); – CodeGuru Dec 21 '12 at 08:43
  • I fixed it by using another function. Any idea why does it conflict? – CodeGuru Dec 21 '12 at 08:57
  • @Jeemusu dur, yup you're right. Thanks for clarifying. I was thinking of the way CI takes the condition directly in the `where` – stormdrain Dec 21 '12 at 12:40