Nette – jednoduché vyhľadávanie v MySQL
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
- Andurit
- Člen | 131
Čaute snažím sa napísať nejaké jednoduché vyhľadávanie v NETTE, s tým ze SQL kontroluje či je nejaké pole NULL.
vyzera to nejako takto:
Presenter:
protected function createComponentSearchForm()
{
$form = new Nette\Application\UI\Form;
$form->addText('firstname', 'FirstName:')
->setRequired('Please enter your firstname.');
$form->addText('surname', 'Surname:')
->setRequired('Please enter your surname.');
$form->addSubmit('send', 'Search');
// call method signInFormSucceeded() on success
$form->onSuccess[] = array($this, 'searchFormSucceeded');
return $form;
}
public function searchFormSucceeded($form, $values)
{
$this->template->searchResult = $this->search->findResults($values);
}
model:
function findResults($userParams)
{
return $this->db->query
("
SELECT *
FROM candidates as c
WHERE (@firstname IS NULL OR c.firstname LIKE '%' + @firstname + '%')
AND (@surname IS NULL OR c.surname LIKE '%' + @firstname + '%')
");
}
Formulár + dump premennej:
{form searchForm}
<div class="form-group">
<label class="col-md-3 col-xs-12 control-label">Meno</label>
<div class="col-md-6 col-xs-12">
<div class="input-group">
<span class="input-group-addon"><span class="fa fa-pencil"></span></span>
<input n:name="firstname" type="text" class="form-control"/>
</div>
<span class="help-block">Hľadá krstné meno </span>
</div>
</div>
<div class="form-group">
<label class="col-md-3 col-xs-12 control-label">Priezvisko</label>
<div class="col-md-6 col-xs-12">
<div class="input-group">
<span class="input-group-addon"><span class="fa fa-user"></span></span>
<input n:name="surname" type="text" class="form-control"/>
</div>
<span class="help-block">Hľadá priezvisko </span>
</div>
</div>
<div class="panel-footer">
<button class="btn btn-default">Resetovať formulár</button>
<button n:name="send" class="btn btn-primary pull-right">Hľadať</button>
</div>
{/form}
{dump $searchResult}
Napriek všetkemu dostavam chybu
ERROR:
Undefined variable: searchResult
Neviem sa odtiaľto odpichnúť, vedel by mi niekto poradiť?