1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* @title Model Class |
4
|
|
|
* |
5
|
|
|
* @author Pierre-Henry Soria <[email protected]> |
6
|
|
|
* @copyright (c) 2012-2017, Pierre-Henry Soria. All Rights Reserved. |
7
|
|
|
* @license GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory. |
8
|
|
|
* @package PH7 / Framework / Mvc / Model / Engine |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace PH7\Framework\Mvc\Model\Engine; |
12
|
|
|
|
13
|
|
|
defined('PH7') or exit('Restricted access'); |
14
|
|
|
|
15
|
|
|
use PH7\Framework\Cache\Cache; |
16
|
|
|
use PH7\Framework\File\File; |
17
|
|
|
|
18
|
|
|
abstract class Model extends Entity |
19
|
|
|
{ |
20
|
|
|
const SQL_FILE_EXT = '.sql'; |
21
|
|
|
|
22
|
|
|
protected $orm, $cache; |
|
|
|
|
23
|
|
|
private $_sContents; |
24
|
|
|
|
25
|
|
|
public function __construct() |
26
|
|
|
{ |
27
|
|
|
$this->orm = Record::getInstance(); |
28
|
|
|
$this->cache = new Cache; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $sFile SQL file name. |
33
|
|
|
* @param string $sPath Path to SQL file. |
34
|
|
|
* @param array $aParams |
35
|
|
|
* |
36
|
|
|
* @return boolean Returns TRUE on success or FALSE on failure. |
37
|
|
|
*/ |
38
|
|
|
public function exec($sFile, $sPath, array $aParams = null) |
39
|
|
|
{ |
40
|
|
|
$rStmt = Db::getInstance()->prepare( $this->getQuery($sFile, $sPath) ); |
41
|
|
|
$bRet = $rStmt->execute($aParams); |
42
|
|
|
Db::free($rStmt); |
43
|
|
|
return $bRet; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get SQL query file. |
48
|
|
|
* |
49
|
|
|
* @param string $sFile SQL file name. |
50
|
|
|
* @param string $sPath Path to SQL file. |
51
|
|
|
* |
52
|
|
|
* @return string The SQL query. |
53
|
|
|
*/ |
54
|
|
|
public function getQuery($sFile, $sPath) |
55
|
|
|
{ |
56
|
|
|
$sFullPath = $sPath . $sFile . static::SQL_FILE_EXT; |
57
|
|
|
$this->_sContents = (new File)->getFile($sFullPath); |
58
|
|
|
$this->_parseVar(); |
59
|
|
|
|
60
|
|
|
return $this->_sContents; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Parse query variables. |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
private function _parseVar() |
69
|
|
|
{ |
70
|
|
|
$this->_sContents = str_replace('[DB_PREFIX]', Db::prefix(), $this->_sContents); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.