Permalink
Browse files

added example Books

  • Loading branch information...
dg committed Sep 9, 2017
1 parent 82dd17c commit cbf733ec58031606e7e301acea04fc7126c9f0a7
@@ -0,0 +1,9 @@
{
"require": {
"php": ">=7.1",
"nette/database": "^3.0",
"nette/bootstrap": "^3.0",
"tracy/tracy": "^2.4"
},
"minimum-stability": "dev"
}
@@ -0,0 +1,7 @@
parameters:
dumpFile: %appDir%/dump/mysql.sql

database:
dsn: 'mysql:host=127.0.0.1;dbname=nette_test'
user: root
password:
@@ -0,0 +1,7 @@
parameters:
dumpFile: %appDir%/dump/postgresql.sql

database:
dsn: 'pgsql:host=127.0.0.1;dbname=nette_test'
user: postgres
password:
@@ -0,0 +1,5 @@
parameters:
dumpFile: %appDir%/dump/sqlite.sql

database:
dsn: 'sqlite::memory:'
@@ -0,0 +1,7 @@
parameters:
dumpFile: %appDir%/dump/sqlsrv.sql

database:
dsn: 'sqlsrv:server=127.0.0.1;database=nette_test'
user:
password:
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
// enable Tracy
// see https://doc.nette.org/en/bootstrap
$configurator = new Nette\Configurator;
$configurator->enableTracy(__DIR__ . '/log');
// create DI container
$configurator->setTempDirectory(__DIR__ . '/temp');
$configurator->addConfig(__DIR__ . '/config/sqlite.neon'); // for SQLite
//$configurator->addConfig(__DIR__ . '/config/mysql.neon'); // for MySQL
//$configurator->addConfig(__DIR__ . '/config/postgresql.neon'); // for PostgreSQL
//$configurator->addConfig(__DIR__ . '/config/sqlsrv.neon'); // for MS SQL Server
$container = $configurator->createContainer();
// get database from DI container
// see https://doc.nette.org/en/di-configuration
/** @var Nette\Database\Context $database */
$database = $container->getByType(Nette\Database\Context::class);
// load database dump
Nette\Database\Helpers::loadFromFile(
$database->getConnection(),
$container->parameters['dumpFile'] // defined in config file
);
// lists the author's name for each book and all its tags:
// see https://doc.nette.org/en/database-explorer
$books = $database->table('book');
echo PHP_SAPI === 'cli' ? '' : '<xmp>';
foreach ($books as $book) {
echo "title: {$book->title} \n";
echo "written by: {$book->author->name} \n"; // $book->author is row from table 'author'
echo 'tags: ';
foreach ($book->related('book_tag') as $bookTag) {
echo $bookTag->tag->name . ', '; // $bookTag->tag is row from table 'tag'
}
echo "\n\n";
}
echo PHP_SAPI === 'cli' ? '' : '</xmp>';
@@ -0,0 +1,93 @@
SET FOREIGN_KEY_CHECKS = 0;

DROP TABLE IF EXISTS `author`, `book`, `book_tag`, `book_tag_alt`, `note`, `tag`;


CREATE TABLE author (
id int NOT NULL AUTO_INCREMENT,
name varchar(30) NOT NULL,
web varchar(100) NOT NULL,
born date DEFAULT NULL,
PRIMARY KEY(id)
) ENGINE=InnoDB AUTO_INCREMENT=13;

INSERT INTO author (id, name, web, born) VALUES (11, 'Jakub Vrana', 'http://www.vrana.cz/', NULL);
INSERT INTO author (id, name, web, born) VALUES (12, 'David Grudl', 'http://davidgrudl.com/', NULL);
INSERT INTO author (id, name, web, born) VALUES (13, 'Geek', 'http://example.com', NULL);



