# dhtmlxconnector:complex_updates [DHX documentation]

DHTMLX Docs & Samples Explorer

Index
dhtmlxconnector
Usage Guide

## Complex updates

By default connector generates all create|update|delete queries automatically, based on configuration. But in some cases it may be necessary to define your own logic. It can be done in one of two ways

- defining custom SQL code for operation
- using server side events to customize operations

#### custom SQL code

You can define your own SQL for specific action (Insert, Update or Delete) as follows:

            $gridConn->sql->attach("Update","Update tableA set name='{name}', price={price} where id={id}");
            //...
            $gridConn->render_sql(" .. ","id","price,name");

The parameters are the following:

- action name. Possible values are: “Update”, “Insert”, “Delete”
- SQL statement. It can use fields(or their aliases) which were mentioned in render_sql or render_table method used for loading data.

#### custom server side events

Grid provides set of events, which can be used to handle server side action in your custom way

          //data preprocessing before update
          function my_update($data){
                $price = $data->get_value("price");
                $price = intval($price);
                $data->set_value("price");
          }
          $conn->event->attach("beforeUpdate","my_update")

          //including additional field to request
          function my_update($data){
                $data->add_field("userId",1); //will be included in update
          }
          $conn->event->attach("beforeUpdate","my_update")

          //fully custom code
          function my_update($data){
                 $price=$data->get_value("price");
                 $id=$data->get_value("id");
                 $conn->sql->query("UPDATE some_table SET price='{$price}' where id={$id}");
                 $data->success(); //if you have made custom update - mark operation as finished
          }
          $conn->event->attach("beforeUpdate","my_update")