Jak spravne strukturovat s paginatorem

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

Zdravím a prosím o radu.
Nevim, jak to spravne strukturovat. Volam data z tabulky Contracts. Je u toho filtr a tak jsem si udelal v modelu metodu getdata().
Problem je, ze potrebuji v presenteru zjistit pro paginator pocet zaznamu, ale v zavislosti na filtru.

Pokud tedy volam v presenteru:

$paginator->itemCount = $this->context->contractsModel->total;

Vysledek mu trva pres 3 vteriny…

Delaji to samozrejme ty soucty, ktere potrebuju, ale nevim, jak to nejak optimalne oddelit.

Diky za radu !

Presenter:

public function renderDefault() {

	$vp = $this['vp'];
	$paginator = $vp->paginator;
	$paginator->itemsPerPage = 25;

	$this->template->contracts = $this->context->contractsModel->getData('contracts', $this->orderby, $paginator->offset, $paginator->itemsPerPage, $this->active, $this->search, $this->branch);
	$paginator->itemCount = $this->context->contractsModel->total;
	$this->template->total = $this->context->contractsModel->total;
	$this->template->ptotal = $this->context->contractsModel->ptotal;
	$this->template->pbtotal = $this->context->contractsModel->pbtotal;
	$this->backlink = $this->getApplication()->storeRequest();
    }

Model:

function __construct(\Nette\Database\Connection $database) {

	parent::__construct($database);

	$this->data = $this->database->table('contracts');
    }

    function getData($table, $orderby, $offset, $limit, $active, $search = NULL, $branch = null) {

	$this->data->select('contracts.*, clients.name AS client_name, branches.name AS branch_name,
								    (SELECT predpis FROM mrp WHERE cislo_zak = contracts.mrp_cislo ORDER BY cislo DESC LIMIT 1) AS last,
								    (SELECT SUM(sum)-SUM(predpis) FROM mrp WHERE cislo_zak = mrp_cislo) AS saldo');

	$this->pbtotal = $this->database->table('payments')->select('sum(amount) AS pbtotal')->WHERE('NOW() BETWEEN `since` AND IFNULL(`to`, \'2099-12-31\')')->fetch()->pbtotal;


	if ($search) {
	    $this->data->where('contracts.name LIKE ? OR branches.name LIKE ? OR contracts.mrp_cislo LIKE ? OR contracts.note LIKE ?', '%' . $search . '%', '%' . $search . '%', '%' . $search . '%', '%' . $search . '%');
	}

	if ($active == 1) {
	    $this->data->where('NOW() BETWEEN contracts.since AND IFNULL(contracts.to, \'2099-12-31\')');
	}

	if($branch) {
	    $this->data->where('branches_id', $branch);
	    $this->pbtotal = $this->database->table('payments')->select('sum(amount) AS pbtotal')->WHERE('NOW() BETWEEN `since` AND IFNULL(`to`, \'2099-12-31\') AND branchId = ?', $branch)->fetch()->pbtotal;
	}

	$this->total = $this->data->count(); // paginator
	$this->ptotal = $this->data->sum('price');

	return $this->data->order($orderby)->limit($limit, $offset);
    }

    function getDataById($id) {
	return $this->data->where('contracts.id', $id);
	}
    }
David Matějka
Moderator | 6445
+
0
-

Nette\Database\Table\Selection::count() bez parametru vybere vse a spocita pocet zaznamu pres php count(), takze bez parametru ho nikdy nepouzivej. staci dat count(‚id‘) a provede to ten spravny dotaz, viz https://api.nette.org/…ion.php.html#433

MW
Člen | 626
+
0
-

To je ono.. Diky !!!