Enough talking…let's see what this thing can do! - HighEdWeb 2012

candlewhynotData Management

Jan 31, 2013 (4 years and 6 months ago)

239 views

Get Your Grubby Hands
Out of the Database!

Jason Pitoniak (@kodiak324)

Technical Team Lead

Enterprise Web Applications Development

Rochester Institute of Technology

Object
-
Relational Mapping


A technique for converting between incompatible
type
-
systems in object
-
oriented programming.






-
Wikipedia



Basically, ORM is a method for converting between an
application’s objects and an unrelated storage system,
such as a relational database.


Conversion should be transparent to the application
layer.

Why ORM?


Centralizes all access to external systems.


Provides for efficiencies such as abstraction

and code
re
-
use.


Removes storage platform dependencies from
application logic.


Write application code instead of SQL.


Makes all data native to the application, regardless of
how it is stored.


BECAUSE DATABASES AREN’T FUN!

ORM Support


All current, major development platforms have ORM
tools available


Tend to be an integral part of Model
-
View
-
Controller
systems


Some 20+ ORM systems available for PHP alone!

But…


ORMs often require lots of configuration.


Some ORMs require manual database configuration.


ORMs require developers to have a solid
understanding of their data structures before program
functionality is developed.


Many of the efficiencies of standard SQL are lost in the
ORM translation layer.

Introducing
RedBeanPHP


http://
www.redbeanphp.com
/



Yet another Object
-
Relational Mapping tool for PHP


But…


RedBean

is different!


Zero configuration


Fluid schemas


I
nterpolation of data to determine column types


Strong but optional model architecture for enforcing
business rules.


Frozen mode for fast, secure production
-
quality
applications.


What makes
RedBean

different?


RedBean

lets developers focus on functionality
without worrying about data structure.


Perfect for rapid prototyping but full
-
featured for
production
-
class use.

Support


Designed to work with PHP 5.3, but should run under
5.2 and 5.4 as well.


Uses PDO for all database access.


Support for SQLite, MySQL,
PostgreSQL
, and CUBRID
out of the box


Support for Oracle with a third
-
party driver


Additional databases can be supported by writing a
simple driver class.


Security


All database queries use prepared statements.


Open source (GPL 2 or BSD


your choice!)


Frequent new releases


Single developer but active user community

The Rules


Bean types and property names must be valid both as
PHP variable names and as table and column names
with the database being used.


Names can consist of lowercase letters or numbers
only; no uppercase letters, underscores, or other
characters are allowed.


Property names must start with a letter (PHP rule).


Property names starting with “own” or “shared” have
special meaning.

Demo

Enough talking…let’s see what this thing can do!

More on Models


There are six events that can be acted on with a model:


update()


called at the start of R::store()


a
fter_update
()


called when R::store() is done writing
data


open()


called after data is fetched with R::load()


d
ispense()


called as a new bean is created with
R::dispense()


d
elete()


called before a bean is deleted with R::trash()


a
fter_delete
()


called after a bean is deleted

And Even More on Models


Events have many practical uses:


Setting default or status values (i.e. timestamps).


Notifications to third
-
party services (i.e. update listserv
when membership database changes)


Form validation


Events do not have return values, but program
execution can be stopped (preventing DB updates) by
throwing exceptions.



Is It Cold In Here?

Wow,
RedBean

looks great for rapid prototyping, but I
don’t want my production schema getting changed.



After connecting to a database, just freeze it:

R::setup();

R::freeze();


This tells
RedBean

to stop trying to fit tables to the
data.


Speeds up execution because
RedBean

no longer needs
to examine the schema on every insert or update.

ReadBean

Advantages


Allows for easy rapid prototyping/development


Frozen schema mode allows for fast DB access in
production environments.


Universal


no DB
-
specific considerations to worry
about


Well supported, frequent updates, stable codebase


Disadvantages


May not be compatible with existing DB schemas.


No direct support for table prefixing.


No direct support for joins.


Easy to create messy data structures if not careful.


In many cases, SQL can more efficient (mass updates,
mass deletes, etc.)


Beans without models make code less understandable.


Dynamic model architecture causes issues with class
autoloading
.

Questions?