Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by Enrico Rinolfi on Oct 18, 2009 13:17
open dhtmlx forum
Updates with two primary keys

I'm creating update code for dhtmlxGrid and all works well when i make update stuff
with one primary key..

But .... in a case where i have 2 primary keys update process doesn't work:

Here's my code:

function config_employee_update($data_config)
    {
$employee_id=$data_config->get_value("employee_id");
$day_of_week=$data_config->get_value("day_of_week");
$number_of_punches=$data_config->get_value("number_of_punches");
$working_hours=$data_config->get_value("working_hours");
$first_pin_ts=$data_config->get_value("first_pin_ts");
$first_pout_ts=$data_config->get_value("first_pout_ts");
$last_pin_ts=$data_config->get_value("last_pin_ts");
$last_pout_ts=$data_config->get_value("last_pout_ts");
$number_of_punches_holidays=$data_config->get_value("number_of_punches_holidays");
$working_hours_holidays=$data_config->get_value("working_hours_holidays");
$pin_ts_holidays=$data_config->get_value("pin_ts_holidays");
$pout_ts_holidays=$data_config->get_value("pout_ts_holidays");

mysql_query("UPDATE hr_aux.hs_hr_hours SET number_of_punches='{$number_of_punches}' , " .
                    "working_hours='{$working_hours}' , first_pin_ts='{$first_pin_ts}' , " .
                    "first_pout_ts='{$first_pout_ts}' , last_pin_ts='{$last_pin_ts}' , " .
                    "last_pout_ts='{$last_pout_ts}' , number_of_punches_holidays='{$number_of_punches_holidays}' , " .
                    "working_hours_holidays='{$working_hours_holidays}' , pin_ts_holidays='{$pin_ts_holidays}' , " .
                    "pout_ts_holidays='{$pout_ts_holidays}' " .
                    "where employee_id={$employee_id} . "AND" . day_of_week={$day_of_week}");

$data_config->success(); //if you have made custom update - mark operation as finished
}
    
    $config_grid->event->attach("beforeUpdate","config_employee_update");
    
    $config_grid->render_sql("select hs_hr_hours.employee_id, hs_hr_employee.emp_lastname," .
                                "hs_hr_employee.emp_firstname, hs_hr_hours.day_of_week," .
                     "hs_hr_hours.number_of_punches,hs_hr_hours.working_hours,hs_hr_hours.first_pin_ts," .
                                "hs_hr_hours.first_pout_ts,hs_hr_hours.last_pin_ts,hs_hr_hours.last_pout_ts," .
                                "hs_hr_hours.number_of_punches_holidays,hs_hr_hours.working_hours_holidays," .
                                "hs_hr_hours.pin_ts_holidays,hs_hr_hours.pout_ts_holidays " .
                                "from hr_mysql.hs_hr_employee join hr_aux.hs_hr_hours " .
                                "on hs_hr_employee.emp_number=hs_hr_hours.employee_id",
                                "employee_id,day_of_week","employee_id,emp_lastname,emp_firstname,day_of_week,number_of_punches,working_hours," .
                                "first_pin_ts,first_pout_ts,last_pin_ts,last_pout_ts,number_of_punches_holidays," .
                                "working_hours_holidays,pin_ts_holidays,pout_ts_holidays");

In the definitio of render_sql commands i insered "first_primary_key,second_primary_key" ...

could u tell my why doesn't work?

N.B:
if i insert in the code one of the 2 primary key the update process works but obiviously not like i want..

is it a Syntax error?

Thank u
Answer posted by Stanislav (support) on Oct 19, 2009 05:07
a) you are using both IDs as visible columns in grid, if they are editable it can cause inconsistence. 
b) grid doesn't support complex IDs , but you can define extra rules - how multiple values can be used to form the single  ID

function  config_employee_update($data){
           $temp = explode("__",$data->get_id());
           $employee_id=$temp[0];
           $day_of_week=$temp[1];
           ... other update code ...
}

function  config_employee_render($data){
           $data->set_id($data->get_value("employee_id")."__".$data->get_value("day_of_week"));
}
$config_grid->event->attach("beforeUpdate","config_employee_update"); 
$config_grid->event->attach("beforeRender","config_employee_render"); 

as result , two values will be used to form the ID during rendering and during update , single value will be split to restore separate values. 
Answer posted by Enrico Rinolfi on Oct 19, 2009 04:38
Employee_id and day_of_week are not editable and only day_of_week are visible in the grid...however i don't understand the code cause
u write two function with the same name...

Cam u pls explain me well the code for render and for update?


Answer posted by Stanislav (support) on Oct 19, 2009 07:09
>>u write two function with the same name...
Above code is updated

