August 27, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Doctrine 2 PHP ORM: Table Schemas and Databases, Page 2

  • March 2, 2011
  • By Jason Gilmore
  • Send Email »
  • More Articles »

Generating the Table Schemas in the Doctrine 2 PHP ORM

With the persistence definitions in place, you can use the Doctrine console to generate the table schemas. The sandbox project uses the SQLite database by default, which is a file-based database solution. Generate the database and table schemas using the following command:

$ doctrine orm:schema-tool:create
ATTENTION: This operation should not be executed in a production environment.

Creating database schema…
Database schema created successfully!

View the directory contents and you'll find a file named database.sqlite. Next, we'll create a PHP script that connects to this SQLite database and uses Doctrine to interact with the tables via the persistent classes.

Talking to the Database Using the Doctrine 2 PHP ORM

The sandbox project's index.php file contains all of the configuration-related code you'll need to make the persistent classes and Doctrine library available to your website. Open this file and navigate to the bottom, where you'll find the comment PUT YOUR TEST CODE BELOW. Below this comment you'll see that the two classes found in the Entities directory are instantiated:

$user = new User;
$address = new Address;

Modify the code to look like this:

$user = new User;
$user->setName('Superman');

$address = new Address;
$address->setStreet('Fortress of Solitude');
$em->persist($address);

$user->setAddress($address);

$em->persist($user);
$em->flush();

Save the file and then execute it within the browser. Before doing so, make sure the database.sqlite file and its parent directory are writable by your Web server. Congratulations, you've just saved your first persistent objects!

While it's of course possible to retrieve the persistent data using the object-oriented interface (I'll show you how to do this in a moment), I wanted to first show you how to use the Doctrine console to quickly query the database. Execute the following command:

$ ./doctrine dbal:run-sql "select * from users"
array(1) {
[0]=>
array(3) {
["id"]=>
string(1) "1"
["address_id"]=>
string(1) "1"
["name"]=>
string(8) "Superman"
}
}

Now let's retrieve the data from the index.php file. Before modifying index.php though, execute the following command:

$ ./doctrine orm:generate-proxies

Proxies are used by Doctrine to facilitate the object-loading process. If you view the contents of the sandbox directory, you'll see that a new directory named Proxies has been created. Now modify index.php, replacing the previous snippet with the following:

$user = $em->getRepository('EntitiesUser')->findOneByName('Superman');

echo $user->getAddress()->getStreet();

Save index.php and reload the browser. You'll see that Fortress of Solitude is output!

About the Author

Jason Gilmore -- Contributing Editor, PHP -- is the founder of EasyPHPWebsites.com, and author of the popular book, "Easy PHP Websites with the Zend Framework". Jason is a cofounder and speaker chair of CodeMash, a nonprofit organization tasked with hosting an annual namesake developer's conference, and was a member of the 2008 MySQL Conference speaker selection board.


Tags: PHP, Object relational mapping

Originally published on http://www.developer.com.

Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel