Nedaří se použití related

- spiider
 - Člen | 162
 
Ahoj, používám datagrid pro nette database a chci si do něj poslat
data.
Mám dvě tabulky, jednu users a druhou tasks. Chci zobrazit vsechny tasky a
uživatele kterým jsou přiřazeny a tento seznam chci zobrazit pouze autorovi
tasku. Cili v tasks mam sloupce id_user(autor) a id_client(tomu je task
přiřazen). Dotaz mam takto:
$this->table = $this->context->createTasks();
$this->table->get($this->presenter->user->getId())
		->related('users')
		->through('id_client')
dotaz pošlu do gridu a hlásí mi to:
Call to a member function related() on a non-object
Volám tedy related nějak špatně? Koukal jsem v dokumentaci, ale přijde mi
že to mám dobře. Poradíte z nette database začínám. Díky

- vvoody
 - Člen | 910
 
Funkcia get musela vratit FALSE. Myslim ze ale problem bude v tom ze primarny kluc tabulky Tasks logicky nemoze byt IDcko uzivatela :) takze v tvojom pripade si narazil na pripad ked idcko uzivatela sa nerovna idcku ziadneho tasku.
Skus nieco taketo:
$this->users = $this->context->createUsers();
$user = $this->users->get($this->presenter->user->getId());
$tasks = $user->ref('tasks','id_client');
foreach($tasks as $task){
	$task; // a tu mas jednotlive tasky
}
Pisem to na slepo, snad to pojde. Samozrejme bude treba nadefinovat, ak este nemas, tovarnicku v configu pre users aby islo createUsers().

- vvoody
 - Člen | 910
 
Stale vkladas ID uzivatela do funkcie ktora predpoklada ze jej davas ID tasku.
To riesenie co som ti postol hore je sice skrabanie sa pravou rukou za lavym uchom, ale fungovala by bez problemu. Neuvedomil som si ze tebe stacia tasky.
Takze aby si to aj pochopil, ekvivalent tvojho kodu, ktory ti nefunguje, je
$this->table = $this->context->createTasks();
$user = $this->table->where('id',$this->presenter->user->getId());
$tasks = $user->ref('tasks', 'id_client');
(predpokladam ze stlpec s primarnym klucom v tabulke tasks
je id)
Funkcne riesenie: ak si v tom strateny tak rovno preskoc na „spravne riesenie“ :D
// zobrat tabulku users
$this->table = $this->context->createUsers();
// vybrat prihlaseneho uzivatela
$user = $this->table->where('id',$this->presenter->user->getId())->fetch();
// v tomto pripade to je to iste ako
// $user = $this->table->get($this->presenter->user->getId());
// a zistit jeho tasky
$tasks = $user->ref('tasks', 'id_client');
Spravne riesenie
//zobrat tabulku tasks
$this->context->createTasks();
// vybrat tasky ktore patria prihlasenemu uzivatelovy
$tasks = $this->table->where('id_client',$this->presenter->user->getId());
V tomto pripade nemozes pouzit get() tak ako si to robil ty, lebo ‚id_client‘ nieje primarny kluc v tabulke tasks.

- spiider
 - Člen | 162
 
Ale já potřebuju do výpisu i jméno a příjmení z tabulky users.
Tabulka tasks obsahuje sloupce id_task,id_user – fk z tabulky users kdo task
vytvořil, id_client-fk z tabulky users komu byl úkol přiřazen
A prostě potřebuju do datagridu předat selection které bude obsahovat
detaily o ukolu z tabulky tasks + jméno a příjmení komu byl úkol
zadán – z tabulky users. Snad jsem to napsal srozumitelně :) díky

- spiider
 - Člen | 162
 
je to tento grid :https://github.com/…tte-datagrid
Editoval spiider (4. 5. 2012 20:10)

- spiider
 - Člen | 162
 
Omlouvám se asi jsem hodil špatný link, mám to z tohoto vlákna https://forum.nette.org/…p-5-3-notorm
Editoval spiider (5. 5. 2012 11:16)

- vvoody
 - Člen | 910
 
Tak predsa v Selection ide nejako joinovat, no v dokumentacii to nieje, ona by ten join spravila aj automatika ale v taskoch mas dva kluce odkazujuce na tu istu tabulku (users) takze tomu Selection treba nejako napovedat, neviem ako, skus npr toto:
$tasks = $this->context->createTasks();
$tasks->select('tasks.*, users.name AS user_name')
	->where('tasks.id_client = users.id')
	->where('id_client',$this->presenter->user->getId());
a v datagride pridat stlpec
$dg->addColumn('user_name', 'Task vytvoril')->setTextFilter('users.name');
				
- spiider
 - Člen | 162
 
Mám to teda takhle:
$this->user = $this->context->createTasks();
$user = $this->table->select('tasks.*, users.lname')
        ->where('tasks.id_client = users.id')
        ->where('id_user',$this->presenter->user->getId());
Hlásí to:No reference found for $tasks->users přitom cizí klíče mám nastavené takže nevím proč to nejde stále.

- spiider
 - Člen | 162
 
CREATE TABLE tasks (
id_task int(10) NOT NULL AUTO_INCREMENT,
id_user int(10) NOT NULL,
id_client int(10) NOT NULL,
text text CHARACTER SET utf8 COLLATE utf8_czech_ci NOT NULL,
created datetime NOT NULL,
completed datetime NOT NULL,
type int(3) NOT NULL,
status tinyint(1) NOT NULL DEFAULT ‚0‘,
PRIMARY KEY (id_task),
KEY id_client (id_client),
KEY id_user (id_user),
CONSTRAINT tasks_ibfk_1 FOREIGN KEY (id_client)
REFERENCES users (id),
CONSTRAINT tasks_ibfk_2 FOREIGN KEY (id_user)
REFERENCES users (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE users (
id int(10) NOT NULL AUTO_INCREMENT,
fname varchar(20) COLLATE utf8_czech_ci NOT NULL,
lname varchar(40) COLLATE utf8_czech_ci NOT NULL,
email varchar(40) COLLATE utf8_czech_ci NOT NULL,
role varchar(30) COLLATE utf8_czech_ci NOT NULL,
password char(60) COLLATE utf8_czech_ci NOT NULL,
last_login datetime NOT NULL,
last_change datetime NOT NULL,
registered_by int(11) NOT NULL,
phone varchar(20) COLLATE utf8_czech_ci NOT NULL,
date_reg date NOT NULL,
reg_number varchar(15) COLLATE utf8_czech_ci NOT NULL,
specialist char(1) COLLATE utf8_czech_ci NOT NULL,
company_name varchar(60) COLLATE utf8_czech_ci NOT NULL,
user_reference int(10) NOT NULL,
note text COLLATE utf8_czech_ci NOT NULL,
birth_number varchar(15) COLLATE utf8_czech_ci NOT NULL,
degree varchar(20) COLLATE utf8_czech_ci NOT NULL,
city varchar(40) COLLATE utf8_czech_ci NOT NULL,
street varchar(40) COLLATE utf8_czech_ci NOT NULL,
number int(7) NOT NULL,
zip int(6) NOT NULL,
called varchar(1) COLLATE utf8_czech_ci NOT NULL DEFAULT
‚0‘,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;