blob: 436ede1067f1163415eb6d3d21340aaf9476a4b0 (
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
|
<?php
namespace Psecio\Gatekeeper\Model;
class Mysql extends \Modler\Model
{
/**
* Data source instance
* @var \Psecio\Gatekeeper\DataSource
*/
private $db;
/**
* Init the object with the datasource and optional data
*
* @param \Psecio\Gatekeeper\DataSource $db Datasource instance
* @param array $data Optional data to populate in model
*/
public function __construct(\Psecio\Gatekeeper\DataSource $db, array $data = array())
{
$this->setDb($db);
parent::__construct($data);
}
/**
* Get the current data source instance
*
* @return \Psecio\Gatekeeper\DataSource instance
*/
public function getDb()
{
return $this->db;
}
/**
* Set the datasource instance
*
* @param \Psecio\Gatekeeper\DataSource $db Data source instance
*/
public function setDb(\Psecio\Gatekeeper\DataSource $db)
{
$this->db = $db;
}
/**
* Get the current model's table name
*
* @return string Table name
*/
public function getTableName()
{
$dbConfig = $this->db->config;
return (isset($dbConfig['prefix']))
? $dbConfig['prefix'].'_'.$this->tableName : $this->tableName;
}
/**
* Make a new model instance
*
* @param string $model Model namespace "path"
* @return object Model instance
*/
public function makeModelInstance($model)
{
$instance = new $model($this->getDb());
return $instance;
}
/**
* Load the given data into the current model
*
* @param array $data Property data
* @param boolean $enforceGuard Enforce guarded properties
* @return boolean True when complete
*/
public function load(array $data, $enforceGuard = true)
{
$loadData = array();
foreach ($this->getProperties() as $propertyName => $propertyDetail) {
// If it's a normal column
if (isset($propertyDetail['column'])) {
$column = $propertyDetail['column'];
if (isset($data[$column]) || isset($data[$propertyName])) {
$value = isset($data[$column]) ? $data[$column] : $data[$propertyName];
$loadData[$propertyName] = $value;
}
// Or for relations...
} elseif ($propertyDetail['type'] == 'relation') {
if (isset($data[$propertyName])) {
$loadData[$propertyName] = $data[$propertyName];
}
}
}
parent::load($loadData, $enforceGuard);
return true;
}
}
|