Nette zblbnute vypisovanie z databazy
- Takeshi
- Člen | 596
Ahojte,
mam mensi problem … v databaze mam tri riadky dat (filmy)
pokial zadam do Basepresentera->beforeRender() nasledovny prikaz
<?php
$database = $this->getService('dvd_movies');
$this->template->count = $database->count("dvd_id");
?>
… vypise mi v sablone pocet fulmov (count()) a aj mi zobrazi vsetky data
(tri riadky)
… avsak ak tam pridam riadok
<?php
$database = $this->getService('dvd_movies');
$this->template->count = $database->count("dvd_id");
$this->template->countSerials = $database->where('dvd_genre','serial')->count('dvd_id');
?>
… v sablone mi sice vypise spravny pocet, teda
<?php
filmy_a_serialy (3)
serialy (1)
?>
…, ale ako data mi vypise len 1 riadok (teda nie tri ako by malo) …
… P.S. ospravedlnujem sa ak je to nie zrozumitelne :-)
- David Matějka
- Moderator | 6445
predpokladam, ze bude spatne sluzba dvd_movies. vypada to, ze dedi od Nette\Database\Table\Selection, ze?
- Takeshi
- Člen | 596
matej21 napsal(a):
predpokladam, ze bude spatne sluzba dvd_movies. vypada to, ze dedi od Nette\Database\Table\Selection, ze?
Hanbim sa, ale na toto ti neviem presne odpovedat … myslim ze nie, ale pre istotu ti posielam basePresenter aj config file, kde som nastavoval samotnu cestu k tabulke
basePresenter
<?php
abstract class BasePresenter extends Nette\Application\UI\Presenter
{
public function beforeRender() {
$database = $this->getService('dvd_movies');
$this->template->count = $database->count("dvd_id");
$this->template->countSerials = $database->where('dvd_genre','serial')->count('dvd_id');
}
}
?>
config.neon
<?php
#
# SECURITY WARNING: it is CRITICAL that this file & directory are NOT accessible directly via a web browser!
#
# If you don't protect this directory from direct web access, anybody will be able to see your passwords.
# https://nette.org/en/security-warning
#
common:
parameters:
php:
date.timezone: Europe/Prague
# zlib.output_compression: yes
nette:
application:
errorPresenter: Error
database:
dsn: 'mysql:host=localhost;dbname=database'
user: root
password:
session:
expiration: 14 days
services:
database: @Nette\Database\Connection
authenticator: Authenticator( @database::table(users) )
dvd_movies:
create: @database::table(dvd_movies)
routerFactory: RouterFactory
router: @routerFactory::createRouter
factories:
production < common:
development < common:
?>
- David Matějka
- Moderator | 6445
jo, to bude v dvd_movies jedna instance Nette\Database\Table\Selection
udelej si pro dvd_movies nejaky repository, treba del quickstartu https://doc.nette.org/cs/quickstart
- majky358
- Člen | 37
Custe, mám podobný problém
$table = $this->database->table('article');
$pages = count($table);
$content = $table->where('id', $id);
v tomto prípade je hodnota $pages ok, správny výsledok. Ak to dám na koniec, za *$table->where… tak už je vo výsledku vždy len 1. Rovnako count($content).
Dvá krát sa vykoná dotaz:
SELECT `id` FROM `article` WHERE (`id` = '1')
Editoval majky358 (11. 2. 2014 21:04)
- David Matějka
- Moderator | 6445
jelikoz stale pracujes s jednim objektem selection, do ktereho se pridavaji
where apod. pokud jsou to rozdilne dotazy, vytvor si novou selection
(->table()
)