Skip to content

Commit 003056b

Browse files
Added connections for yii 2 and phpcake 3
1 parent 78aad87 commit 003056b

File tree

4 files changed

+306
-149
lines changed

4 files changed

+306
-149
lines changed

codebase/db_phpcake.php

Lines changed: 56 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -11,75 +11,62 @@
1111
1212
if you plan to use it for Oracle - use Oracle connection type instead
1313
**/
14-
class PHPCakeDBDataWrapper extends ArrayDBDataWrapper{
15-
public function select($sql){
16-
$source = $sql->get_source();
17-
if (is_array($source)) //result of find
18-
$res = $source;
19-
else
20-
$res = $this->connection->find("all");
21-
22-
$temp = array();
23-
if (sizeof($res)){
24-
$name = get_class($this->connection);
25-
for ($i=sizeof($res)-1; $i>=0; $i--)
26-
$temp[]=&$res[$i][$name];
27-
}
28-
return new ArrayQueryWrapper($temp);
29-
}
30-
31-
protected function getErrorMessage(){
32-
$errors = $this->connection->invalidFields();
33-
$text = array();
34-
foreach ($errors as $key => $value){
35-
$text[] = $key." - ".$value[0];
36-
}
37-
return implode("\n", $text);
38-
}
39-
40-
public function insert($data,$source){
41-
$name = get_class($this->connection);
42-
$save = array();
43-
$temp_data = $data->get_data();
44-
unset($temp_data[$this->config->id['db_name']]);
45-
unset($temp_data["!nativeeditor_status"]);
46-
$save[$name] = $temp_data;
47-
48-
if ($this->connection->save($save)){
49-
$data->success($this->connection->getLastInsertID());
50-
} else {
51-
$data->set_response_attribute("details", $this->getErrorMessage());
52-
$data->invalid();
53-
}
54-
}
55-
public function delete($data,$source){
56-
$id = $data->get_id();
57-
$this->connection->delete($id);
58-
$data->success();
59-
}
60-
public function update($data,$source){
61-
$name = get_class($this->connection);
62-
$save = array();
63-
$save[$name] = &$data->get_data();
64-
65-
if ($this->connection->save($save)){
66-
$data->success();
67-
} else {
68-
$data->set_response_attribute("details", $this->getErrorMessage());
69-
$data->invalid();
70-
}
71-
}
72-
73-
74-
public function escape($str){
75-
throw new Exception("Not implemented");
76-
}
77-
public function query($str){
78-
throw new Exception("Not implemented");
79-
}
80-
public function get_new_id(){
81-
throw new Exception("Not implemented");
82-
}
14+
class PHPCakeDBDataWrapper extends ArrayDBDataWrapper {
15+
public function select($sql) {
16+
if(is_array($this->connection)) //result of findAll
17+
$query = $this->connection;
18+
else
19+
$query = $this->connection->find("all");
20+
$temp = array();
21+
foreach($query as $row)
22+
$temp[] = $row->toArray();
23+
return new ArrayQueryWrapper($temp);
24+
}
25+
protected function getErrorMessage() {
26+
$errors = $this->connection->invalidFields();
27+
$text = array();
28+
foreach ($errors as $key => $value){
29+
$text[] = $key." - ".$value[0];
30+
}
31+
return implode("\n", $text);
32+
}
33+
public function insert($data, $source) {
34+
$table = TableRegistry::get($source->get_source());
35+
$obj = $table->newEntity();
36+
$obj = $this->fillModel($obj, $data);
37+
$savedResult = $this->connection->save($obj);
38+
$data->success($savedResult->get($this->config->id["db_name"]));
39+
}
40+
public function delete($data, $source) {
41+
$table = TableRegistry::get($source->get_source());
42+
$obj = $table->get($data->get_id());
43+
$this->connection->delete($obj);
44+
}
45+
public function update($data, $source) {
46+
$table = TableRegistry::get($source->get_source());
47+
$obj = $table->get($data->get_id());
48+
$obj = $this->fillModel($obj, $data);
49+
$table->save($obj);
50+
}
51+
private function fillModel($obj, $data) {
52+
//Map data to model object.
53+
for($i = 0; $i < count($this->config->text); $i++) {
54+
$step=$this->config->text[$i];
55+
$obj->set($step["name"], $data->get_value($step["name"]));
56+
}
57+
if($relation = $this->config->relation_id["db_name"])
58+
$obj->set($relation, $data->get_value($relation));
59+
return $obj;
60+
}
61+
public function escape($str){
62+
throw new Exception("Not implemented");
63+
}
64+
public function query($str){
65+
throw new Exception("Not implemented");
66+
}
67+
public function get_new_id(){
68+
throw new Exception("Not implemented");
69+
}
8370
}
8471

8572
?>

codebase/db_phpcake2.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/*
3+
@author dhtmlx.com
4+
@license GPL, see license.txt
5+
*/
6+
require_once("db_common.php");
7+
8+
//DataProcessor::$action_param ="dhx_editor_status";
9+
10+
/*! Implementation of DataWrapper for PDO
11+
12+
if you plan to use it for Oracle - use Oracle connection type instead
13+
**/
14+
class PHPCake2DBDataWrapper extends ArrayDBDataWrapper{
15+
public function select($sql){
16+
$source = $sql->get_source();
17+
if (is_array($source)) //result of find
18+
$res = $source;
19+
else
20+
$res = $this->connection->find("all");
21+
22+
$temp = array();
23+
if (sizeof($res)){
24+
$name = get_class($this->connection);
25+
for ($i=sizeof($res)-1; $i>=0; $i--)
26+
$temp[]=&$res[$i][$name];
27+
}
28+
return new ArrayQueryWrapper($temp);
29+
}
30+
31+
protected function getErrorMessage(){
32+
$errors = $this->connection->invalidFields();
33+
$text = array();
34+
foreach ($errors as $key => $value){
35+
$text[] = $key." - ".$value[0];
36+
}
37+
return implode("\n", $text);
38+
}
39+
40+
public function insert($data,$source){
41+
$name = get_class($this->connection);
42+
$save = array();
43+
$temp_data = $data->get_data();
44+
unset($temp_data[$this->config->id['db_name']]);
45+
unset($temp_data["!nativeeditor_status"]);
46+
$save[$name] = $temp_data;
47+
48+
if ($this->connection->save($save)){
49+
$data->success($this->connection->getLastInsertID());
50+
} else {
51+
$data->set_response_attribute("details", $this->getErrorMessage());
52+
$data->invalid();
53+
}
54+
}
55+
public function delete($data,$source){
56+
$id = $data->get_id();
57+
$this->connection->delete($id);
58+
$data->success();
59+
}
60+
public function update($data,$source){
61+
$name = get_class($this->connection);
62+
$save = array();
63+
$save[$name] = &$data->get_data();
64+
65+
if ($this->connection->save($save)){
66+
$data->success();
67+
} else {
68+
$data->set_response_attribute("details", $this->getErrorMessage());
69+
$data->invalid();
70+
}
71+
}
72+
73+
74+
public function escape($str){
75+
throw new Exception("Not implemented");
76+
}
77+
public function query($str){
78+
throw new Exception("Not implemented");
79+
}
80+
public function get_new_id(){
81+
throw new Exception("Not implemented");
82+
}
83+
}
84+
85+
?>

codebase/db_phpyii.php

Lines changed: 74 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -6,86 +6,80 @@
66

77
require_once("db_common.php");
88

9-
class PHPYiiDBDataWrapper extends ArrayDBDataWrapper{
10-
public function select($sql){
11-
if (is_array($this->connection)) //result of findAll
12-
$res = $this->connection;
13-
else
14-
$res = $this->connection->findAll();
15-
16-
$temp = array();
17-
if (sizeof($res)){
18-
foreach ($res as $obj)
19-
$temp[]=$obj->getAttributes();
20-
}
21-
return new ArrayQueryWrapper($temp);
22-
}
23-
24-
protected function getErrorMessage(){
25-
$errors = $this->connection->invalidFields();
26-
$text = array();
27-
foreach ($errors as $key => $value){
28-
$text[] = $key." - ".$value[0];
29-
}
30-
return implode("\n", $text);
31-
}
32-
public function insert($data,$source){
33-
$name = get_class($this->connection);
34-
$obj = new $name();
35-
36-
$this->fill_model_and_save($obj, $data);
37-
}
38-
public function delete($data,$source){
39-
$obj = $this->connection->findByPk($data->get_id());
40-
if ($obj->delete()){
41-
$data->success();
42-
$data->set_new_id($obj->getPrimaryKey());
43-
} else {
44-
$data->set_response_attribute("details", $this->errors_to_string($obj->getErrors()));
45-
$data->invalid();
46-
}
47-
}
48-
public function update($data,$source){
49-
$obj = $this->connection->findByPk($data->get_id());
50-
$this->fill_model_and_save($obj, $data);
51-
}
52-
53-
protected function fill_model_and_save($obj, $data){
54-
$values = $data->get_data();
55-
56-
//map data to model object
57-
for ($i=0; $i < sizeof($this->config->text); $i++){
58-
$step=$this->config->text[$i];
59-
$obj->setAttribute($step["name"], $data->get_value($step["name"]));
60-
}
61-
if ($relation = $this->config->relation_id["db_name"])
62-
$obj->setAttribute($relation, $data->get_value($relation));
63-
64-
//save model
65-
if ($obj->save()){
66-
$data->success();
67-
$data->set_new_id($obj->getPrimaryKey());
68-
} else {
69-
$data->set_response_attribute("details", $this->errors_to_string($obj->getErrors()));
70-
$data->invalid();
71-
}
72-
}
73-
74-
protected function errors_to_string($errors){
75-
$text = array();
76-
foreach($errors as $value)
77-
$text[]=implode("\n", $value);
78-
return implode("\n",$text);
79-
}
80-
public function escape($str){
81-
throw new Exception("Not implemented");
82-
}
83-
public function query($str){
84-
throw new Exception("Not implemented");
85-
}
86-
public function get_new_id(){
87-
throw new Exception("Not implemented");
88-
}
9+
class PHPYiiDBDataWrapper extends ArrayDBDataWrapper {
10+
11+
public function select($sql) {
12+
if(is_array($this->connection)) //result of findAll
13+
$res = $this->connection;
14+
else
15+
$res = $this->connection->find()->all();
16+
$temp = array();
17+
if(sizeof($res)) {
18+
foreach($res as $obj)
19+
$temp[] = $obj->getAttributes();
20+
}
21+
return new ArrayQueryWrapper($temp);
22+
}
23+
protected function getErrorMessage() {
24+
$errors = $this->connection->getErrors();
25+
$text = array();
26+
foreach($errors as $key => $value)
27+
$text[] = $key." - ".$value[0];
28+
return implode("\n", $text);
29+
}
30+
public function insert($data, $source) {
31+
$name = get_class($this->connection);
32+
$obj = new $name();
33+
$this->fill_model_and_save($obj, $data);
34+
}
35+
public function delete($data, $source) {
36+
$obj = $this->connection->findOne($data->get_id());
37+
if($obj->delete()) {
38+
$data->success();
39+
$data->set_new_id($obj->getPrimaryKey());
40+
}
41+
else {
42+
$data->set_response_attribute("details", $this->errors_to_string($obj->getErrors()));
43+
$data->invalid();
44+
}
45+
}
46+
public function update($data, $source) {
47+
$obj = $this->connection->findOne($data->get_id());
48+
$this->fill_model_and_save($obj, $data);
49+
}
50+
protected function fill_model_and_save($obj, $data) {
51+
//Map data to model object.
52+
for($i=0; $i < sizeof($this->config->text); $i++) {
53+
$step=$this->config->text[$i];
54+
$obj->setAttribute($step["name"], $data->get_value($step["name"]));
55+
}
56+
if($relation = $this->config->relation_id["db_name"])
57+
$obj->setAttribute($relation, $data->get_value($relation));
58+
//Save model.
59+
if($obj->save()) {
60+
$data->success();
61+
$data->set_new_id($obj->getPrimaryKey());
62+
}
63+
else {
64+
$data->set_response_attribute("details", $this->errors_to_string($obj->getErrors()));
65+
$data->invalid();
66+
}
67+
}
68+
protected function errors_to_string($errors) {
69+
$text = array();
70+
foreach($errors as $value)
71+
$text[] = implode("\n", $value);
72+
return implode("\n",$text);
73+
}
74+
public function escape($str) {
75+
throw new Exception("Not implemented");
76+
}
77+
public function query($str) {
78+
throw new Exception("Not implemented");
79+
}
80+
public function get_new_id() {
81+
throw new Exception("Not implemented");
82+
}
8983
}
9084

9185
?>

0 commit comments

Comments
 (0)