PHP code example of zeptech / code-templates
1. Go to this page and download the library: Download zeptech/code-templates library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
zeptech / code-templates example snippets
class /*# actor #*/ {
// ...
public function create(\/*# class #*/ $model) {
if (!$this->validator->validate($model, $e)) {
throw $e;
}
if ($model->get/*# id_property */() !== null) {
return $model->get/*# id_property */();
}
#{ if beforeCreate
$model->beforeCreate();
#}
try {
$startTransaction = $this->_pdo->beginTransaction();
$model->set/*# id_property */(self::CREATE_MARKER);
$params = Array();
#{ each properties as prop
#{ if prop[type] = boolean
$params['/*# prop[col] #*/'] = $model->get/*# prop[name] #*/() ? 1 : 0;
#{ else
$params['/*# prop[col] #*/'] = $model->get/*# prop[name] #*/();
#}
#}
#{ each relationships as rel
#{ if rel[type] = many-to-one
// Populate /*# rel[rhs] #*/ parameter --------------------------------
// ...
// -------------------------------------------------------------------
#}
#}
$this->_create->execute($params);
$id = $this->transformer->idFromDb($this->_pdo->lastInsertId());
$model->set/*# id_property */($id);
$this->_cache[$id] = $model;
#{ each collections as col
$this->insertCollection_/*# col[property] #*/(
$id,
$model->get/*# col[property] #*/()
);
#}
#{ each relationships as rel
$related = $model->get/*# rel[lhsProperty] #*/();
#{ if rel[type] = many-to-many
// ...
#{ elseif rel[type] = one-to-many
// ...
#}
#}
if ($startTransaction) {
$this->_pdo->commit();
}
#{ if onCreate
$model->onCreate();
#}
return $id;
} catch (PDOException $e) {
$this->_pdo->rollback();
$model->set/*# id_property */(null);
$e = new PdoExceptionWrapper($e, '/*# class #*/');
$e->setSql($sql, $params);
throw $e;
}
}
// ...
}