Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

The model: Doctrine 2

From: MVC Frameworks for Building PHP Web Applications

Video: The model: Doctrine 2

No matter what framework you use, a large amount of application design and tasks ultimately involves working with the data layer. Symfony2 comes bundled with the Doctrine Object Relational Mapping, or ORM, library. While Symfony doesn't require using Doctrine, it's definitely a tool worth learning. Let's have a look at how we can handle data with Symfony and Doctrine. The first thing we need to do for our database to work is go back to the config.php in the web root of our Symfony directory. From this page, you'll want to go to configure your Symfony application online. This will allow us to enter our database configuration information. So make sure to put the correct information to connect your database. In this case, we're using MySQL at localhost. You can set the name of your database to whatever you like. Symfony is fine. In the case of MAMP, it resides on port 8889. So I want to make sure to reference that port, and I want to enter the password, so that it can have access.

The model: Doctrine 2

No matter what framework you use, a large amount of application design and tasks ultimately involves working with the data layer. Symfony2 comes bundled with the Doctrine Object Relational Mapping, or ORM, library. While Symfony doesn't require using Doctrine, it's definitely a tool worth learning. Let's have a look at how we can handle data with Symfony and Doctrine. The first thing we need to do for our database to work is go back to the config.php in the web root of our Symfony directory. From this page, you'll want to go to configure your Symfony application online. This will allow us to enter our database configuration information. So make sure to put the correct information to connect your database. In this case, we're using MySQL at localhost. You can set the name of your database to whatever you like. Symfony is fine. In the case of MAMP, it resides on port 8889. So I want to make sure to reference that port, and I want to enter the password, so that it can have access.

Go ahead and click Next Step. This will generate a global secret. Among other things, this will make sure that you can't get hacked from cross-site request forgeries. If you want to generate a new secret, you can click the Generate key; otherwise, go ahead and go to the Next Step. What this will do is create a number of parameter values and set them into your parameters.ini config file. Now your application can connect to the database. There's one other configuration thing that we want to do to make sure that Symfony knows where and how to find your database.

If you open the config.yml, this contains a number of different configurations. I'm going to add another line called unix_socket, and I'm going to point to the mysql.sock file that's located in the MAMP directory. This will ensure that Symfony is connecting to the correct version of MySQL on my computer. If you're not using MAMP, make sure that this is going to point to the correct mysql.sock. Using the command-line, I can generate my database directly. The first thing I want to do is go into my Symfony folder, which is in the Apache web root.

Once I'm in here, I can use the PHP console to call on my special Doctrine functionality. So I'll specify doctrine:database:create, and this will create the database for me. So you can see, "Created database for connection named Symfony." So I now have an empty database. The next thing is to generate an entity, and there's two pieces to the entity. One is going to be the class itself, which is going to have all those fields that match my database table, and from there, I can actually generate the schema itself.

So, let's go ahead and type in PHP app/ console, and again we're going to use Doctrine. But this time we're going to use the Generate command, and we're going to specify that it's an entity that we want to generate. We're going to give the entity a name. I'm going to call it AcmeJoinBundle, which is the ID of the bundle we've been working on, :MailList. Make sure you close that quote. This will open the entity generator. So we can follow generally the defaults.

We're going to create it with the same shortcut name as we defined. The format we're going to use to configure, let's go ahead and keep it Annotation, but note that you could also use YAML or XML or PHP. So now what we're going to do is we're going to enter all of the fields that we need to represent our data. You don't need to worry about the ID as it will auto-generate that for you. So the first field that I'm going to add is first_name. I can use the default, which is a string, and the length can be 100 characters. I'm going to go ahead and generate the same for the last_name, and the email, address, the state_code, which is also a string. We'll give it 10 characters in case we have other countries or things we want to include. Zip and postal code like this, string, username. Oops, I keep doing that. password, bio, interests, and the last one is going to be the number of tours the user has gone on. We're going to store that just as a string.

When you're done, go ahead and hit Enter again. It will ask us if we want to generate an empty repository class, and we don't need to do that. Then it'll ask to confirm that you want to generate this. Go ahead and click Yes. So it generated the code, and let's go have a look at what got generated. So we're going to want to go into our Bundle, and notice there's a directory here called Entity. If you open it up, you'll see there's a MailList.php and inside of here, you can see that it's using the Doctrine mapping, and it also has these notations that define the tables, that define the columns. In this case, it's an ID that's an auto-generated integer. We have the first_name and the last_name, and they all have the correct data types as we entered them.

Those notations are what can be used by the Doctrine engine to actually generate your database schema. So let's go ahead and do that. The way we do that is use the php app/console again and call on doctrine. This time we're going to use schema and we're going to use update, and make sure to do the force. It's not always necessary, but this will make sure that if the table already exists, it will re-create it in the fashion you want it, based on the new version of the entity.

