Skip to content

Commit

Permalink
Merge pull request #10 from CakeDC/feature/fix-on-beforeMarshall
Browse files Browse the repository at this point in the history
Feature/fix on before marshall
  • Loading branch information
steinkel authored Sep 10, 2024
2 parents 09c260f + 911d4dc commit ad1cf5d
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 217 deletions.
1 change: 1 addition & 0 deletions config/sql/example(postgresql).psql
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ CREATE TABLE public.pages (
modified timestamp NOT NULL,
category_id int4 NULL,
published_date date NULL,
active bool DEFAULT false NULL,
CONSTRAINT pages_pkey PRIMARY KEY (id)
);

Expand Down
2 changes: 1 addition & 1 deletion templates/bake/Model/table.twig
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class {{ name }}Table extends Table{{ fileBuilder.classBuilder.implements ? ' im
public function beforeMarshal(EventInterface $event, ArrayObject $data, ArrayObject $options): void
{
foreach ($this->getSchema()->columns() as $column) {
if ($this->getSchema()->getColumnType($column) === 'timestampfractional' && $data[$column] !== null) {
if (isset($data[$column]) && $this->getSchema()->getColumnType($column) === 'timestampfractional' && $data[$column] !== null) {
$data[$column] = Date::parseDate($data[$column], 'YYYY-MM-dd');
}
}
Expand Down
6 changes: 5 additions & 1 deletion templates/bake/Template/view.twig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ function formatDate(date) {
return moment(date).format("YYYY-MM-DD")
}
function confirm(){
return window.confirm("Are you sure?");
}
onMounted(() => {
console.log('Component categories/add onMounted hook called')
})
Expand Down Expand Up @@ -132,7 +136,7 @@ onMounted(() => {
<td class="actions align-middle">
<Link class="btn btn-sm btn-outline-info ms-1" as="button" :href="'{{ baseUrl }}{{ otherSingularVar }}/view/' + {{ otherPk }}">View</Link>
<Link class="btn btn-sm btn-outline-warning ms-1" as="button" :href="'{{ baseUrl }}{{ otherSingularVar }}/edit/' + {{ otherPk }}">Edit</Link>
<Link class="btn btn-sm btn-outline-danger ms-1" as="button" method="post" :href="'{{ baseUrl }}{{ otherSingularVar }}/delete/' + {{ otherPk }}">Delete</Link>
<Link class="btn btn-sm btn-outline-danger ms-1" as="button" method="delete" :headers="{'X-CSRF-Token': props.csrfToken}" :onBefore="() => confirm()" :href="'{{ baseUrl }}{{ otherSingularVar }}/delete/' + {{ otherPk }}">Delete</Link>
</td>
</tr>
</tbody>
Expand Down
66 changes: 32 additions & 34 deletions templates/bake/element/Controller/edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,50 @@
{% set belongsTo = Bake.aliasExtractor(modelObj, 'BelongsTo') %}
{% set belongsToMany = Bake.aliasExtractor(modelObj, 'belongsToMany') %}
{% set compact = ["'#{singularName}'"] %}
/**
* Edit method VUE
*
* @param string|null $id {{ singularHumanName }} id.
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function edit($id = null)
{
$errors = [];
${{ singularName }} = $this->{{ currentModelName }}->get($id, [
'contain' => {{ Bake.exportArray(belongsToMany)|raw }},
]);
if ($this->request->is(['patch', 'post', 'put'])) {
${{ singularName }} = $this->{{ currentModelName }}->patchEntity(${{ singularName }}, $this->getRequest()->getData());
if ($this->{{ currentModelName }}->save(${{ singularName }})) {
$this->Flash->set(
__('The {{ singularHumanName|lower }} has been saved.'),
['element' => 'success', 'key' => 'alert']
);
/**
* Edit method VUE
*
* @param string|null $id {{ singularHumanName }} id.
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function edit($id = null)
{
$errors = [];
${{ singularName }} = $this->{{ currentModelName }}->get($id, contain: {{ Bake.exportArray(belongsToMany)|raw }});
if ($this->request->is(['patch', 'post', 'put'])) {
${{ singularName }} = $this->{{ currentModelName }}->patchEntity(${{ singularName }}, $this->getRequest()->getData());
if ($this->{{ currentModelName }}->save(${{ singularName }})) {
$this->Flash->set(
__('The {{ singularHumanName|lower }} has been saved.'),
['element' => 'success', 'key' => 'alert']
);

return $this->redirect(['action' => 'index']);
}
$errors = ${{ singularName }}->getErrors();
$this->Flash->set(
__('The {{ singularHumanName|lower }} could not be saved. Please, try again.'),
['element' => 'danger', 'key' => 'alert']
);
}
return $this->redirect(['action' => 'index']);
}
$errors = ${{ singularName }}->getErrors();
$this->Flash->set(
__('The {{ singularHumanName|lower }} could not be saved. Please, try again.'),
['element' => 'danger', 'key' => 'alert']
);
}
{% set associations = Bake.aliasExtractor(modelObj, 'BelongsTo') %}
{%- for assoc in associations %}
{%- set otherName = Bake.getAssociatedTableAlias(modelObj, assoc) %}
{%- set otherPlural = otherName|variable %}
${{ otherPlural }} = $this->{{ currentModelName }}->{{ otherName }}->find('list', ['limit' => 200])->toArray();
${{ otherPlural }} = $this->{{ currentModelName }}->{{ otherName }}->find('list', ['limit' => 200])->toArray();
{{- "\n" }}
{%- set compact = compact|merge(["'#{otherPlural}'"]) %}
{% endfor %}
{% set associations = Bake.aliasExtractor(modelObj, 'BelongsToMany') %}
{%- for assoc in associations %}
{%- set otherName = Bake.getAssociatedTableAlias(modelObj, assoc) %}
{%- set otherPlural = otherName|variable %}
$options_{{ otherPlural }} = $this->{{ currentModelName }}->{{ otherName }}->find('all', ['limit' => 200])->disableHydration()->toArray();
$options_{{ otherPlural }} = $this->{{ currentModelName }}->{{ otherName }}->find('all', ['limit' => 200])->disableHydration()->toArray();
{{- "\n" }}
{%- set optionsOtherPlural = 'options_' ~ otherPlural %}
{%- set compact = compact|merge(["'#{optionsOtherPlural}'"]) %}
{%- set optionsOtherPlural = 'options_' ~ otherPlural %}
{%- set compact = compact|merge(["'#{optionsOtherPlural}'"]) %}
{% endfor %}
{%- set compact = compact|merge(["'errors'"]) %}
$this->set(compact({{ compact|join(', ')|raw }}));
}
$this->set(compact({{ compact|join(', ')|raw }}));
}
Loading

0 comments on commit ad1cf5d

Please sign in to comment.