>>Cam u pls explain me well the code for render and for update?
during rendering
       $data->set_id($data->get_value("employee_id")."__".$data->get_value("day_of_week"));
we combine two fields , which you are using as primary ID , and built a string ID for the row|

during updating, we take row ID and split it back, restoring two separate ID values
       $temp = explode("__",$data->get_id());
       $employee_id=$temp[0];
       $day_of_week=$temp[1];
Answer posted by Enrico RInolfi on Oct 19, 2009 07:58
mmm...still something wrong... render_sql works well and in the employee_id columm i see 1_1 ( employee_id__day_of_week) ... and could be ok.

but update not work....here is the code like u suggest me:

$config_grid = new GridConnector($res);
    $config_grid->enable_log("Log/config_timesheet_grid_connector.log",true);
    $config_grid->set_encoding("ISO-8859-1");
    $config_grid->dynamic_loading(50);
   
    //$config_grid->event->attach("beforeRender",giorno);
   
    function config_employee_update($data_config)
    {
                 $temp = explode("__",$data_config->get_id());
                $employee_id=$temp[0];
                $day_of_week=$temp[1];
             //$employee_id=$data_config->get_value("employee_id");
             //$day_of_week=$data_config->get_value("day_of_week");
             $number_of_punches=$data_config->get_value("number_of_punches");
             $working_hours=$data_config->get_value("working_hours");
             $first_pin_ts=$data_config->get_value("first_pin_ts");
             $first_pout_ts=$data_config->get_value("first_pout_ts");
             $last_pin_ts=$data_config->get_value("last_pin_ts");
             $last_pout_ts=$data_config->get_value("last_pout_ts");
             $number_of_punches_holidays=$data_config->get_value("number_of_punches_holidays");
             $working_hours_holidays=$data_config->get_value("working_hours_holidays");
             $pin_ts_holidays=$data_config->get_value("pin_ts_holidays");
             $pout_ts_holidays=$data_config->get_value("pout_ts_holidays");
           
             mysql_query("UPDATE hr_aux.hs_hr_hours SET number_of_punches='{$number_of_punches}' , " .
                                 "working_hours='{$working_hours}' , first_pin_ts='{$first_pin_ts}' , " .
                                 "first_pout_ts='{$first_pout_ts}' , last_pin_ts='{$last_pin_ts}' , " .
                                 "last_pout_ts='{$last_pout_ts}' , number_of_punches_holidays='{$number_of_punches_holidays}' , " .
                                 "working_hours_holidays='{$working_hours_holidays}' , pin_ts_holidays='{$pin_ts_holidays}' , " .
                                 "pout_ts_holidays='{$pout_ts_holidays}' " .
                                 "where employee_id={$employee_id} . AND . day_of_week={$day_of_week}");
            
             $data_config->success(); //if you have made custom update - mark operation as finished
     }
   
   
   
    function  config_employee_render($data)
    {                           
        $data->set_id($data->get_value("employee_id")."__".$data->get_value("day_of_week"));
    }
   
    $config_grid->event->attach("beforeUpdate","config_employee_update");
    $config_grid->event->attach("beforeRender","config_employee_render");
   
    $config_grid->render_sql("select hs_hr_hours.employee_id, hs_hr_employee.emp_lastname," .
                                "hs_hr_employee.emp_firstname,  hs_hr_hours.day_of_week," .
                                "hs_hr_hours.number_of_punches,hs_hr_hours.working_hours,hs_hr_hours.first_pin_ts," .
                                "hs_hr_hours.first_pout_ts,hs_hr_hours.last_pin_ts,hs_hr_hours.last_pout_ts," .
                                "hs_hr_hours.number_of_punches_holidays,hs_hr_hours.working_hours_holidays," .
                                "hs_hr_hours.pin_ts_holidays,hs_hr_hours.pout_ts_holidays " .
                                "from hr_mysql.hs_hr_employee join hr_aux.hs_hr_hours " .
                                "on hs_hr_employee.emp_number=hs_hr_hours.employee_id",
                                "employee_id","employee_id,emp_lastname,emp_firstname,day_of_week,number_of_punches,working_hours," .
                                "first_pin_ts,first_pout_ts,last_pin_ts,last_pout_ts,number_of_punches_holidays," .
                                "working_hours_holidays,pin_ts_holidays,pout_ts_holidays");
   
Answer posted by Stanislav (support) on Oct 19, 2009 09:47
Can you please add the next line
       $grid->enable_log("some.txt");
and provide the log records for the incorrect operation. 
Answer posted by Enrico Rinolfi on Oct 19, 2009 14:20
In the log u can see the same error at the top of Question:

====================================
Log started, 19/10/2009 06:10:24
====================================

