Databáze - úvod
Níže je uveden pouze náhled materiálu. Kliknutím na tlačítko 'Stáhnout soubor' stáhnete kompletní formátovaný materiál ve formátu PDF.
VALUES (?, ?, ?, ?)");
$stmt->bindParam(1, $nazev);
$stmt->bindParam(2, $popis);
$stmt->bindParam(3, $url);
$stmt->bindParam(4, $cena);
// vloz jeden zaznam
$nazev = 'DVD mechanika';
$nazev = 'DVD mechanika'
$popis = 'DVD vypalovačka';
$url = 'dvdobrazek.jpg';
$cena = '350';
$stmt->execute();
$dbh->commit();
Doctrine ORM
www.doctrine-project.org
Cíl – mapování objektového modelu a ER
Objekový model
Relační model
Relační mode
Ukázka – navázání spojení
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
require_once('../doctrine/branches/1.2/lib/Doctrine.php');spl_autoload_register(
array('Doctrine', 'autoload'));
bootstrap
$user = 'dbuser';
$password = 'dbpass';
$dbh =
new PDO($dsn, $user, $password);
$conn = Doctrine_Manager::connection($dbh);
// At this point no actual connection to the database is created
$conn = Doctrine_Manager::connection('mysql://username:password@localhost/test');
// The first time the connection is needed, it is instantiated
// This query triggers the connection to be created
$conn->execute('SHOW TABLES');
Lazy verze
Ukázka použití
$conn->export->createTable('test',
array('name' => array('type' =>
'string')));
$conn->execute('INSERT INTO test (name) VALUES (?)',
array('Martin'));
$stmt = $conn->prepare('SELECT * FROM test');
$stmt->execute();
$results = $stmt->fetchAll();
print_r($results);
Array
(
[0] => Array
(
[name] => Martin
[0] => Martin
)
)
Výsledek
Definice dat
// models/Book.php
class Book extends Doctrine_Record{
public function setTableDefinition(){
$this->hasColumn('bookTitle as title', 'string');
}
}
Model
// test.php
// ...
$book =
new Book();
$book->title = 'Some book';
$book->save();
Použití
Relační závislosti
// models/Forum_Thread.php
class Forum_Thread extends Doctrine_Record{
public function setTableDefinition(){
$this->hasColumn('user_id', 'integer');
$this->hasColumn('board_id', 'integer');
$this->hasColumn('title', 'string', 200);
$this->hasColumn('updated', 'integer', 10);
Model Forum_Thread
Forum_Board
Forum_Thread
1
N
// models/Forum_Boadr.php
class Forum_Board extends Doctrine_Record{
public function setTableDefinition(){
$this->hasColumn('name', 'string',
100);
$this->hasColumn('description',