4 * Create DAO ORM classes.
6 class CRM_Core_CodeGen_DAO
extends CRM_Core_CodeGen_BaseTask
{
16 private $tableChecksum;
25 * translate function name
27 private $tsFunctionName;
30 * CRM_Core_CodeGen_DAO constructor.
32 * @param \CRM_Core_CodeGen_Main $config
34 * @param string $tsFunctionName
36 public function __construct($config, $name, $tsFunctionName = 'ts') {
37 parent
::__construct($config);
39 $this->tsFunctionName
= $tsFunctionName;
44 * TRUE if an update is needed.
46 public function needsUpdate() {
47 if (!file_exists($this->getAbsFileName())) {
51 if ($this->getTableChecksum() !== self
::extractRegex($this->getAbsFileName(), ';\(GenCodeChecksum:([a-zA-Z0-9]+)\);')) {
55 return !$this->isApproxPhpMatch(
56 file_get_contents($this->getAbsFileName()),
63 public function run() {
64 echo "Generating {$this->name} as " . $this->getRelFileName() . "\n";
66 if (empty($this->tables
[$this->name
]['base'])) {
67 echo "No base defined for {$this->name}, skipping output generation\n";
71 $template = new CRM_Core_CodeGen_Util_Template('php');
72 $template->assign('table', $this->tables
[$this->name
]);
73 if (empty($this->tables
[$this->name
]['index'])) {
74 $template->assign('indicesPhp', var_export([], 1));
77 $template->assign('indicesPhp', var_export($this->tables
[$this->name
]['index'], 1));
79 $template->assign('genCodeChecksum', $this->getTableChecksum());
80 $template->assign('tsFunctionName', $this->tsFunctionName
);
81 $template->run('dao.tpl', $this->getAbsFileName());
85 * Generate the raw PHP code for the DAO.
89 public function getRaw() {
91 $template = new CRM_Core_CodeGen_Util_Template('php');
92 $template->assign('table', $this->tables
[$this->name
]);
93 if (empty($this->tables
[$this->name
]['index'])) {
94 $template->assign('indicesPhp', var_export([], 1));
97 $template->assign('indicesPhp', var_export($this->tables
[$this->name
]['index'], 1));
99 $template->assign('genCodeChecksum', 'NEW');
100 $template->assign('tsFunctionName', $this->tsFunctionName
);
101 $this->raw
= $template->fetch('dao.tpl');
107 * Get relative file name.
111 public function getRelFileName() {
112 return $this->tables
[$this->name
]['fileName'];
116 * Get the absolute file name.
120 public function getAbsFileName() {
121 $directory = $this->config
->phpCodePath
. $this->tables
[$this->name
]['base'];
122 CRM_Core_CodeGen_Util_File
::createDir($directory);
123 $absFileName = $directory . $this->getRelFileName();
128 * Get a unique signature for the table/schema.
132 protected function getTableChecksum() {
133 if (!$this->tableChecksum
) {
135 CRM_Utils_Array
::flatten($this->tables
[$this->name
], $flat);
137 $this->tableChecksum
= md5(json_encode($flat));
139 return $this->tableChecksum
;