Database ORM Example

There methods find, relate, insert, update, relatedInsert, relatedUpdate, insert_attributes, update_attributes, and delete are the main operations in DooSqlMagic. And it should be sufficient for you to do DBRMS operations wihtout writing SQL manually.

# Enable sql tracking for debug purpose, remove it in Production mode
Doo::db()->sql_tracking = true;

# A simple Find all
Doo::db()->find('Food');

# A simple relate search
Doo::db()->relate('Food', 'FoodType');

# A simple relate search that ONLY returns FoodType that has Food
Doo::db()->relate('FoodType','Food', array('match'=>true));

# A simple relate search that ONLY returns FoodType that has Food
Doo::db()->relate('FoodType','Food', array('match'=>true));

# Find a record, this is exposed to sql injection and doesn't handle any escaping/quoting 
Doo::db()->find('Food', array(
                            'limit'=>1,
                            'where'=>'food.name = ' . $this->params['foodname'],
                    ));

# Instead, Do this! Pass array to param
Doo::db()->find('Food', array(
                            'limit'=>1,
                            'where'=>'food.name = ?',
                            'param'=> array( $this->params['foodname'] )
                    ));

# Or create the Model object and search. Escaping/quoting is automatically done
Doo::loadModel('Food');
$food = new Food;
$food->name = $this->params['foodname'];
Doo::db()->find($food, array('limit'=>1));
        

Go ahead and download the code to learn more!

BACK TO TOP