CREATE TABLE tag (
id int NOT NULL AUTO_INCREMENT,
name varchar(20) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=25;

INSERT INTO tag (id, name) VALUES (21, 'PHP');
INSERT INTO tag (id, name) VALUES (22, 'MySQL');
INSERT INTO tag (id, name) VALUES (23, 'JavaScript');
INSERT INTO tag (id, name) VALUES (24, 'Neon');



CREATE TABLE book (
id int NOT NULL AUTO_INCREMENT,
author_id int NOT NULL,
translator_id int,
title varchar(50) NOT NULL,
next_volume int,
PRIMARY KEY (id),
CONSTRAINT book_author FOREIGN KEY (author_id) REFERENCES author (id),
CONSTRAINT book_translator FOREIGN KEY (translator_id) REFERENCES author (id),
CONSTRAINT book_volume FOREIGN KEY (next_volume) REFERENCES book (id)
) ENGINE=InnoDB AUTO_INCREMENT=5;

CREATE INDEX book_title ON book (title);

INSERT INTO book (id, author_id, translator_id, title) VALUES (1, 11, 11, '1001 tipu a triku pro PHP');
INSERT INTO book (id, author_id, translator_id, title) VALUES (2, 11, NULL, 'JUSH');
INSERT INTO book (id, author_id, translator_id, title) VALUES (3, 12, 12, 'Nette');
INSERT INTO book (id, author_id, translator_id, title) VALUES (4, 12, 12, 'Dibi');



CREATE TABLE book_tag (
book_id int NOT NULL,
tag_id int NOT NULL,
PRIMARY KEY (book_id, tag_id),
CONSTRAINT book_tag_tag FOREIGN KEY (tag_id) REFERENCES tag (id),
CONSTRAINT book_tag_book FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE
) ENGINE=InnoDB;

INSERT INTO book_tag (book_id, tag_id) VALUES (1, 21);
INSERT INTO book_tag (book_id, tag_id) VALUES (3, 21);
INSERT INTO book_tag (book_id, tag_id) VALUES (4, 21);
INSERT INTO book_tag (book_id, tag_id) VALUES (1, 22);
INSERT INTO book_tag (book_id, tag_id) VALUES (4, 22);
INSERT INTO book_tag (book_id, tag_id) VALUES (2, 23);



CREATE TABLE book_tag_alt (
book_id int NOT NULL,
tag_id int NOT NULL,
state varchar(30),
PRIMARY KEY (book_id, tag_id),
CONSTRAINT book_tag_alt_tag FOREIGN KEY (tag_id) REFERENCES tag (id),
CONSTRAINT book_tag_alt_book FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE
) ENGINE=InnoDB;

INSERT INTO book_tag_alt (book_id, tag_id, state) VALUES (3, 21, 'public');
INSERT INTO book_tag_alt (book_id, tag_id, state) VALUES (3, 22, 'private');
INSERT INTO book_tag_alt (book_id, tag_id, state) VALUES (3, 23, 'private');
INSERT INTO book_tag_alt (book_id, tag_id, state) VALUES (3, 24, 'public');



CREATE TABLE note (
book_id int NOT NULL,
note varchar(100),
CONSTRAINT note_book FOREIGN KEY (book_id) REFERENCES book (id)
) ENGINE=InnoDB;

SET FOREIGN_KEY_CHECKS = 1;
@@ -0,0 +1,92 @@
DROP TABLE IF EXISTS `author`, `book`, `book_tag`, `book_tag_alt`, `note`, `tag`;


CREATE TABLE author (
id serial NOT NULL,
name varchar(30) NOT NULL,
web varchar(100) NOT NULL,
born date DEFAULT NULL,
PRIMARY KEY(id)
);

INSERT INTO author (id, name, web, born) VALUES (11, 'Jakub Vrana', 'http://www.vrana.cz/', NULL);
INSERT INTO author (id, name, web, born) VALUES (12, 'David Grudl', 'http://davidgrudl.com/', NULL);
INSERT INTO author (id, name, web, born) VALUES (13, 'Geek', 'http://example.com', NULL);
SELECT setval('author_id_seq', 13, TRUE);



CREATE TABLE tag (
id serial NOT NULL,
name varchar(20) NOT NULL,
PRIMARY KEY (id)
);

INSERT INTO tag (id, name) VALUES (21, 'PHP');
INSERT INTO tag (id, name) VALUES (22, 'MySQL');
INSERT INTO tag (id, name) VALUES (23, 'JavaScript');
INSERT INTO tag (id, name) VALUES (24, 'Neon');
SELECT setval('tag_id_seq', 24, TRUE);



CREATE TABLE book (
id serial NOT NULL,
author_id int NOT NULL,
translator_id int,
title varchar(50) NOT NULL,
next_volume INT,
PRIMARY KEY (id),
CONSTRAINT book_author FOREIGN KEY (author_id) REFERENCES author (id),
CONSTRAINT book_translator FOREIGN KEY (translator_id) REFERENCES author (id),
CONSTRAINT book_volume FOREIGN KEY (next_volume) REFERENCES book (id)
);

CREATE INDEX book_title ON book (title);



INSERT INTO book (id, author_id, translator_id, title) VALUES (1, 11, 11, '1001 tipu a triku pro PHP');
INSERT INTO book (id, author_id, translator_id, title) VALUES (2, 11, NULL, 'JUSH');
INSERT INTO book (id, author_id, translator_id, title) VALUES (3, 12, 12, 'Nette');
INSERT INTO book (id, author_id, translator_id, title) VALUES (4, 12, 12, 'Dibi');
SELECT setval('book_id_seq', 4, TRUE);

CREATE TABLE book_tag (
book_id int NOT NULL,
tag_id int NOT NULL,
PRIMARY KEY (book_id, tag_id),
CONSTRAINT book_tag_tag FOREIGN KEY (tag_id) REFERENCES tag (id),
CONSTRAINT book_tag_book FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE
);

INSERT INTO book_tag (book_id, tag_id) VALUES (1, 21);
INSERT INTO book_tag (book_id, tag_id) VALUES (3, 21);
INSERT INTO book_tag (book_id, tag_id) VALUES (4, 21);
INSERT INTO book_tag (book_id, tag_id) VALUES (1, 22);
INSERT INTO book_tag (book_id, tag_id) VALUES (4, 22);
INSERT INTO book_tag (book_id, tag_id) VALUES (2, 23);



CREATE TABLE book_tag_alt (
book_id int NOT NULL,
tag_id int NOT NULL,
state varchar(30),
PRIMARY KEY (book_id, tag_id),
CONSTRAINT book_tag_alt_tag FOREIGN KEY (tag_id) REFERENCES tag (id),
CONSTRAINT book_tag_alt_book FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE
);

INSERT INTO book_tag_alt (book_id, tag_id, state) VALUES (3, 21, 'public');
INSERT INTO book_tag_alt (book_id, tag_id, state) VALUES (3, 22, 'private');
INSERT INTO book_tag_alt (book_id, tag_id, state) VALUES (3, 23, 'private');
INSERT INTO book_tag_alt (book_id, tag_id, state) VALUES (3, 24, 'public');



CREATE TABLE note (
book_id int NOT NULL,
note varchar(100),
CONSTRAINT note_book FOREIGN KEY (book_id) REFERENCES book (id)
);
@@ -0,0 +1,93 @@
DROP TABLE IF EXISTS note;
DROP TABLE IF EXISTS book_tag_alt;
DROP TABLE IF EXISTS book_tag;
DROP TABLE IF EXISTS book;
DROP TABLE IF EXISTS tag;
DROP TABLE IF EXISTS author;




CREATE TABLE author (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name TEXT NOT NULL,
web TEXT NOT NULL,
born DATE
);

INSERT INTO author (id, name, web, born) VALUES (11, 'Jakub Vrana', 'http://www.vrana.cz/', NULL);
INSERT INTO author (name, web, born) VALUES ('David Grudl', 'http://davidgrudl.com/', NULL);
INSERT INTO author (name, web, born) VALUES ('Geek', 'http://example.com', NULL);



CREATE TABLE tag (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name TEXT NOT NULL
);

INSERT INTO tag (id, name) VALUES (21, 'PHP');
INSERT INTO tag (name) VALUES ('MySQL');
INSERT INTO tag (name) VALUES ('JavaScript');
INSERT INTO tag (name) VALUES ('Neon');



CREATE TABLE book (
id INTEGER PRIMARY KEY AUTOINCREMENT,
author_id INTEGER NOT NULL,
translator_id INTEGER,
title TEXT NOT NULL,
next_volume INTEGER,
CONSTRAINT book_author FOREIGN KEY (author_id) REFERENCES author (id),
CONSTRAINT book_translator FOREIGN KEY (translator_id) REFERENCES author (id),
CONSTRAINT book_volume FOREIGN KEY (next_volume) REFERENCES book (id)
);

CREATE INDEX book_title ON book (title);

INSERT INTO book (author_id, translator_id, title) VALUES (11, 11, '1001 tipu a triku pro PHP');
INSERT INTO book (author_id, translator_id, title) VALUES (11, NULL, 'JUSH');
INSERT INTO book (author_id, translator_id, title) VALUES (12, 12, 'Nette');
INSERT INTO book (author_id, translator_id, title) VALUES (12, 12, 'Dibi');



CREATE TABLE book_tag (
book_id INTEGER NOT NULL,
tag_id INTEGER NOT NULL,
CONSTRAINT book_tag_tag FOREIGN KEY (tag_id) REFERENCES tag (id),
CONSTRAINT book_tag_book FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE,
PRIMARY KEY (book_id, tag_id)
);

INSERT INTO book_tag (book_id, tag_id) VALUES (1, 21);
INSERT INTO book_tag (book_id, tag_id) VALUES (3, 21);
INSERT INTO book_tag (book_id, tag_id) VALUES (4, 21);
INSERT INTO book_tag (book_id, tag_id) VALUES (1, 22);
INSERT INTO book_tag (book_id, tag_id) VALUES (4, 22);
INSERT INTO book_tag (book_id, tag_id) VALUES (2, 23);



CREATE TABLE book_tag_alt (
book_id INTEGER NOT NULL,
tag_id INTEGER NOT NULL,
state TEXT,
PRIMARY KEY (book_id, tag_id),
CONSTRAINT book_tag_alt_tag FOREIGN KEY (tag_id) REFERENCES tag (id),
CONSTRAINT book_tag_alt_book FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE
);

INSERT INTO book_tag_alt (book_id, tag_id, state) VALUES (3, 21, 'public');
INSERT INTO book_tag_alt (book_id, tag_id, state) VALUES (3, 22, 'private');
INSERT INTO book_tag_alt (book_id, tag_id, state) VALUES (3, 23, 'private');
INSERT INTO book_tag_alt (book_id, tag_id, state) VALUES (3, 24, 'public');



CREATE TABLE note (
book_id int NOT NULL,
note varchar(100),
CONSTRAINT note_book FOREIGN KEY (book_id) REFERENCES book (id)
);
Oops, something went wrong.

0 comments on commit cbf733e

Please sign in to comment.