Insert do child tabulky referencujucej id rodica

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

Zdravim,

mam dve tabulky ‚users‘ a ‚users_details‘, ktore vytvaram takto:

<?php
CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(16) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `active` tinyint(1) DEFAULT '1' COMMENT '	',
  `role` tinytext,
  `banned` tinyint(1) DEFAULT '0',
  `reset_key` varchar(255) DEFAULT NULL,
  `reset_expires` timestamp NULL DEFAULT NULL,
  `registered` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`),
  UNIQUE KEY `username_UNIQUE` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='Table containing basic user details';

CREATE TABLE `users_details` (
  `user_id` int(11) NOT NULL,
  `first_name` varchar(45) DEFAULT NULL,
  `last_name` varchar(45) DEFAULT NULL,
  `profile_photo` varchar(255) DEFAULT NULL,
  `gender` varchar(6) DEFAULT NULL,
  `facebook_id` varchar(16) DEFAULT NULL,
  `google_id` varchar(20) DEFAULT NULL,
  `updated` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`user_id`),
  UNIQUE KEY `user_id_UNIQUE` (`user_id`),
  CONSTRAINT `fk_userdetails_users` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

?>

Pre kazdu tabulku mam ‚UsersRepository‘ a ‚Users_detailsRepository‘. Naplnit udaje do prvej tabulky samozrejme nie je problem, no da sa nejako rozumne vyuzit Nette\Database, na to aby som automaticky prepojil pomocou ->insert funkcie tieto dve tabulky? Teda existuje riesenie aby som do metody v Users_detailsRepository nemusel rucne predavat ‚user_id‘ ?

momentalne to mam takto:

<?php
// UsersRepository class
	/**
	 * Register new Facebook or Google user to the database
	 * @param array me Facebook or Google user profile info array
	 * @return Nette\Database\Table\ActiveRow
	 */
	public function registerFromSocial(array $me, $setActive = 1)
	{
		return $this->create(array(
			'email' 		=> $me['email'],
			'role' 			=> 'user',
			'active'		=> $setActive,
		));
	}

// Users_detailsRepository

	/**
	 * Register new Facebook user to the database
	 * @param User_id from users
	 * @param array me Facebook user profile info array
	 * @return Nette\Database\Table\ActiveRow
	 */
	public function createFromFacebook($user_id, array $me)
	{
		// $stop();
		$this->insert(array(
			'user_id'		=> $user_id,
			'facebook_id' 	=> $me['id'],
			'first_name'	=> $me['first_name'],
			'last_name'		=> $me['last_name'],
			'gender'		=> $me['gender'],
		));

		return $user_id;
	}
?>

V NotOrm (ktore nepouzivam) som nasiel ze sa da spravit nieco taketo:

<?php
// insert application
$application = $db->application()->insert(array(
    "id" => 5, // omit or null for auto_increment
    "author_id" => 11,
    "title" => "NotORM",
));
$application->application_tag()->insert(array("tag_id" => 21)); // insert its tag

// Vygeneruje
INSERT INTO application (id, author_id, title) VALUES (5, 11, 'NotORM');
INSERT INTO application_tag (application_id, tag_id) VALUES (5, 21);
?>

Co v podstate robi to co by som potreboval. Da sa nieco take spravit priamo v Nette\Database?

Dakujem za napady, pripadne pripomienky ze nieco robim zle :)

David Matějka
Moderator | 6445
+
0
-

v Nette\Database by melo fungovat nad ActiveRow obsahujici zaznam z users tohle:

->related('users_details')->insert(...);

a ten activerow by se snad mel vracet z toho insertu, jak tam mas..

Stic
Člen | 28
+
0
-

matej21 napsal(a):

v Nette\Database by melo fungovat nad ActiveRow obsahujici zaznam z users tohle:

->related('users_details')->insert(...);

a ten activerow by se snad mel vracet z toho insertu, jak tam mas..

tak som skusil takto:

<?php
	/**
	 * Register new Facebook user to the database
	 * @param Nette\Database\Table\ActiveRow Users
	 * @param array me Facebook user profile info array
	 * @return Nette\Database\Table\ActiveRow
	 */
	public function createFromFacebook($user, array $me)
	{
		return $user->related($this->getTableName())->insert(array(
			'facebook_id' 	=> $me['id'],
			'first_name'	=> $me['first_name'],
			'last_name'		=> $me['last_name'],
			'gender'		=> $me['gender'],
		));
	}
?>

A funguje. Nevedel som ze to ->related funguje aj na insertoch, mam v tom stale celkom gulas. Dakujem