66
77require_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