1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace puck; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @property totalCount |
8
|
|
|
*/ |
9
|
|
|
class Model |
10
|
|
|
{ |
11
|
|
|
protected $db; |
12
|
|
|
protected $field; |
13
|
|
|
protected $table; |
14
|
|
|
protected $dbConn; |
15
|
|
|
protected $limitCount=null; |
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
if(!$this->dbConn){ |
19
|
|
|
$this->dbConn='main'; |
20
|
|
|
} |
21
|
|
|
$this->db = Mysql::getDb($this->dbConn); |
22
|
|
|
} |
23
|
|
|
public function __call($method,$arg){ |
24
|
|
|
$ret=$this; |
25
|
|
|
if(method_exists ($this->db, $method)){ |
26
|
|
|
$ret=call_user_func_array(array($this->db,$method),$arg); |
27
|
|
|
} |
28
|
|
|
return $ret==$this->db? $this: $ret; |
29
|
|
|
} |
30
|
|
|
public function __get($name){ |
31
|
|
|
if(property_exists($this->db, $name)){ |
32
|
|
|
return $this->db->$name; |
33
|
|
|
} |
34
|
|
|
throw new MemberAccessException('model Property ' . $name . ' not exists'); |
35
|
|
|
} |
36
|
|
|
public function limit($limit){ |
37
|
|
|
$this->limitCount=$limit; |
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
public function find(){ |
41
|
|
|
return $this->db->getOne($this->table,$this->field); |
42
|
|
|
} |
43
|
|
|
public function select(){ |
44
|
|
|
return $this->db->get($this->table,$this->limitCount?$this->limitCount:null,$this->field); |
45
|
|
|
} |
46
|
|
|
public function count(){ |
47
|
|
|
return $this->field("count(*) as count")->find()['count']; |
48
|
|
|
} |
49
|
|
|
public function table($table){ |
50
|
|
|
$this->table=$table; |
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
public function add($data){ |
54
|
|
|
return $this->db->insert($this->table,$data); |
55
|
|
|
} |
56
|
|
|
public function replace($data){ |
57
|
|
|
return $this->db->replace($this->table,$data); |
58
|
|
|
} |
59
|
|
|
public function update($data){ |
60
|
|
|
return $this->db->update($this->table,$data); |
61
|
|
|
} |
62
|
|
|
public function field($field){ |
63
|
|
|
$this->field=$field; |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
public function delete(){ |
67
|
|
|
return $this->db->delete($this->table); |
68
|
|
|
} |
69
|
|
|
public function page($page,$pageLimit='10'){ |
70
|
|
|
$this->db->pageLimit=$pageLimit; |
|
|
|
|
71
|
|
|
$info= $this->db->paginate($this->table,$page,$this->field); |
72
|
|
|
return $info; |
73
|
|
|
} |
74
|
|
|
} |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.