⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

callback_after_update + condition + $post_array



mcl

mcl
  • profile picture
  • Member

Posted 30 September 2013 - 22:25 PM

Hello,

 

I have a problem evaluating a condition. I have if((empty($post_array['Date_Deb'])) && ($post_array['Etat'] == '3')) as condition and Date_Deb is being updated even if Date_Deb is NOT empty.

The second part of the condition is evaluated correctly. If Etat is not equal to 3, Date_Deb is not updated.

You can see my code below.

Note that Date_Deb is set as a read only field but removing that constraint don't change anything. It seems GC is seeing $post_array['Date_Deb'] on update even if it had been previously filled in using the function.

 

Can you tell me what is wrong please?

function st3() {
                  $crud = new grocery_CRUD();
            $crud->set_table('t3');
            $crud->columns('ID','Nom_agt','Date_Rem','Date_Deb', 'Etat');
            $crud->set_subject('T3');
            $crud->set_relation('Etat','etat_debriefe','etat');
            $crud->set_relation('Nom_agt','agents','agt');
            $crud->fields('Nom_agt','Date_Rem','Date_Deb', 'Etat');
            $crud->edit_fields('Nom_agt','Date_Deb', 'Etat');
           $crud->field_type('Date_Deb', 'readonly');
            $crud->callback_after_update(array($this, 'update_date_debriefe'));
            $output = $crud->render();
            $this->_example_output($output);
    }    
function update_date_debriefe($post_array,$primary_key)
{
if((empty($post_array['Date_Deb'])) && ($post_array['Etat'] == '3'))  {
    $date_debriefe = array("ID" => $primary_key,"Date_Deb" => date('2013-07-01'));
    $this->db->update('t3',$date_debriefe,array('ID' => $primary_key));
   return true;
   }
}

mcl

mcl
  • profile picture
  • Member

Posted 30 September 2013 - 23:48 PM

I have found a way of doing it but i need to query db on each update and this add some execution time.

Any other way to store the content of Date_Deb in a variable before evaluating it ?

function update_date_debriefe($post_array,$primary_key)
{
$this->db->select('*')
                     ->from('t3')
                     ->where('ID', $primary_key);
            $db = $this->db->get();
            $row = $db->row(0);
            $DD = $row->Date_Deb;

if((empty($DD)) && ($post_array['Etat'] == '3'))  {
    $date_debriefe = array("ID" => $primary_key,"Date_Deb" => date('2017-01-10'));
    $this->db->update('t3',$date_debriefe,array('ID' => $primary_key));
   return $post_array;
   }
   
}

davidoster

davidoster
  • profile picture
  • Member

Posted 01 October 2013 - 04:15 AM

Hello.

Why don't you debug  / echo / inspect see the actual value that the field has on the $post_array after update.

Obviously there is something different there, otherwise it should evaluate the whole condition to true.

 

UPDATE: On your last post you reassign the same value as before of the ID. Isn't this autonumbered? Maybe this generates some problems?

Why don't you update just the date field?