public function ensureEntityExists($name) { if (StringTool::start_with($name, '[new]')) { $name = StringTool::remove_leading($name, '[new]'); } $search = ['name' => $name]; return $this->findOrCreate($search); }
/** * Copy method * * @param string|null $id Application id. * @return void Redirects on successful copy, renders view otherwise. * @throws \Cake\Network\Exception\NotFoundException When record not found. */ public function copy($id = null) { $application = $this->Applications->get($id, ['contain' => ['Frameworks', 'Technologies']]); if ($this->request->is(['patch', 'post', 'put'])) { /* * Save eventual new technologies or frameworks */ foreach ($this->request->data['frameworks']['_ids'] as $index => $framework_id) { if (StringTool::start_with($framework_id, '[new]')) { $framework = $this->Applications->Frameworks->newEntity(['name' => StringTool::remove_leading($framework_id, '[new]')]); if ($this->Applications->Frameworks->save($framework)) { $this->request->data['frameworks']['_ids'][$index] = $framework->id; } } } foreach ($this->request->data['technologies']['_ids'] as $index => $technology_id) { if (StringTool::start_with($technology_id, '[new]')) { $technology = $this->Applications->Technologies->newEntity(['name' => StringTool::remove_leading($technology_id, '[new]')]); if ($this->Applications->Technologies->save($technology)) { $this->request->data['technologies']['_ids'][$index] = $technology->id; } } } $application = $this->Applications->newEntity(); $application = $this->Applications->patchEntity($application, $this->request->data); if ($this->Applications->save($application)) { $this->Flash->success(___('the application has been saved'), ['plugin' => 'Alaxos']); return $this->redirect(['action' => 'index']); } else { $this->Flash->error(___('the application could not be saved. Please, try again.'), ['plugin' => 'Alaxos']); } } $frameworks = $this->Applications->Frameworks->find('list', ['limit' => 200]); $technologies = $this->Applications->Technologies->find('list', ['limit' => 200]); $application->id = $id; $this->set(compact('application', 'frameworks', 'technologies')); $this->set('_serialize', ['application']); }