# Welcome to Database ORM Demo

Here you can learn about how to do database Operation with DooPHP.

Please refer to the API for DooSqlMagic, DooDbExpression and DooModelGen class. Models are generated by accessing http://yourappurl.com/gen_model using DooModelGen. The Model class doesn't need to extend any parent class. You can download the backup SQL for the MySQL database here.

For debugging purpose, it is recommended that you use the Database profiling tools with your data centric app. A profiler viewer tool is under development currently which gives you a better view of your log files.

The database consist of 6 tables as below:

The Tables Relationship

Tables relationship are defined in a single file, db.conf.php

$dbmap[Table A]['has_one'][Table B] = array('foreign_key'=> Table B's column that links to Table A )
$dbmap[Table B]['belongs_to'][Table A] = array('foreign_key'=> Table A's column where Table B links to )

    //Food relationship
    $dbmap['Food']['belongs_to']['FoodType'] = array('foreign_key'=>'id');
    $dbmap['Food']['has_many']['Article'] = array('foreign_key'=>'food_id');
    $dbmap['Food']['has_one']['Recipe'] = array('foreign_key'=>'food_id');
    $dbmap['Food']['has_many']['Ingredient'] = array('foreign_key'=>'food_id', 'through'=>'food_has_ingredient');

    //Food Type
    $dbmap['FoodType']['has_many']['Food'] = array('foreign_key'=>'food_type_id');

    //Article
    $dbmap['Article']['belongs_to']['Food'] = array('foreign_key'=>'id');

    //Recipe
    $dbmap['Recipe']['belongs_to']['Food'] = array('foreign_key'=>'id');

    //Ingredient
    $dbmap['Ingredient']['has_many']['Food'] = array('foreign_key'=>'ingredient_id', 'through'=>'food_has_ingredient');

Test drive ORM Find/Relate operations

    //list all food
    /food/all

    //Each food belongs to a type
    /food_with_type

    //Same as above but only type with at least a matched food is return
    /food_with_type/matched

    //The reversed of the link above, 1 Type has many food
    /type_with_food

    //Many to many relationship, food & ingredient is link with table food_has_ingredient
    /food_with_ingredients

    //The reversed of the link above
    /ingredients_with_food

    //An advanced query with 3 related Models linked, a Food is return with its Type & Articles
    /nasilemak_type_&_article

    //Search for a food and return the food with its Type
    /food_&_type_by_name/Nasi Lemak

    //Search for a food and return the food with its Type
    /recipe/Nasi Lemak

    //Search for a food by its Id
    /food_by_id/7

    //get articles by food name
    /article_by_food/Nasi Lemak

    //get articles by food name and sort it by creation date Descendingly
    /article_by_food_desc/Nasi Lemak

    //get articles (not drafts) by food name and sort it by creation date Descendingly
    /article_by_food_desc_published/Nasi Lemak

Test drive ORM Insert/Relate Insert operations

    //add a new Food, replace the params with your values
    /food/insert/:foodtype/:foodname/:location/:desc

    //add a new Food, replace the params with your values
    /food/insert/:foodtype/:foodname/:location/:desc

    //add a new Food along with an associated Ingredient, many to many insert (no repeats of value)
    /food/insert/:foodtype/:foodname/:location/:desc/:ingredient

There's more! However I leave it to you to discover.

BACK TO TOP