Last active
April 12, 2016 08:10
-
-
Save LeoOnTheEarth/3e5d57dec75b537eb7185ca013f8d186 to your computer and use it in GitHub Desktop.
Propel2 Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"propel/propel": "2.0.0-alpha5", | |
"twig/twig": "~1.24" | |
}, | |
"autoload": { | |
"psr-4": { | |
"": "generated-classes/" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require __DIR__ . '/vendor/autoload.php'; | |
require __DIR__ . '/generated-conf/config.php'; | |
$loader = new Twig_Loader_Filesystem(__DIR__ . '/view'); | |
$twig = new Twig_Environment($loader, []); | |
// ========== 寫資料 ========== | |
$book = new \My\Entity\Book; | |
$book->setTitle('挪威森林'); | |
$book->setISBN('MORI123LOST'); | |
$author = new \My\Entity\Author; | |
$author->setLastName('村上'); | |
$author->setFirstName('春樹'); | |
$publisher = new \My\Entity\Publisher; | |
$publisher->setName('JAPAN SOMEWHERE'); | |
var_dump($author->getId()); | |
var_dump($publisher->getId()); | |
$book->setAuthor($author); | |
$book->setPublisher($publisher); | |
$book->save(); // author, publisher 資料庫才有資料 | |
var_dump($author->getId()); | |
var_dump($publisher->getId()); | |
$book2 = new \My\Entity\Book; | |
$book2->setTitle('科學人'); | |
$book2->setISBN('SCIENCE123HUMAN'); | |
$book2->setAuthor($author); | |
$book2->setPublisher($publisher); | |
$book2->save(); | |
var_dump($author->getId()); | |
var_dump($publisher->getId()); | |
// ========== 找資料 ========== | |
$bookQuery = new \My\Entity\BookQuery; | |
$authorQuery = new \My\Entity\AuthorQuery(); | |
$books = $bookQuery | |
//->filterByAuthor($author) | |
->filterByAuthorId(4) | |
->find(); | |
// 搭配 Twig 使用 | |
foreach ($books as $book) | |
{ | |
$bookAuthor = $book->getAuthor(); | |
$bookPublisher = $book->getPublisher(); | |
echo $twig->render('index.html.twig', [ | |
'book' => $book, | |
'bookAuthor' => $bookAuthor, | |
'bookPublisher' => $bookPublisher, | |
]); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
propel: | |
database: | |
connections: | |
propel2_demo: | |
adapter: mysql | |
classname: Propel\Runtime\Connection\ConnectionWrapper | |
dsn: "mysql:host=localhost;dbname=my_propel2_demo" | |
user: YOUR_USER | |
password: YOUR_PASSWORD | |
attributes: | |
settings: | |
charset: utf8mb4 | |
queries: | |
utf8: "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci, COLLATION_CONNECTION = utf8mb4_unicode_ci, COLLATION_DATABASE = utf8mb4_unicode_ci, COLLATION_SERVER = utf8mb4_unicode_ci" | |
runtime: | |
defaultConnection: propel2_demo | |
connections: | |
- propel2_demo | |
generator: | |
defaultConnection: propel2_demo | |
connections: | |
- propel2_demo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<database name="propel2_demo" defaultIdMethod="native" namespace="My\Entity" tablePrefix="foo_"> | |
<table name="book" phpName="Book"> | |
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/> | |
<column name="title" type="varchar" size="255" required="true" /> | |
<column name="isbn" type="varchar" size="24" required="true" phpName="ISBN"/> | |
<column name="publisher_id" type="integer" required="true"/> | |
<column name="author_id" type="integer" required="true"/> | |
<foreign-key foreignTable="publisher" phpName="Publisher" refPhpName="Book"> | |
<reference local="publisher_id" foreign="id"/> | |
</foreign-key> | |
<foreign-key foreignTable="author"> | |
<reference local="author_id" foreign="id"/> | |
</foreign-key> | |
</table> | |
<table name="author" phpName="Author"> | |
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/> | |
<column name="first_name" type="varchar" size="128" required="true"/> | |
<column name="last_name" type="varchar" size="128" required="true"/> | |
</table> | |
<table name="publisher" phpName="Publisher"> | |
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" /> | |
<column name="name" type="varchar" size="128" required="true" /> | |
</table> | |
</database> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment