Database filter with form
Notice: This thread is very old.
- hegerworld
- Member | 4
Hello,
I've been sitting around for a long time. My template has a form and a list of
objects. On the left are filters which I can click. Now the object list is to
be filtered. But it does not work. All are displayed.
Here is my source:
<?php
namespace App\Presenters;
use App\Model;
use Nette\Application\UI;
use Nette\Security\Passwords;
use Tracy\Debugger;
class FincasPresenter extends \App\Presenters\BasePresenter {
/** @var Model\ObjektRepository */
private $objekte;
public function __construct(Model\ObjektRepository $objekte) {
$this->objekte = $objekte;
}
public function renderDefault() {
$this->template->objekte = $objekte;
}
// called after form is successfully submitted
public function formSucceeded(UI\Form $form, $vals) {
dump($vals);
$this->template->objekte = $this->objekte->findAll()->where('anzahlPersonen >= ?', 7);
}
protected function createComponentFilterForm() {
$form = new UI\Form;
$form->getElementPrototype()->class('form-horizontal');
$form->addGroup('Größe');
$form->addCheckboxList('personen', '', [
'0' => 'Alle',
'1' => 'bis 3 Personen',
'2' => '4 bis 5 Personen',
]);
$form->addSubmit('suche', 'Suchen');
$form->onSuccess[] = [$this, 'formSucceeded'];
$form->setDefaults([
'personen' => '0']);
return $form;
}
}
Can anyone give me a hint?
THH!
- hegerworld
- Member | 4
CZechBoY wrote:
You should filter by query string (http method GET).
But in this case – you are replacing value in
renderDefault
, which is filled informSucceeded
before.
If I did not use renderdefault, will it work?
I do not know what I can do
- hegerworld
- Member | 4
Unfortunately, this does not work either. Now comes the error message: The variable ‘objects’ does not exist in template
20: /** @var Model\ObjektRepository */
21: private $objekte;
22:
23: public function __construct(Model\ObjektRepository $objekte) {
24: parent::__construct($objekte->getDatabase());
25: $this->objekte = $objekte;
26: }
27:
28: public function beforeRender() {
29:
30: if ($this->template->objekte === null) { // <== ERROR
31: $this->template->objekte = $this->objekte->findAll();
32: }
33:
34: $this->template->objekte = $this->objekte->findAll()->order('titel')->limit(10);
- CZechBoY
- Member | 3608
It's the same…
Try this
public function renderDefault() {
if ($this->template->objekte === null) {
$this->template->objekte = $objekte;
}
}
// called after form is successfully submitted
public function formSucceeded(UI\Form $form, $vals) {
dump($vals);
$this->template->objekte = $this->objekte->findAll()->where('anzahlPersonen >= ?', 7);
}