This repository has been archived by the owner on May 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
BaseModel.php
139 lines (117 loc) · 2.94 KB
/
BaseModel.php
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
<?php
/**
* Copyright (C) 2011 by Igor Hlina ([email protected])
*/
namespace Diggriola;
/**
* Service for Model loading & database connection
*
* @package Diggriola
* @author Igor Hlina <[email protected]>
* @license license.txt MIT
* @version 1.0
* @link https://github.com/srigi/Diggriola
* @link http://wiki.nette.org/cs/cookbook/jednoduchy-model-s-notorm
*
*/
class BaseModel extends \Nette\Object
{
/**
* @var NotORM
*/
protected $connection = null;
/**
* @var string
*/
protected $tableName = '';
/**
* @param NotORM $connection
*/
public function __construct($connection)
{
$this->connection = $connection;
$classNameParts = explode('\\', get_class($this));
$this->tableName = strtolower(array_pop($classNameParts)) . 's';
}
/**
* Finds all records in table. Use fluent to limit result set.
*
* @return NotORM_Result
*/
public function findAll()
{
return $this->connection->{$this->tableName}();
}
/**
* Magic findBy* method
*
* @param string $name
* @param array $args
* @return NotORM_Result
*/
public function __call($name, $args)
{
if (false !== strpos($name, 'findBy')) {
$cammelCaseSplit = preg_split('~(?<=\\w)(?=[A-Z])~', str_replace('findBy', '', $name));
$loweredCammels = array_map(function($in) {
return strtolower($in);
}, $cammelCaseSplit);
$findCondition = implode('.', $loweredCammels);
if (isset($args[1]) && true === $args[1]) {
// M:N relation
$relationTableName = $loweredCammels[0] . 's_' . $this->tableName;
$mn = $this->connection->{$relationTableName}($findCondition, $args[0])
->select(substr($this->tableName, 0, -1) . '_id');
try {
$result = $this->connection->{$this->tableName}('id', $mn);
} catch (\PDOException $e) {
if (false !== strpos($e->getMessage(), 'Table') && false !== strpos($e->getMessage(), 'doesn\'t exist')) {
// switch table name elements
$relationTableName = $this->tableName . '_' . $loweredCammels[0] . 's';
$mn = $this->connection->{$relationTableName}($findCondition, $args[0])
->select(substr($this->tableName, 0, -1) . '_id');
$result = $this->connection->{$this->tableName}('id', $mn);
} else {
throw $e;
}
}
return $result;
} else {
// no or 1:N relation
return $this->connection->{$this->tableName}()
->where($findCondition, $args[0]);
}
}
}
/**
* Insert new record to table
*
* @param array $data
* @return NotORM_Row
*/
public function insert(array $data)
{
return $this->connection->{$this->tableName}()->insert($data);
}
/**
* Update rowset
*
* @param NotORM_Result $rowset
* @param array $data
* @return integer Number of affected rows
*/
public function update($rowset, $data)
{
return $rowset->update($data);
}
/**
* Delete rowset
*
* @param NotORM_Result $rowset
* @return integer Number of affected rows
*/
public function delete($rowset)
{
return $rowset->delete();
}
}