Welcome Guest, Not a member yet? Register   Sign In
Returning Boolean Values from model not working
#1

[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";
        }
        
    }
#2

[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;
        }
    }
#3

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

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

[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()}";
        }
#6

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

[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];
        }




Theme © iAndrew 2016 - Forum software by © MyBB