summaryrefslogtreecommitdiffstats
path: root/codebase/connector/db_phpyii.php
blob: 1d8d5d1b315f85b962cf3e2a6ca2a77c16dcba54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/*
	@author dhtmlx.com
	@license GPL, see license.txt
*/

require_once("db_common.php");

class PHPYiiDBDataWrapper extends ArrayDBDataWrapper {

    public function select($source) {
        $sourceData = $source->get_source();
        if(is_array($sourceData))	//result of find
            $res = $sourceData;
        else
            $res = $sourceData->find()->all();

        $temp = array();
        if(sizeof($res)) {
            foreach($res as $obj)
                $temp[] = $obj->getAttributes();
        }
        return new ArrayQueryWrapper($temp);
    }

    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) {
        $name = get_class($source->get_source());
        $obj = new $name();
        $this->fill_model_and_save($obj, $data);
    }

    public function delete($data, $source) {
        $obj = $source->get_source()->findOne($data->get_id());
        if($obj->delete()) {
            $data->success();
            $data->set_new_id($obj->getPrimaryKey());
        }
        else {
            $data->set_response_attribute("details", $this->errors_to_string($obj->getErrors()));
            $data->invalid();
        }
    }

    public function update($data, $source) {
        $obj = $source->get_source()->findOne($data->get_id());
        $this->fill_model_and_save($obj, $data);
    }

    protected function fill_model_and_save($obj, $data) {
        //Map data to model object.
        for($i=0; $i < sizeof($this->config->text); $i++) {
            $step=$this->config->text[$i];
            $obj->setAttribute($step["name"], $data->get_value($step["name"]));
        }

        if($relation = $this->config->relation_id["db_name"])
            $obj->setAttribute($relation, $data->get_value($relation));

        //Save model.
        if($obj->save()) {
            $data->success();
            $data->set_new_id($obj->getPrimaryKey());
        }
        else {
            $data->set_response_attribute("details", $this->errors_to_string($obj->getErrors()));
            $data->invalid();
        }
    }

    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");
    }
}

?>