4 * Create DAO ORM classes.
6 class CRM_Core_CodeGen_DAO
extends CRM_Core_CodeGen_BaseTask
{
16 private $tableChecksum;
24 * CRM_Core_CodeGen_DAO constructor.
26 * @param \CRM_Core_CodeGen_Main $config
29 public function __construct($config, $name) {
30 parent
::__construct($config);
36 * TRUE if an update is needed.
38 public function needsUpdate() {
39 if (!file_exists($this->getAbsFileName())) {
43 if ($this->getTableChecksum() !== self
::extractRegex($this->getAbsFileName(), ';\(GenCodeChecksum:([a-zA-Z0-9]+)\);')) {
47 return !$this->isApproxPhpMatch(
48 file_get_contents($this->getAbsFileName()),
55 public function run() {
56 echo "Generating {$this->name} as " . $this->getRelFileName() . "\n";
58 if (empty($this->tables
[$this->name
]['base'])) {
59 echo "No base defined for {$this->name}, skipping output generation\n";
63 $template = new CRM_Core_CodeGen_Util_Template('php');
64 $template->assign('table', $this->tables
[$this->name
]);
65 if (empty($this->tables
[$this->name
]['index'])) {
66 $template->assign('indicesPhp', var_export(array(), 1));
69 $template->assign('indicesPhp', var_export($this->tables
[$this->name
]['index'], 1));
71 $template->assign('genCodeChecksum', $this->getTableChecksum());
72 $template->run('dao.tpl', $this->getAbsFileName());
76 * Generate the raw PHP code for the DAO.
80 public function getRaw() {
82 $template = new CRM_Core_CodeGen_Util_Template('php');
83 $template->assign('table', $this->tables
[$this->name
]);
84 if (empty($this->tables
[$this->name
]['index'])) {
85 $template->assign('indicesPhp', var_export(array(), 1));
88 $template->assign('indicesPhp', var_export($this->tables
[$this->name
]['index'], 1));
90 $template->assign('genCodeChecksum', 'NEW');
91 $this->raw
= $template->fetch('dao.tpl');
97 * Get relative file name.
101 public function getRelFileName() {
102 return $this->tables
[$this->name
]['fileName'];
106 * Get the absolute file name.
110 public function getAbsFileName() {
111 $directory = $this->config
->phpCodePath
. $this->tables
[$this->name
]['base'];
112 CRM_Core_CodeGen_Util_File
::createDir($directory);
113 $absFileName = $directory . $this->getRelFileName();
118 * Get a unique signature for the table/schema.
122 protected function getTableChecksum() {
123 if (!$this->tableChecksum
) {
125 CRM_Utils_Array
::flatten($this->tables
[$this->name
], $flat);
127 $this->tableChecksum
= md5(json_encode($flat));
129 return $this->tableChecksum
;