Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by Ray on Oct 22, 2009 15:26
open dhtmlx forum
Render sql and Delete statment in table without primary key

Hi,
i have a mysql table without primary key...just 4 colums that can have duplicate data in some colums...

in render sql i didn't specify id rows because it cause rendering with only one row per id...

for example...if i set colum "employee_id" like id field the grid show one record per employee_id.....

in this case delete operation completes ...

In case of leaving id blank in render sql command every rows are showed correctly but delete operation
fails .... "Incorrect field name used:"

how can i solve this problem?

Thank u



Answer posted by Alex (support) on Oct 23, 2009 05:52

Hello, 

without key there is no way update record in the database. There is no way to specify which row should be deleted.

If teh table can not be changed, you can use beforeRender event to specify custom id for grid row. That id should be unigue and allow to find the correctsponding record in the database:

function doBeforeRender($action){

$temp1 = $action->getValue("employee_id");

$temp2 = $action->getValue("type_id");

$action->setId($temp1." ".$temp2);

}

$grid->event->attach("beforeRender","doBeforeRender");

$grid->render_table("eemployees","employee_id","employee_id,type_id");

In order to save changes in the database you need to specify beforeProcessing event and insert, update and delete rows in this event manually.

Answer posted by Ray on Oct 23, 2009 07:36
tried but it says to me:

Call to undefined method GridDataItem::getValue() in <b>/var/www/punch1.0/includes/DataProcessor/away_employee_grid_connector.php
Answer posted by Ray on Oct 23, 2009 07:55
sorry...was my error....

i'm tryng and i'll let u know
Answer posted by Ray on Oct 23, 2009 08:10
i've tried......the render_sql works well but there is something wrong with update and delete....

i explode the id and make update statment:

here is my code:

<?php

    require("../dhtmlxSuite/dhtmlxConnector/php/codebase/grid_connector.php");

    $res=mysql_connect("localhost","root","sandbgroup");
    mysql_select_db("hr_mysql");

   
    $away_employee_grid = new GridConnector($res);
    $away_employee_grid->set_encoding("ISO-8859-1");
    $away_employee_grid->enable_log("Log/away_employee_grid_connector.log",true);
    $away_employee_grid->dynamic_loading(100);
   
   
   
    function away_update($data)
    {
           
            $temp = explode("@@",$data->get_id());
                $employee_id=$temp[0];
                $from_date=$temp[1];
            
             $to_date=$data->get_value("to_date");
             $status=$data->get_value("status");
            
             mysql_query("UPDATE hr_aux.hs_hr_away SET from_date='{$from_date}' , to_date='{$to_date}' , status='{$status}' where employee_id={$employee_id} ");
             $data->success(); //if you have made custom update - mark operation as finished
     }
   
      function away_delete($data)
        {
           
            $temp = explode("@@",$data->get_id());
                $employee_id=$temp[0];
                $from_date=$temp[1];
             //$employee_id=$data->get_value("employee_id");
             //$from_date=$data->get_value("from_date");
             $to_date=$data->get_value("to_date");
            

            
            
             //$conn->sql->query("UPDATE hs_hr_attendance SET in_note='{$in_note}' , out_note='{$out_note}' where attendance_id={$attendance_id}");
             mysql_query("DELETE FROM  hr_aux.hs_hr_away  where employee_id={$employee_id} AND from_date={$from_date} AND to_date={$to_date}");
             $data->success(); //if you have made custom update - mark operation as finished
         }
   
   
    function  before_render($row)
    {                           
        $row->set_id($row->get_value("employee_id")."@@".$row->get_value("from_date"));
       
        switch ($row->get_value("status"))                 
           {
               case "0":
                $row->set_value("status","Disattivato");
            break;
            case "1":
                $row->set_value("status","Maternità");
            break;
            case "2":
                $row->set_value("status","Allattamneto");
            break;
            case "3":
                $row->set_value("status","Distacco");
            break;
           
           }            
    }
   
   
    $away_employee_grid->event->attach("beforeDelete","away_delete");
    $away_employee_grid->event->attach("beforeUpdate","away_update");
    $away_employee_grid->event->attach("beforeRender","before_render");
   

$away_employee_grid->render_sql("select distinct a.emp_number, a.emp_lastname,a.emp_firstname, b.employee_id, b.from_date,b.to_date,b.status from hr_mysql.hs_hr_employee a join hr_aux.hs_hr_away b on a.emp_number=b.employee_id" ,"employee_id","employee_id,emp_lastname,emp_firstname,from_date,to_date,status");

?>
   

   
   
   
   

Answer posted by Alex (support) on Oct 26, 2009 10:31

hello,

the provided code looks correct. You can try to use $away_employee_grid->sql->query(...) instead of mysql_query(...) to enable log for the query result. It'll allow to get more details about the issue.