dao.txt

Gesponsorde koppelingen

PHP script bestanden

  1. dao.txt
  2. queryBuilder.txt
  3. db.txt
  4. voorbeeldtabel.txt

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
 * @package Simple MVC
 * @version 1.0
 *
 * Data Acces Object for PHP.
 * Provides simple crud actions
 * - Create
 * - Read
 * - Update
 * - Delete()
 *
 * @todo
 * Implement support for multiple primary keys
 * Implement support for updating individual fields
 * Implement validation before creation
 *
 *  */


abstract class dao {
    
    const WHERE_IN = 2;
    const WHERE_NOT = 4;
    
    const ORDER_DESC = 8;
    const ORDER_ASC = 16;
    
    public $arAssoc = array ();
    
    
    public static $arTableFields = array(                    
    );

    
    /**
     * Returns an array of objects
     *
     * @param $filter array           
     * @param $filterWay array           
     * @param $orderField array           
     */

    public static function getArObj($filter = array(), $filterWay = array(), $orderField = array(), $limit = array()) {
        $oObj = new static ();
        $query = queryBuilder::buildSelect ( $db, $oObj->table, $filter, $filterWay, $orderField , $limit);
        $res = $db->query ( $query )->allAssoc ();
        return $this->createObjects($res);
    }

    
    /**
      * Creates a new objects and returns
       * the created one
     */

    public function create(){
        $query = queryBuilder::buildInsert($db, $this);
        $db->query($query);                
        $query2 = queryBuilder::simpleSelect($this->table, $this->primaryKey, $db->insert_id);
        $res = $db->query($query2)->allAssoc();
        $this->fillObject($res);        
    }

    /**
     * Updates an object
     */

    public function update(){
        $query = queryBuilder::buildUpdate($db, $this);
        $db->query($query);
        $query2 = queryBuilder::simpleSelect($this->table, $this->primaryKey, $db->insert_id);
        $res = $db->query($query2)->allAssoc();
        $this->fillObject($res);
    }

    /**
     * Deletes an object
     */

    public function delete(){
        $query = queryBuilder::buildDelete($db, $this);
        $db->query($query);
    }

    /**
     * Creates an array of objects
     * @param array $res
     * @return array
     */

    private function createObjects($res = array()){
        $arObj = array();
        foreach($res as $row){
            $oObj = new static ();
            foreach($row as $key=>$value)
                $oObj->arAssoc [$key] = $value;
        
            $arObj [] = $oObj;
        }

        
        return $arObj;        
    }

    private function fillObject($res = array()){

        foreach($res as $row){
                    foreach($row as $key=>$value)
                        $this->arAssoc [$key] = $value;
                    
                    $this->arAssoc ['saved'] = true;
                }
    
    
    }

    
    public function __get($name) {
        
        switch ($name){
            case
'saved':
                return empty($this->arAssoc['saved']) ? false : true;
            default :

                return $this->arAssoc [$name];
        }
    }

    
    public function __construct($primaryKey = null) {
        if(! is_null ( $primaryKey )){        
            
            $query = queryBuilder::buildSelect ($db, $this->table, array (    $this->primaryKey => $primaryKey ) );
            $res = $db->query ( $query )->allAssoc ();
            if(! empty ( $res )){
                foreach($res as $row){
                    foreach($row as $key=>$value)
                        $this->arAssoc [$key] = $value;                    
                    $this->arAssoc ['saved'] = true;
                }
            }
else {
                $this->arAssoc['saved'] = false;
            }
        }

        
        $this->getTableFields();

        
    }

    
    private function getTableFields(){
        if(empty(self::$arTableFields)){
            $db = db::get();
            $res = $db->query("SHOW FIELDS FROM ".$this->table)->allAssoc();
            if(!empty($res)){
                foreach($res as $row){
                    self::$arTableFields [$row['Field']] = $row['Field'];
                }
            }
        }
        
    }

    
    public function __set($name, $val) {
        $this->arAssoc [$name] = $val;
    }
    

}

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.