diff options
-rwxr-xr-x | codebase/db_phplaravel.php | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/codebase/db_phplaravel.php b/codebase/db_phplaravel.php new file mode 100755 index 0000000..54f371f --- /dev/null +++ b/codebase/db_phplaravel.php @@ -0,0 +1,75 @@ +<?php +namespace Dhtmlx\Connector\DataStorage; +use Dhtmlx\Connector\DataProcessor\DataProcessor; +use \Exception; + +class PHPLaravelDBDataWrapper extends ArrayDBDataWrapper { + + public function select($source) { + $className = $source->get_source(); + return new ArrayQueryWrapper($className::all()->toArray()); + } + + protected function getErrorMessage() { + $errors = $this->connection->getErrors(); + $text = array(); + foreach($errors as $key => $value) + $text[] = $key." - ".$value[0]; + + return implode("\n", $text); + } + + public function insert($data, $source) { + $className = $source->get_source(); + $obj = $className::create(); + $this->fill_model_data($obj, $data)->save(); + + $fieldPrimaryKey = $this->config->id["db_name"]; + $data->success($obj->$fieldPrimaryKey); + } + + public function delete($data, $source) { + $className = $source->get_source(); + $className::destroy($data->get_id()); + $data->success(); + } + + public function update($data, $source) { + $className = $source->get_source(); + $obj = $className::find($data->get_id()); + $this->fill_model_data($obj, $data)->save(); + $data->success(); + } + + private function fill_model_data($obj, $data) { + $dataArray = $data->get_data(); + unset($dataArray[DataProcessor::$action_param]); + unset($dataArray[$this->config->id["db_name"]]); + + foreach($dataArray as $key => $value) + $obj->$key = $value; + + return $obj; + } + + protected function errors_to_string($errors) { + $text = array(); + foreach($errors as $value) + $text[] = implode("\n", $value); + + return implode("\n",$text); + } + + public function escape($str) { + throw new Exception("Not implemented"); + } + + public function query($str) { + throw new Exception("Not implemented"); + } + + public function get_new_id() { + throw new Exception("Not implemented"); + } + +}
\ No newline at end of file |