persistentní parametr v komponentě

Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
batko
Člen | 219
+
0
-

Ahoj, řeším problém s předáváním ID v komponentě. Mám komponentu na editaci záznamu.

metodě render($id) předám id

pokud ale provedu uložení formuláře tak se ID mezi tím ztratí a vyvolá to chybu kde ID je NULL

<?php



class FormProductEdit extends Nette\Application\UI\Control {

    /** @persistent */
    public $id = 0;

    /** @var object */
    public $product;

    /** @var Todo\ProductRepository Description */
    public $productRepository;

    public function __construct(Todo\ProductRepository $productRepository) {
        parent::__construct();
        $this->productRepository = $productRepository;
    }

    public function render($id) {
        $this->id = $id;

        $this->template->setFile(__DIR__ . '/FormProductEdit.latte');
        $this->template->render();
    }

    public function createComponentFormProductEdit() {

        $product = $this->productRepository->findBy(array("id" => $this->id))->fetch();

        $form = new Nette\Application\UI\Form();
        $form->addText("name", "název: ")->setValue($product->name);
        $form->addTextArea("text", "text: ")->setDefaultValue($product->text);
        $form->addTextArea("seo", "seo: ")->setDefaultValue($product->seo);
        $form->addText("price", "cena: ")
                ->setDefaultValue($product->price)
                ->addRule(Nette\Application\UI\Form::FILLED, "cena musí být vyplněna")
                ->addRule(Nette\Application\UI\Form::FLOAT, "cena musí být číslo");
        $form->addSubmit('save', 'uložit');
        $form->addHidden("id", $product->id);
        $form->onSuccess[] = $this->formProductEditProces;
        return $form;
    }

    public function formProductEditProces($form) {
          dump($this->id);
        unset($form->values->save);
        $this->productRepository->update($form->values->id, $form->values);
        $this->presenter->flashMessage("Produkt byl upraven", 'success');
        $this->presenter->redirect("product:default");
    }

}
?>
Felix
Nette Core | 1190
+
0
-

Pokud se nepletu tak bys vubec nemusel delat $this->id = $id;, po odeslani formulare, kdyz udelas dump($this->getParameter()); tak co vidis?