tags:

views:

215

answers:

3

I aim to toggle the status number in the contacts table in the contact_status_id field each time a link containing the id of the contact is clicked. The page shows no errors, but the action does not change the number. There is no validation implemented yet.

Perhaps a set of fresh eyes can spot a problem?

function inbox_toggle_number_status($id=null)
{
    //Call from the inbox when the number is clicked and status toggled.

    $this->User->Contact->id = $id;

    if (!empty($id))
    {

        $current_status = $this->User->Contact->find('first', array('conditions' => array('id' => $id)));

        if ($current_status['Contact']['contact_status_id'] == '1'):
            $this->User->Contact->saveField('contact_status_id', '2');
            exit();

        elseif ($current_status['Contact']['contact_status_id'] == '2'):
            $this->User->Contact->saveField('contact_status_id', '3');
            exit();

        elseif ($current_status['Contact']['contact_status_id'] == '3'):
            $this->User->Contact->saveField('contact_status_id', '2');
            exit();

        else:
            exit();

        endif;
    }
}
A: 

I've never had much luck with some of the save methods in cake. For that reason, I almost always use Model::save( array() ), with the array containing both the id and the field to update. If you wanted to go this route, you could do:

if(!empty($id)) {
    $arr = array( 'Contact' => array( 'id' => $id ) );

    $current_status = $this->User->Contact->find('first', array('conditions' => array('id' => $id)));

    if ($current_status['Contact']['contact_status_id'] == '1'):
        $arr['Contact']['contact_status_id'] = 2;
    <other conditional clauses here>


    $this->User->Contact->save($arr);
    exit();
}

If you want to continue with your route, please set your debug level 2 to and let us know if the script is even attempting to save the values into the db (give us the SQL dump at the bottom of the page).

Travis Leleu
A: 

I had an idea; see this line?

$this->User->Contact->id = $id;

this id setting might only last for the first query executed after it is set,

so

you might have to set it again just before the if statement containing the saveField calls.

however, you don't really need it before your find(...) call because you have the $id set as part of the find(...) 'conditions' array.

Solution

try moving $this->User->Contact->id = $id , to the line after find(...), so it will be set for the coming setField(...) call.

Also

instead of using the !empty(...), try beginning the function with

if(!isset($id)){
    $this->redirect(/*some url with an error message*/ );
}

followed by the code that would execute if the $id is valid

smchacko
+1  A: 
function inbox_toggle_number_status( $id = null )
{
    if( !$id )
    {
        $this->Session->setFlash( 'no id' );
        $this->redirect( array( 'action' => 'index' ) );
    }

    $this->User->Contact->id = $id;
    $current_status = $this->User->Contact->read( null, $id );

    switch( $current_status['Contact']['contact_status_id'] )
    {                
        case 1:
            $this->User->Contact->saveField( 'contact_status_id', 2 );
            break;

        case 2:
            $this->User->Contact->saveField( 'contact_status_id', 3 );
            break;

        case 3:
            //should yu not go back to 1?
            $this->User->Contact->saveField( 'contact_status_id', 2 );
            break;
    } // switch
}
dogmatic69