CodeIgniter Forums
Returning Boolean Values from model not working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Returning Boolean Values from model not working (/showthread.php?tid=12769)



Returning Boolean Values from model not working - El Forum - 10-30-2008

[eluser]timboland[/eluser]
I seem to be getting some odd results returning a boolean value from a model. Appears the type is lost when the result gets back to the controller, the controller keeps showing the value as FALSE, when I can see the logic should be returning true. Anyone seen this. Using 1.6.3

MODEL
Code:
function UpdateClientEmail($data)
    {
        $mybool = true;
        //FIRST MAKE SURE A USER DOESN'T EXIST
        $this->db->where('clientemail', $data['clientemail']);
        $user_exist = $this->db->get('clientemail');
        if ($user_exist->num_rows() > 0 )
        {
            $mybool = false;
        }
        else
        {
            $this->db->where('id', $data['id']);
            $this->db->update('clientemail', $data);
            $mybool = true;
        }
        
        return $mybool;
    }

CONTROLLER
Code:
function edituserfast($type)
    {
        //UPDATE
        $this->load->model('client/Client_model');
        if ($this->Client_model->UpdateClientEmail($_POST))
        {
            echo $_POST[$type];
        }
        else
        {
            echo "User name already exists";
        }
        
    }



Returning Boolean Values from model not working - El Forum - 10-30-2008

[eluser]fesweb[/eluser]
I think your function should actually return true or false, not a variable.

Try this:
Code:
function UpdateClientEmail($data)
    {
        //FIRST MAKE SURE A USER DOESN'T EXIST
        $this->db->where('clientemail', $data['clientemail']);
        $user_exist = $this->db->get('clientemail');
        if ($user_exist->num_rows() > 0 )
        {
            return FALSE;
        }
        else
        {
            $this->db->where('id', $data['id']);
            $this->db->update('clientemail', $data);
            return TRUE;
        }
    }



Returning Boolean Values from model not working - El Forum - 10-31-2008

[eluser]timboland[/eluser]
I had that before, no dice


Returning Boolean Values from model not working - El Forum - 10-31-2008

[eluser]GSV Sleeper Service[/eluser]
your code should work. you could try
Code:
return (bool) $mybool;



Returning Boolean Values from model not working - El Forum - 10-31-2008

[eluser]fesweb[/eluser]
Have you tried adding some debugging echoes to at least make sure that your function is returning the expected results?

Code:
$mybool = true;
        //FIRST MAKE SURE A USER DOESN'T EXIST
        $this->db->where('clientemail', $data['clientemail']);
        $user_exist = $this->db->get('clientemail');
        if ($user_exist->num_rows() > 0 )
        {
            $mybool = false;
echo 'mybool should be FALSE<br />';
echo "num rows = {$user_exist->num_rows()}";
        }
        else
        {
            $this->db->where('id', $data['id']);
            $this->db->update('clientemail', $data);
            $mybool = true;
echo 'mybool should be TRUE<br />';
echo "num rows = {$user_exist->num_rows()}";
        }



Returning Boolean Values from model not working - El Forum - 10-31-2008

[eluser]timboland[/eluser]
Yes, I swear Smile It's like PHP isn't handling the type conversion properly


Returning Boolean Values from model not working - El Forum - 10-31-2008

[eluser]fesweb[/eluser]
Weird.

Anyway, my last idea on this is to try having your function return the num_rows() value instead of the $mybool. Then you can test that with your controller.

Doesn't solve the larger problem, but it should make the controller work.
Code:
//UPDATE
        $this->load->model('client/Client_model');
        if ($this->Client_model->UpdateClientEmail($_POST) > 0)
        {
            echo "User name already exists";
        }
        else
        {
            echo $_POST[$type];
        }