Načtení funkce z modelu do presenteru

Allconius
Člen | 313
+
0
-

Ahoj mám tento model načtený přes servisu v neon.conf:

<?php
namespace App\Model;
use Nette;

class ShareManager
{
    use Nette\SmartObject;
    private $nar;

    public function __construct()
    {
        $this->nar = 999;
    }

    public function nar($str){
    $str = $str + $this->nar;
    if ($str==$this->nar) $str=0;
    return $str;
    }

    public function denar($str){
    if ($str==0) $str=$this->nar;
    $str = $str - $this->nar;
    return $str;
    }

}
?>

a nevím jak použít tu funkci nar(denar) v presenteru když ji chci použít už v konstruktoru ?:

<?php

namespace App\Presenters;

use Nette;
use App\Model\ShareManager;
use Nette\Application\UI\Form;


class HomepagePresenter extends Nette\Application\UI\Presenter
{


    private $shareManager;
    private $database;
    private $narozeniod;
    private $narozenido;


    parent::__construct(ShareManager $shareManager)
    {

    $this->shareManager = $shareManager;

    }

    public function __construct(Nette\Database\Context $database)
    {
        $this->database = $database;
        $this->narozeniod = $this->shareManager->nar(19180101);
        $this->narozenido = $this->shareManager->nar(date("Y").'1231');

    }
....
?>

Editoval Allconius (16. 11. 2018 12:47)

Ondřej Kubíček
Člen | 494
+
0
-

moc nevidím duvod proč cokoli dělat v constructoru v presenteru ?

Allconius
Člen | 313
+
0
-

Ahoj, no nastavuji si tam výchozí hodnoty proměnných

<?php
$this->jabko = 1;
$this->hruska = 7;
atd. .. :-)
?>

Ale možná to dělám blbje :-) Ale šlo mi o to jak udělat, když mám stejnou funkci v 5-ti presenterech abych to nemusel pořád měnit na 5-ti místech. Tak jsem myslel, že si udělám nějaký model s třídou třeba ABCTrida kde bude ta funkce a v těch presenterech si pak už jen zavolám:

<?php
knedlik = $this->ABCTrida->funkce($var);
?>
Ondřej Kubíček
Člen | 494
+
+1
-

to spíš, normálně si na to udělej třídu, injectni do presenteru přes @inject, pokud to máš pro více presenteru a používáš basepresenter tak to dej do něj a pak jen zavoláš, kde potřebuješ, a jestli to potřebuješ mít brzo zavolané, tak použij startup metodu

Allconius
Člen | 313
+
0
-

Ahoj, no nevím právě jak to injectnout do toho presenteru, když to udělám takto:

<?php

namespace App\Presenters;

use Nette;
use App\Model\ShareManager;
use Nette\Application\UI\Form;


class HomepagePresenter extends Nette\Application\UI\Presenter
{

    /** @var  ShareManager @inject */
    public $shareManager;
    private $database;
    private $narozeniod;

    public function __construct(Nette\Database\Context $database, ShareManager $shareManager)
    {
        $this->database = $database;
        $this->narozeniod = $this->shareManager->nar(19180101);

    }
?>

tak mi to vypíše:
Nette\DI\ServiceCreationException

Multiple services of type App\Model\ShareManager found: 27_App_Model_ShareManager, 28_App_Model_ShareManager

MajklNajt
Člen | 470
+
0
-

ako písal ondrej, namiesto __construct použiť metódu startup:

/** @var ShareManager @inject */
public $shareManager;

	/** @var Nette\Database\Context @inject */
private $database;

private $narozeniod;

public function startup()
{
		parent::startup();
    $this->narozeniod = $this->shareManager->nar(19180101);
}

btw. načo potrebuješ v presnteri Nette\Database\Context, keď si tam súčasne injectuješ nejaký model? práca s db je vecou modelu…

Editoval MajklNajt (19. 11. 2018 8:42)

Allconius
Člen | 313
+
0
-

Ahoj, to jsem tak původně chtěl mít to v modelu, ale nepodařilo se mi to rozchodit.
Mám 2 připojení do databáze tak jsem si chtěl pro tu DB2 vytvořit model Db2Manager.php v app/model:

<?php

namespace App\Model;

use Nette;

class Db2Manager
{
    use Nette\SmartObject;

    private $db2;

    public function __construct(Nette\Database\Context $db2)
    {
        $this->db2 = $db2;
    }

}
?>

v config.neon mám:

<?php
database:
    default:
        dsn: 'mysql:host=xxxxxxx;port=3311;dbname=db1'
        user: xxxxx
        password: xxxxx
        options:
            lazy: true
            autowired: true
    db2:
        dsn: 'mysql:host=xxxxx;port=3311;dbname=db2'
        user: xxxxxx
        password: xxxxxx
        options:
            lazy: true
            autowired: false

services:
    - App\Model\Db2Manager(@database.db2.context)
?>

pak mám presenter:

<?php

namespace App\Presenters;

use Nette;
use App\Model\Db2Manager;
use Nette\Application\UI\Form;


class SignPresenter extends Nette\Application\UI\Presenter
{


    /** @var Nette\Database\Context @inject */
    private $db2;


    public function __construct(Db2Manager $db2)
    {
        $this->db2 = $db2;
    }

....

     public function signChangeFormSucceeded(Form $form, $values)
    {

        $user = $this->getUser();
        $username = $user->getIdentity();
        //$user->setAuthenticator($this->authenticator);
        $id = $username->id;

        #save to DB
        $result = $this->db2->table('uzivatele')
        ->where('id', $id) // must be called before update()
        ->update([
        'heslo' => md5($values->password)
        ]);

        $this->getUser()->logout();
        $this->flashMessage('Změna hesla byla provedena. Prosím přihlašte se znovu.');
        $this->redirect('Sign:in');

    }
?>

ale píše mi to:
Nette\DI\ServiceCreationException

Service ‚application.6‘ (type of App\Presenters\SignPresenter): Multiple services of type App\Model\Db2Manager found: 30_App_Model_Db2Manager, 31_App_Model_Db2Manager (needed by $db2 in App\Presenters\SignPresenter::__construct())

David Matějka
Moderator | 6445
+
0
-

nemas ji nahodou v configu registrovanou 2×?

Allconius
Člen | 313
+
0
-

Ahoj, mám tam ještě ten autentifikátor, myslíš že se to mezi sebou nějak mele ? :-)
config.neon:

<?php
parameters:


application:
  errorPresenter: Error
  mapping:
    *: App\*Module\Presenters\*Presenter


session:
  expiration: 14 days


database:
    default:
        dsn: 'mysql:host=xxxxxxxx;port=3311;dbname=db1'
        user: xxxx
        password: xxxxx
        options:
            lazy: true
            autowired: true
    db2:
        dsn: 'mysql:host=xxxxxxx;port=3311;dbname=db2'
        user: xxxxx
        password: xxxxxxx
        options:
            lazy: true
            autowired: false

services:
    - App\Model\ShareManager
    - App\Model\Db2Manager(@database.db2.context)
    router: App\RouterFactory::createRouter
    authenticator:
      App\Auth\Authenticator(@database.db2.context)


extensions:
  tbDatePicker: RadekDostal\NetteComponents\DateTimePicker\TbDatePicker\DI\TbDatePickerExtension

tbDatePicker:
  format: j. n. Y
?>

Authenticator.php

<?php

namespace App\Auth;

use Nette;
use Nette\Security as NS;

class Authenticator implements NS\IAuthenticator
{

    private $db2;


    public function __construct(Nette\Database\Context $db2)
    {
        $this->db2 = $db2;

    }

    function authenticate(array $credentials)
    {
        list($username, $password) = $credentials;

        $row = $this->db2->table('trideni')
            ->where('aplikace', 70)
            ->where('uzivatele.login', $username)->fetch();

        if (!$row) {

            throw new NS\AuthenticationException('User not found.');

        }
        if ($password <> $row->uzivatele->heslo) {
            throw new NS\AuthenticationException('Invalid password.');

        }
        return new NS\Identity($row->iduzivatele, $row->uzivatele->login);
    }
}
?>
David Matějka
Moderator | 6445
+
0
-

a nededi nejaka sluzba od Db2Manager? treba to ShareManager?

Allconius
Člen | 313
+
0
-

Ahoj, měl jsi pravdu, já jsem nějak nepochopil rozdíl mezi config.local.neon a config.neon. Myslel jsem , že config.local.neon se bere když to mám u sebe na lokalhostu a config.neon, když to mám na serveru, ale pak jsem zjistil, že se načítají oba dva a tu službu jsem měl samozřejmě v obou :-)

Allconius
Člen | 313
+
0
-

Už to píše jen:
Nette\MemberAccessException

Call to undefined method App\Model\Db2Manager::table()

Allconius
Člen | 313
+
0
-

to už jsem vyřešil přidáním do modelu:

<?php

namespace App\Model;

use Nette;

class Db2Manager
{
    use Nette\SmartObject;

    /** @var Nette\Database\Context */
    private $db2;

    public function __construct(Nette\Database\Context $db2)
    {
        $this->db2 = $db2;
    }

    public function saveUser($id, $passw)
    {
        return $this->db2->table('uzivatele')
        ->where('id', $id) // must be called before update()
        ->update([
        'heslo' => md5($passw)
        ]);

    }

}
?>