symfony 2

22
SYMFONY ORM - DOCTRINE Sayed Ahmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years [email protected] http://sayed.justetc.net 1 0 / 8 / 2 0 1 1 s a y e d @ j u s t e t c . n e t 1

Upload: sayed-ahmed

Post on 12-Feb-2017

21 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Symfony 2

[email protected]

1

SYMFONYORM - DOCTRINE

Sayed Ahmed

B.Sc. Eng. in Computer Science & EngineeringM. Sc. in Computer ScienceExploring Computing for 14+ [email protected]://sayed.justetc.net

10/8/2011

Page 2: Symfony 2

2

[email protected]

SYMFONY - ORM Instead of writing SQL statements to retrieve records from the

database, we can use objects Doctrine and Propel can help We will discuss Doctrine

The ORM needs a description of the tables and their relationships to create the related classes

Can be done using tools or by hand You can define the schema in the file

config/doctrine/schema.yml $ php symfony doctrine:build-schema

It will generate the schema if database schema is defined in databases.yml

Configuration files are written in YAML YAML Format: http://components.symfony-project.org/yaml/trunk/book/02-YAML

10/8/2011

Page 3: Symfony 2

3

[email protected]

THE RELATIONAL MODEL 10/8/2011

Page 4: Symfony 2

4

[email protected]

YAML SCHEMA # config/doctrine/schema.yml JobeetCategory:

actAs: { Timestampable: ~ } columns:

name: { type: string(255), notnull: true, unique: true }

10/8/2011

Page 5: Symfony 2

5

[email protected]

YAML SCHEMA JobeetJob:

actAs: { Timestampable: ~ } columns:

category_id: { type: integer, notnull: true } type: { type: string(255) } c ompany: { type: string(255), notnull: true } logo: { type: string(255) } url: { type: string(255) } position: { type: string(255), notnull: true } location: { type: string(255), notnull: true } description: { type: string(4000), notnull: true } how_to_apply: { type: string(4000), notnull: true } token: { type: string(255), notnull: true, unique: true } is_public: { type: boolean, notnull: true, default: 1 } is_activated: { type: boolean, notnull: true, default: 0 } email: { type: string(255), notnull: true } expires_at: { type: timestamp, notnull: true }

relations: JobeetCategory: {

onDelete: CASCADE, local: category_id, foreign: id, foreignAlias: JobeetJobs

}

10/8/2011

Page 6: Symfony 2

6

[email protected]

YAML SCHEMA JobeetAffiliate:

actAs: { Timestampable: ~ } columns:

url: { type: string(255), notnull: true } email: { type: string(255), notnull: true, unique: true } token: { type: string(255), notnull: true } is_active: { type: boolean, notnull: true, default: 0 }

relations: JobeetCategories:

class: JobeetCategory refClass: JobeetCategoryAffiliate local: affiliate_id foreign: category_id foreignAlias: JobeetAffiliates

10/8/2011

Page 7: Symfony 2

7

[email protected]

YAML SCHEMA JobeetCategoryAffiliate:

columns: category_id: { type: integer, primary: true } affiliate_id: { type: integer, primary: true } relations:

JobeetCategory: { onDelete: CASCADE, local: category_id, foreign: id

} JobeetAffiliate: {

onDelete: CASCADE, local: affiliate_id, foreign: id }

10/8/2011

Page 8: Symfony 2

8

[email protected]

CONFIGURE DATABASE Configure database in Symfony

php symfony configure:database "mysql:host=localhost;dbname=jobeet" root mYsEcret

it might be better to edit the config/databases.yml to change the password.

Of course, to keep the password safe, the configuration file access mode should also be

restricted

10/8/2011

Page 9: Symfony 2

9

[email protected]

CREATING THE MODEL & DATABASE Using the database description from the

schema.yml file we can use some Doctrine built-in tasks to generate

the SQL statements needed to create the database tables

to generate the SQL you must build your models from your schema files $ php symfony doctrine:build –model

Now that your models are present you can generate and insert the SQL. $ php symfony doctrine:build –sql

The doctrine:build --sql task generates SQL statements in the data/sql/ directory optimized for the database engine we have configured

10/8/2011

Page 10: Symfony 2

10

[email protected]

CREATING THE MODEL & DATABASE To actually create the tables in the database,

you need to run the doctrine:insert-sql task $ php symfony doctrine:insert-sql

Syntax: Symfony commands: $ php symfony help doctrine:insert-sql

The ORM also generates PHP classes that map table records to objects: $ php symfony doctrine:build –model

The doctrine:build --model task generates PHP files in the lib/model/

directory that can be used to interact with the database

10/8/2011

Page 11: Symfony 2

11

[email protected]

CREATING THE MODEL & DATABASE Doctrine generates three classes per table.

For the jobeet_job table: JobeetJob: An object of this class represents a

single record of the jobeet_job table. The class is empty by default.

BaseJobeetJob: The parent class of JobeetJob. Each time you run doctrine:build --model, this class is overwritten, so all customizations must be done in the JobeetJob class.

JobeetJobTable: The class defines methods that mostly return collections of JobeetJob objects. The class is empty by default.

10/8/2011

Page 12: Symfony 2

12

[email protected]

ACCESSORS & MUTATORS $job = new JobeetJob(); $job->setPosition('Web developer'); $job->save();   echo $job->getPosition();   $job->delete();

10/8/2011

Page 13: Symfony 2

13

[email protected]

DEFINE FOREIGN KEYS $category = new JobeetCategory(); $category->setName('Programming');   $job = new JobeetJob(); $job->setCategory($category);

10/8/2011

Page 14: Symfony 2

14

[email protected]

DOCTRINE:BUILD --ALL The doctrine:build --all

is a shortcut for the tasks we have run in this section and some more

run this task now to generate forms and validators for the Jobeet model classes:

$ php symfony doctrine:build --all --no-confirmation

10/8/2011

Page 15: Symfony 2

15

[email protected]

THE DATA The Data

Initial data: Test data User data

Each time symfony creates the tables in the database, all the data are lost.

We can create YAML files in the data/fixtures/ directory to define initial data and use the doctrine:data-load task to load data

into the database.

10/8/2011

Page 16: Symfony 2

16

[email protected]

DATA FIXTURE FILES # data/fixtures/categories.yml JobeetCategory:

design: name: Design

programming: name: Programming

manager: name: Manager

administrator: name: Administrator

10/8/2011

Page 17: Symfony 2

17

[email protected]

LOAD INITIAL DATA $ php symfony doctrine:data-load $ php symfony doctrine:build --all --and-load

10/8/2011

Page 18: Symfony 2

18

[email protected]

MODULES – AUTO GENERATE symfony project is made of applications Each application is further divided into

modules. A module is a self-contained set of PHP code that

represents a feature of the application (the API module for

example) or a set of manipulations the user can do on a model

object (a job module for example) Symfony is able to automatically generate

a module for a given model that provides basic manipulation features:

$ php symfony doctrine:generate-module --with-show --non-verbose-templates frontend job JobeetJob

10/8/2011

Page 19: Symfony 2

19

[email protected]

MODULES – AUTO GENERATE the apps/frontend/modules/job/ directory:

actions/ The module actions templates/ The module templates

The actions/actions.class.php file defines all the available action for the job module: index Displays the records of the table show Displays the fields and their values for a given

record new Displays a form to create a new record create Creates a new record edit Displays a form to edit an existing record update Updates a record according to the user

submitted values delete Deletes a given record from the table

10/8/2011

Page 20: Symfony 2

20

[email protected]

TEST MODULES You can now test the job module in a

browser: http://www.jobeet.com.localhost/frontend_dev.ph

p/job

10/8/2011

Page 21: Symfony 2

21

[email protected]

APPENDIX - YAML YAML is "a human friendly data serialization standard for

all programming languages“ YAML is a simple language to describe data (strings,

integers, dates, arrays, and hashes). Structure is shown through indentation, sequence items are denoted by a dash, and key/value pairs within a map are separated by a

colon. arrays are explicitly shown with [] and hashes with {}. symfony framework uses it extensively for its

configuration files. indentation must be done with one or more spaces,

but never with tabulations.

10/8/2011

Page 22: Symfony 2

22

[email protected]

REFERENCES http://www.symfony-project.org/jobeet/1_4/

Doctrine/en/

10/8/2011