SELECT DISTINCT first_pin_ts as value FROM hr_mysql.hs_hr_employee join hr_aux.hs_hr_hours on hs_hr_employee.emp_number=hs_hr_hours.employee_id

SELECT DISTINCT last_pin_ts as value FROM hr_mysql.hs_hr_employee join hr_aux.hs_hr_hours on hs_hr_employee.emp_number=hs_hr_hours.employee_id

SELECT  hs_hr_hours.employee_id, hs_hr_employee.emp_lastname,hs_hr_employee.emp_firstname,  hs_hr_hours.day_of_week,hs_hr_hours.number_of_punches,hs_hr_hours.working_hours,hs_hr_hours.first_pin_ts,hs_hr_hours.first_pout_ts,hs_hr_hours.last_pin_ts,hs_hr_hours.last_pout_ts,hs_hr_hours.number_of_punches_holidays,hs_hr_hours.working_hours_holidays,hs_hr_hours.pin_ts_holidays,hs_hr_hours.pout_ts_holidays FROM hr_mysql.hs_hr_employee join hr_aux.hs_hr_hours on hs_hr_employee.emp_number=hs_hr_hours.employee_id LIMIT 0,50

SELECT COUNT(*) as DHX_COUNT  FROM hr_mysql.hs_hr_employee join hr_aux.hs_hr_hours on hs_hr_employee.emp_number=hs_hr_hours.employee_id

Done in 0.00667190551758s



====================================
Log started, 19/10/2009 06:10:28
====================================

DataProcessor object initialized
1__0_gr_id => 1__0
1__0_c0 => 1__0
1__0_c1 => PESCE
1__0_c2 => GIOVANNA
1__0_c3 => 0
1__0_c4 => 0
1__0_c5 => 0.00
1__0_c6 => 00:00:00
1__0_c7 => 00:00:00
1__0_c8 => 00:00:00
1__0_c9 => 00:00:00
1__0_c10 => 10
1__0_c11 => 0.00
1__0_c12 => 00:00:00
1__0_c13 => 00:00:00
1__0_!nativeeditor_status => updated
ids => 1__0

Row data [1__0]

Incorrect field name used: number_of_punches

data

Incorrect field name used: working_hours

data

Incorrect field name used: first_pin_ts

data

Incorrect field name used: first_pout_ts

data

Incorrect field name used: last_pin_ts

data

Incorrect field name used: last_pout_ts

data

Incorrect field name used: number_of_punches_holidays

data

Incorrect field name used: working_hours_holidays

data

Incorrect field name used: pin_ts_holidays

data

Incorrect field name used: pout_ts_holidays

data

Event code for update processed

Edit operation finished
0 => action:updated; sid:1__0; tid:1__0;

Done in 0.0105981826782s

Everytime it says incorrect data field...as i write at first post if i make update process with only one id in where clause:

 mysql_query("UPDATE hr_aux..................where employee_id={$employee_id}"); the update process completes but
change obiviously too much data....

Answer posted by Stanislav (support) on Oct 20, 2009 02:15
Please try to change 
  $temp = explode("__",$data_config->get_id());
with
  $temp = explode("@@",$data_config->get_id());
and 
  $data->set_id($data->get_value("employee_id")."__".$data->get_value("day_of_week"));
with
  $data->set_id($data->get_value("employee_id")."@@".$data->get_value("day_of_week"));
Answer posted by Enrico Rinolfi on Oct 20, 2009 04:11
whao...it works.....

my last question is:

i add a function in order to change value of day_of_week during render...all works...the only thing is that it logs me an error...

the function is:

function  config_employee_render($data)
    {                           
        //$data->set_id($data->get_value("employee_id")."__".$data->get_value("day_of_week"));
        $data->set_id($data->get_value("employee_id")."@@".$data->get_value("day_of_week"));
       
        switch ($data->get_value("day_of_week"))                 
           {
               case "0":
                $data->set_value(day_of_week,"Domenica");
            break;
            case "1":
                $data->set_value(day_of_week,"Lunedì");
            break;
            case "2":
                $data->set_value(day_of_week,"Martedì");
            break;
            case "3":
                $data->set_value(day_of_week,"Mercoledì");
            break;
            case "4":
                $data->set_value(day_of_week,"Giovedì");
            break;
            case "5":
                $data->set_value(day_of_week,"Venerdì");
            break;
            case "6":
                $data->set_value(day_of_week,"Sabato");
            break;                
           }            
    }

an the error is:

Use of undefined constant day_of_week - assumed 'day_of_week' at /var/www/punch1.0/includes/DataProcessor/config_timesheet_grid_connector.php line 96

how can i do?

thank u