Viewers: in countries Watching now:
In this course, author Drew Falkman tours four open-source PHP frameworks: Zend, Symfony, CakePHP, and CodeIgniter. The course covers basic framework concepts; explores the Model-View-Controller (MVC) pattern as a means of simplifying, organizing, and maintaining code; and details the ins and outs of developing basic applications in each of the four frameworks.
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 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 generate an empty repository class, and we don't need to do that. Then it'll ask 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.
There are currently no FAQs about MVC Frameworks for Building PHP Web Applications (2012).
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.