How to access data from PHP code

This tutorial shows you how to access data in entities from PHP code.

1. SaveData($appEntity, $data) – insert/update data in database for any entity in application and return id of new inserted data or true if you are updating data. This function is located in entity service. Parameter $appEntity must be a string and $data must be an array.

$entityService = Services::GetEntityService();

//INSERT 1 ROW
$data = array("Name" => "Product test", "Product category" => 1, "Product price" => "20,00");
$entityService->SaveData("Product", $data);

//UPDATE DATA
$data = array("Id" => 1, "Name" => "Product test", "Product category" => 1, "Product price" => "20,00");
$entityService->SaveData("Product", $data);

//INSERT MULTIPLE ROWS AT ONCE
$data = array("Data" => array(array("Name" => "Product test", "Product category" => 1, "Product price" => "20,00"), array("Id" => 1, "Name" => "Product test 2", "Product category" => 2, "Product price" => "17,00")));
$entityService->SaveData("Product", $data);

2. DeleteData($appEntity, $id) – delete row of entity data by entity name and id and return true. This function is located in entity service. Parameter $appEntity must be a string and $id must be an integer.

$entityService = Services::GetEntityService();
$result->status = $entityService->DeleteData("Product", 1);

3. GetDataById($appEntity, $id) – return data as array by entity name and id. This function is located in entity service. Parameter $appEntity must be a string and $id must be an integer.

$entityService = Services::GetEntityService();
$resultData = $entityService->GetDataById("Product", 2);

4. GetDataByAttributes($appEntity, $attributeFilters) – return data as array with rowNum and data keys. Parameters of this function are entity name and attributes names and values. This function is located in entity service. Parameter $appEntity must be a string and $attributeFilters must be an array.

$entityService = Services::GetEntityService();
$resultData = $entityService->GetDataByAttributes("Product",  array("Name" => "Product test", "Price" => "20,00"));

5. GetLoggedUser() – return an array with logged user information. This function is located in component service.

$componentService = Services::GetComponentService();
$loggedUser = $componentService->GetLoggedUser();

6. ExecuteQuery($query) – execute query on database and return array with data and rowNum keys. This function is located in database service. Parameter $query must be a string.

$databaseService = Services::GetDatabaseService();
$query = "SELECT * FROM Product WHERE Id > 0;";
$result->data = $databaseService->ExecuteQuery($query);

7. ExecuteCommand($command) – execute database command and return number of affected rows. This function is located in database service. Parameter $command must be a string.

$databaseService = Services::GetDatabaseService();
$command = "UPDATE Product SET  Price = \"0,00\" WHERE Price=\"\"; ";
$result->data = $databaseService->ExecuteCommand($command);