Issue
I'm trying to create a Dropdown menu from the table Holidays for the column name. But I'm too dumb to make it work.
I already tried some of the things people wrote but most of them have a second database. But I only have one.
My goal is to show all the names from the Holiday name column so, if I create a new holiday, I just get the name of the date from the Dropdown menu and don't need to write it down every time.
HolidaysController.php
public function index()
{
$holidays = $this->paginate($this->Holidays);
$this->set(compact('holidays'));
$name =$this->name->find('list');
$this->set(compact('name'));
}
Holidays add.ctp
<?php
echo $this->Form->control('date', array('label' => __('Datum', true)));
echo $this->Form->control('name', ['options' => $name, 'empty' => true]);
?>
I thought he would show me now a dropdown list from the names of the holidays but i just have a normal field and get the error:
Notice (8): Undefined variable: name [APP/Template\Holidays\add.ctp, line 19]
I'm probably missing something really basic here, I'm still learning coding so if I missed any information, you need, just let me know.
EDIT:
My add function after the edit
HolidaysController
public function add()
{
$name =$this->name->find('list');
$this->set(compact('name'));
$holiday = $this->Holidays->newEntity();
if ($this->request->is('post')) {
$holiday = $this->Holidays->patchEntity($holiday, $this->request->getData());
if ($this->Holidays->save($holiday)) {
$this->Flash->success(__('Der Feiertag wurde gespeichert.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Der Feiertag wurde nicht gespeichert. Bitte, versuchen sie es erneut.'));
}
$this->set(compact('holiday'));
}
Solution
You must find and set list in your add() method, like :
public function add()
{
// your add code here .. then:
$name =$this->Holidays->find('list');
$this->set(compact('name'));
}
By default every controller method has own view template:
index() >> index.ctp
add() >> add.ctp
edit() >> edit.ctp
myCustomMethod() >> my_custom_method.ctp
// ...
EDIT:
If I add your code to function add() i get an Error: Call to a member function find() on string.
I copy code from your question. But you must update:
from: $this->name->find('list');
to
$this->Holidays->find('list', [
'keyField' => 'name',
]);
Answered By - Salines
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.