In fact Doctrine make use of another language (YAML) to deal with schema definition. The relationship between Doctrine and YAML could be found here.
In Symfony, in order to define your data model (which is fundamentally the relational database schema), the first file you need to hack in is the schema.yml which is placed under (config/doctrine/)
I followed the instruction in Practical Symfony book, which define the whole database and its relation in YAML format. But you can do a reverse Doctrine generate through symfony doctrine:build-schema, which essentially build the YAML file based on your existing mysql structure. Mind you you have to define the right foreign key and define the database info in databases.yml (in config/) first.
Here's the really cool thing about Symfony, php database objects generation.
symfony doctrine:build --model (generate models files)
symfony doctrine:build --sql (generate sql for inserting data models to SQL)
symfony doctrine:insert-sql (quite obvious, isn't it?)
Alternatively, all these steps could be compress to a simple
symfony doctrine:build --all command
So the database tables has been built, but we still need to load some data into the DB to get it to work. You're right, we need another YAML file. These files are put in /data/fixtures
For instance if we're going to put some data in the obeetCategory table, we can create a file like the following:
JobeetCategory: actAs: { Timestampable: ~ } columns: name: { type: string(255), notnull: true, unique: true }We can do the same for creating other yml files for other tables, and put it in the folder (data/fixture/)
After all the yml files are created, we can now run the following command and load the initial data in the database
symfony doctrine:data-load
Before we see the miracle of symfony, we need to run one more command to generate the nessceary php files in the module folder.
symfony doctrine:generate-module --with-show --non-verbose-templates frontend job JobeetJob
Now if we check the module folder (apps/frontend/modules/), a new folder (job/) is created with actions and templates in it.
Visit the following URL to have a taste of Symfony
http://localhost/frontend_dev.php/job
Viola, it's already working!
No comments:
Post a Comment