Tutorial – Learn PHP ORM Doctrine2 with Examples


In this tutorial I will show you basic use of PHP ORM Doctrine2 using sample project, so you just need to clone the existing source code from my public bitbucket repository.

This source code based on http://doctrine-orm.readthedocs.org/en/latest/tutorials/getting-started.html

  1. Clone existing sample source code :
    • yusuf@yusuf-VirtualBox:/var/www/html$ sudo git clone https://yoesoff@bitbucket.org/yoesoff/doctrine-aja.git
    • yusuf@yusuf-VirtualBox:/var/www/html$ ls
      belajar doctrine-aja index.html info.php test.php testx
    • yusuf@yusuf-VirtualBox:/var/www/html$ cd ./doctrine-aja/
    • yusuf@yusuf-VirtualBox:/var/www/html/doctrine-aja$ ls
      bootstrap.php composer.json create_product.php list_bugs_array.php list_products.php show_bug.php update_product.php
      cli-config.php composer.lock create_user.php list_bugs.php products.php show_product.php web
      close_bug.php create_bug.php dashboard.php list_bugs_repository.php readme.md src
  2. Ok now you already have the source code at /var/www/html/doctrine-aja (or another folder you just created).
  3. now let’s go to inside of /var/www/html/doctrine-aja
  4. then run composer install to install all dependencies that we need
    • yusuf@yusuf-VirtualBox:/var/www/html/doctrine-aja$ sudo composer install
  5. Now setup the database
    1. create the datase ‘doctrine-aja’
    2. setup the database connection
      • yusuf@yusuf-VirtualBox:/var/www/html/doctrine-aja$ vi bootstrap.php 
      • and :
      • yusuf@yusuf-VirtualBox:/var/www/html/doctrine-aja$ vi ./web/function/bootstrap.php
      • *yes this is not good practice to have more than one bootstrap.php (why so serious…?)
      • bootstrap.php must be configured with your own database username and password
        • $conn = array(
          ‘dbname’ => ‘doctrine-aja’,
            ‘user’ => ‘rootii’,
              ‘password’ => ‘rootii’,
          ‘host’ => ‘localhost’,
          ‘driver’ => ‘pdo_mysql’,
          );
    3. oke now I assume that database has been created and connected to your code.
  6. Now generate the tables
    • we will use doctrine tool to generate the tables, so make sure you can execute the doctrine commands
      • yusuf@yusuf-VirtualBox:/var/www/html/doctrine-aja$ ./vendor/bin/doctrine
    • ok now let generate the tables
      • yusuf@yusuf-VirtualBox:/var/www/html/doctrine-aja$ ./vendor/bin/doctrine orm:schema-tool:create
    • ok now all tables have been generated in your ‘doctrine-aja‘ database.
  7. Now let show you how to use this (using linux terminal) :
    • Create Usersyusuf@yusuf-VirtualBox:/var/www/html/doctrine-aja$ php create_user.php Yoesoff
      Created User with ID 1
      yusuf@yusuf-VirtualBox:/var/www/html/doctrine-aja$ php create_user.php Yusuf
      Created User with ID 2
      yusuf@yusuf-VirtualBox:/var/www/html/doctrine-aja$ php create_user.php ‘Yuka Chan’
      Created User with ID 3
    • List of usersyusuf@yusuf-VirtualBox:/var/www/html/doctrine-aja$ php list_users.php
      – Yuka Chan
      – Yusuf
      – Yoesoff
    • Create Products
      yusuf@yusuf-VirtualBox:/var/www/html/doctrine-aja$ php create_product.php “Mz Offixe”
      Created Product with ID 1
      yusuf@yusuf-VirtualBox:/var/www/html/doctrine-aja$ php create_product.php “Xorel Drawz”
      Created Product with ID 2
      yusuf@yusuf-VirtualBox:/var/www/html/doctrine-aja$ php create_product.php “Adobest Poto Crop”
      Created Product with ID 3
    • List of Products

      yusuf@yusuf-VirtualBox:/var/www/html/doctrine-aja$ php list_products.php
      -Mz Offixe, bugs(0)
      -Xorel Drawz, bugs(0)
      -Adobest Poto Crop, bugs(0)

    • Create Bugs

      yusuf@yusuf-VirtualBox:/var/www/html/doctrine-aja$ sudo vi create_bug.php
      [sudo] password for yusuf:
      yusuf@yusuf-VirtualBox:/var/www/html/doctrine-aja$ php create_bug.php 1 3 1 “user cannot close main window”
      Thank for reporting new bug(s), Your new Bug Id: 1
      yusuf@yusuf-VirtualBox:/var/www/html/doctrine-aja$ php create_bug.php 1 3 2 “user cannot save as file properly”
      Thank for reporting new bug(s), Your new Bug Id: 2
      yusuf@yusuf-VirtualBox:/var/www/html/doctrine-aja$ php create_bug.php 1 3 3 “documentation is not accessible”
      Thank for reporting new bug(s), Your new Bug Id: 3

      *php create_bug.php reporter_id engineer_id product_id description

    • List of Bugs

      yusuf@yusuf-VirtualBox:/var/www/html/doctrine-aja$ php list_bugs.php
      3 – 10.08.2015
      Reported by: Yoesoff
      Assigned to: Yuka Chan
      – Product: Adobest Poto Crop

      2 – 10.08.2015
      Reported by: Yoesoff
      Assigned to: Yuka Chan
      – Product: Xorel Drawz

      1 – 10.08.2015
      Reported by: Yoesoff
      Assigned to: Yuka Chan
      – Product: Mz Offixe

    • add existing bug to existing product

      yusuf@yusuf-VirtualBox:/var/www/html/belajar/doctrine-aja$ php add_bug_to_product.php 1 3 1 3
      Thank for reporting new bug(s), Your Bug Id: 1

      *php add_bug_to_product.php reporter_id engineer_id bug_id product_id

    • add existing product to existing bug

      use “add existing bug to existing product”

  8. Now let’s try on the browser
    1. open http://localhost/doctrine-aja/web/
  9. to be continue

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s