If this should happen to you when you're trying to generate your schema, don't worry. This is a pretty common event. Basically, what's happening, you're most likely on Mac and it's looking for a php.ini file that has a setting that tells it what time zone it's in. Because by default PHP doesn't load an INI file, it's not there, so when it tries to do something with this daytime object--which it's doing behind the scenes to generate the schema--it's getting an error. The way you can fix this, probably the easiest way, is to go ahead and go into your TextMate and open your php.ini file from MAMP, which is going to be in Applications/MAMP/bin/php/php5.3.6, and then in the conf directory. Make sure this has it set, and you can do that by searching for date.timezone. You can see in this case mine is. It's set to America /Los_Angeles. So go ahead and save it, but this time Save As. if you use Command+Shift+G, it will open the Go To folder, and then you can go to the etc folder. Now save it as php.ini.

If you already have a file there, you'll want to make sure to rename it before you do this. So now that you've corrected that error, go ahead and use the up arrow. This will bring back the last command you entered into the terminal. Hit Enter and you can see, "Updating database schema... Database schema updated successfully!" I'm going to go into MySQL Workbench, which is an application I've downloaded so that I can see my database. And you can see I have my Symfony table here. If I open the tables, you can see that I have my MailList table, and when I open it, the fields match the information that I entered into that command line. So, implementing the model layer of the Symfony2 application is generally done using the Doctrine ORM Library.

In this unit, we discussed how to create a database, an entity object, and even generate the tables all by using a command-line tool. But this is really just a brief glimpse into all that Doctrine can do. We'll take another peek into Doctrine when we look at how we can use it to save our data to the database.

Show transcript

This video is part of

Image for MVC Frameworks for Building PHP Web Applications
MVC Frameworks for Building PHP Web Applications

51 video lessons · 20337 viewers

Drew Falkman
Author

 
Expand all | Collapse all
  1. 4m 58s
    1. Welcome
      51s
    2. What you should know
      2m 9s
    3. Using the exercise files
      1m 19s
    4. Installing Apache, MySQL, and PHP
      39s
  2. 17m 42s
    1. Why use a framework?
      2m 59s
    2. MVC is the MVP
      2m 1s
    3. Framework concepts
      3m 19s
    4. Finding the right solution
      2m 58s
    5. Comparing Zend, Symfony, CakePHP, and CodeIgniter
      6m 25s
  3. 15m 49s
    1. Intro to Zend 1.11 and the 2.0 beta
      4m 2s
    2. Zend concepts, advantages, and issues
      4m 11s
    3. Zend components
      3m 24s
    4. Zend flow and architecture
      4m 12s
  4. 43m 53s
    1. Downloading and setting up Zend Framework
      6m 30s
    2. The development environment
      2m 48s
    3. Creating a project
      4m 36s
    4. The view
      9m 1s
    5. The model
      7m 36s
    6. Controller, form, and actions
      10m 17s
    7. Taking it from here
      3m 5s
  5. 11m 57s
    1. Intro to Symfony 2.0
      2m 44s
    2. Symfony concepts, advantages, and issues
      3m 7s
    3. Symfony components
      1m 38s
    4. Symfony flow and architecture
      4m 28s
  6. 35m 25s
    1. Downloading and setting up Symfony
      4m 36s
    2. Routing, controllers, and bundles
      6m 50s
    3. The view: Twig and templates
      7m 15s
    4. The model: Doctrine 2
      7m 53s
    5. Form and actions
      6m 43s
    6. Taking it from here
      2m 8s
  7. 11m 40s
    1. Intro to CodeIgniter 2.1
      2m 9s
    2. CodeIgniter concepts, advantages, and issues
      2m 51s
    3. CodeIgniter classes
      1m 50s
    4. CodeIgniter flow and architecture
      4m 50s
  8. 32m 8s
    1. Downloading and setting up CodeIgniter
      2m 0s
    2. The model
      6m 7s
    3. The controller
      5m 30s
    4. The view
      7m 19s
    5. Forms and making it all work
      9m 24s
    6. Taking it from here
      1m 48s
  9. 12m 48s
    1. Intro to CakePHP 2.0
      2m 19s
    2. CakePHP concepts, advantages, and issues
      3m 14s
    3. CakePHP components
      2m 32s
    4. CakePHP flow and architecture
      4m 43s
  10. 28m 15s
    1. Downloading and setting up CakePHP
      2m 22s
    2. The model
      4m 9s
    3. The controller
      5m 0s
    4. The view and the FormHelper
      8m 53s
    5. Making it all work
      5m 59s
    6. Taking it from here
      1m 52s
  11. 36s
    1. Goodbye
      36s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed MVC Frameworks for Building PHP Web Applications.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.