Problém pri presune z local(u) na hosting
- StanlieK
- Člen | 28
Ahojte, pri presune na hosting, z local(u) mi Nette hádže túto chybu… Na locale ju nehádže, ako z toho von.
Chyba: Using $this when not in object context
25: }
26:
27: protected function createComponentForm($name) {
28: $form = new Nette\Application\UI\Form($this, $name);
29:
30: $znackyPairs = $this->znackyRepository->findAll()->order('znacka ASC')->fetchPairs('id', 'znacka');
31:
32: $form->addSelect('znacka', 'Značka', $znacka = array('0' => 'Nezáleží', $znackyPairs));
33: $form->addDSelect('model', 'Model', $form['znacka'], function($form) {
34: $v = $form['znacka']->getValue();
35: $modelsPairs = $this->modelyRepository->findBy(array('znacka_id' => $v))->order('model ASC')->fetchPairs('id', 'model');
36: if ($v == '0'){
37: $modelyPairs = $this->modelyRepository->findAll()->order('model ASC')->fetchPairs('id', 'model');
38: return array ('0' => 'Nezáleží', $modelyPairs);
39: }
Chyba je na 35: riadku.
Pridávam aj celý zdroj:
<?php
/**
* Homepage presenter.
*/
class HomepagePresenter extends BasePresenter
{
private $znackyRepository;
private $modelyRepository;
protected function startup() {
parent::startup();
$this->znackyRepository = $this->context->znackyRepository;
$this->modelyRepository = $this->context->modelyRepository;
\DependentSelectBox\DependentSelectBox::register('addDSelect');
\DependentSelectBox\JsonDependentSelectBox::register('addJSelect');
}
protected function beforeRender() {
parent::beforeRender();
DependentSelectBox\JsonDependentSelectBox::tryJsonResponse($this);
}
public function renderDefault() {
}
protected function createComponentForm($name) {
$form = new Nette\Application\UI\Form($this, $name);
$znackyPairs = $this->znackyRepository->findAll()->order('znacka ASC')->fetchPairs('id', 'znacka');
$form->addSelect('znacka', 'Značka', $znacka = array('0' => 'Nezáleží', $znackyPairs));
$form->addDSelect('model', 'Model', $form['znacka'], function($form) {
$v = $form['znacka']->getValue();
$modelsPairs = $this->modelyRepository->findBy(array('znacka_id' => $v))->order('model ASC')->fetchPairs('id', 'model');
if ($v == '0'){
$modelyPairs = $this->modelyRepository->findAll()->order('model ASC')->fetchPairs('id', 'model');
return array ('0' => 'Nezáleží', $modelyPairs);
}
else {
return array('0' => 'Nezáleží', $modelsPairs);
}
});
$form->addSelect('palivo', 'Palivo', $palivo = array(
'' => 'Nezáleží',
'1' => 'Diesel',
'2' => 'Benzín',
'3' => 'LPG',
'4' => 'Hybrid',
)
);
$form->addSelect('prevodovka', 'Prevodovka', $prevodovka = array(
'' => 'Nezáleží',
'1' => 'Manuálna',
'2' => 'Automatická',
)
);
$presenter = $this;
if($this->isAjax()) {
$form['model']->addOnSubmitCallback(function() use($presenter) {
$presenter->invalidateControl('formSnippet');
});
}
$form->addSubmit('vyhladat', 'Vyhľadať')->onClick[] = function($button) {
dump($button->form->values);
};
return $form;
}
}
Ako z toho von, prosím.
- peter.z
- Člen | 37
Aku verziu PHP mas na serveri?
Edit:
Ak tam mas PHP 5.3 a nie PHP 5.4, nemozes pouzit $this v anonymnych funkciach.
T.j. musis spravit nieco taketo:
$self = $this;
$form->addDSelect('model', 'Model', $form['znacka'], function($form) use ($self) {
$v = $form['znacka']->getValue();
$modelsPairs = $self->modelyRepository->findBy(array('znacka_id' => $v))->order('model ASC')->fetchPairs('id', 'model');
if ($v == '0'){
$modelyPairs = $self->modelyRepository->findAll()->order('model ASC')->fetchPairs('id', 'model');
return array ('0' => 'Nezáleží', $modelyPairs);
} else {
return array('0' => 'Nezáleží', $modelsPairs);
}
});
Editoval peter.z (6. 4. 2013 17:55)
- StanlieK
- Člen | 28
A je tu ďalší problém. Vie to niekto prepísať tak, aby to fungovalo na PHP 5.3? Prosím, ja to asi fakt nezvládnem sám, potrebujem pomoc.
<?php
/**
* Homepage presenter.
*/
class HomepagePresenter extends BasePresenter
{
private $znackyRepository;
private $modelyRepository;
protected function startup() {
parent::startup();
$this->znackyRepository = $this->context->znackyRepository;
$this->modelyRepository = $this->context->modelyRepository;
$self = $this;
$self->modelyRepository = $this->context->modelyRepository;
\DependentSelectBox\DependentSelectBox::register('addDSelect');
\DependentSelectBox\JsonDependentSelectBox::register('addJSelect');
}
protected function beforeRender() {
parent::beforeRender();
DependentSelectBox\JsonDependentSelectBox::tryJsonResponse($this);
}
public function renderDefault() {
}
protected function createComponentForm($name) {
$self = $this;
$form = new Nette\Application\UI\Form($this, $name);
$znackyPairs = $this->znackyRepository->findAll()->order('znacka ASC')->fetchPairs('id', 'znacka');
$form->addSelect('znacka', 'Značka', $znacka = array('0' => 'Nezáleží', $znackyPairs));
$form->addDSelect('model', 'Model', $form['znacka'], function($form) use ($self) {
$v = $form['znacka']->getValue();
$modelsPairs = $self->modelyRepository->findBy(array('znacka_id' => $v))->order('model ASC')->fetchPairs('id', 'model');
if ($v == '0'){
$modelyPairs = $this->modelyRepository->findAll()->order('model ASC')->fetchPairs('id', 'model');
return array ('0' => 'Nezáleží', $modelyPairs);
}
else {
return array('0' => 'Nezáleží', $modelsPairs);
}
});
$form->addSelect('palivo', 'Palivo', $palivo = array(
'' => 'Nezáleží',
'1' => 'Diesel',
'2' => 'Benzín',
'3' => 'LPG',
'4' => 'Hybrid',
)
);
$form->addSelect('prevodovka', 'Prevodovka', $prevodovka = array(
'' => 'Nezáleží',
'1' => 'Manuálna',
'2' => 'Automatická',
)
);
$presenter = $this;
if($this->isAjax()) {
$form['model']->addOnSubmitCallback(function() use($presenter) {
$presenter->invalidateControl('formSnippet');
});
}
$form->addSubmit('vyhladat', 'Vyhľadať')->onClick[] = function($button) {
dump($button->form->values);
};
return $form;
}
}
Tento script teraz hádže túto chybu – Cannot read an undeclared property HomepagePresenter::$modelyRepository.
- peter.z
- Člen | 37
<?php
/**
* Homepage presenter.
*/
class HomepagePresenter extends BasePresenter
{
public $znackyRepository;
public $modelyRepository;
protected function startup() {
parent::startup();
$this->znackyRepository = $this->context->znackyRepository;
$this->modelyRepository = $this->context->modelyRepository;
\DependentSelectBox\DependentSelectBox::register('addDSelect');
\DependentSelectBox\JsonDependentSelectBox::register('addJSelect');
}
protected function beforeRender() {
DependentSelectBox\JsonDependentSelectBox::tryJsonResponse($this);
}
protected function createComponentForm() {
$form = new Nette\Application\UI\Form;
$znackyPairs = $this->znackyRepository->findAll()->order('znacka ASC')->fetchPairs('id', 'znacka');
$form->addSelect('znacka', 'Značka', $znackyPairs)
->setPrompt('Nezáleží');
$self = $this;
$form->addDSelect('model', 'Model', $form['znacka'], function($form) use ($self) {
$v = $form['znacka']->getValue();
$modelsPairs = $self->modelyRepository->findBy(array('znacka_id' => $v))->order('model ASC')->fetchPairs('id', 'model');
if ($v == '0'){
$modelyPairs = $this->modelyRepository->findAll()->order('model ASC')->fetchPairs('id', 'model');
return array ('0' => 'Nezáleží', $modelyPairs);
}
else {
return array('0' => 'Nezáleží', $modelsPairs);
}
});
$form->addSelect('palivo', 'Palivo', array(
'1' => 'Diesel',
'2' => 'Benzín',
'3' => 'LPG',
'4' => 'Hybrid',
)
)->setPrompt('Nezáleží');
$form->addSelect('prevodovka', 'Prevodovka', array(
'1' => 'Manuálna',
'2' => 'Automatická',
)
)->setPrompt('Nezáleží');
if ($this->isAjax()) {
$form['model']->addOnSubmitCallback(function() use($self) {
$self->invalidateControl('formSnippet');
});
}
$form->addSubmit('vyhladat', 'Vyhľadať')->onClick[] = function($button) {
dump($button->form->values);
};
return $form;
}
}
A este dalsia vec: skus si pozriet injectovanie sluzieb do presenteru – https://pla.nette.org/…ect-autowire. Zbavis sa tym toho nepekneho $this->context->znackyRepository a pod. v startupe.
- StanlieK
- Člen | 28
Vedel by mi niekto ukázať to injectovanie napr. na tomto scripte. Pretože ten návod nejako neviem pochopiť, neviem, čo mi je :D, ale fakt mi to nechce do hlavy ísť, aj keď možno len niekde robím chybu … Vďaka za pochopenie
p.s. Ďakujem peter.z, script mi už pekne krásne funguje.
- peter.z
- Člen | 37
Cez inject:
public function injectRepositories(ZnackyRepository $znackyRepository, ModelyRepository $modelyRepository)
{
$this->znackyRepository = $znackyRepository;
$this->modelyRepository = $modelyRepository;
}
Funkcia ale predpoklada, ze ZnackyRepository a ModelyRepository su v rovnakom namespace ako presenter, tak si to budes musiet pravdepodobne upravit podla seba.