Love and Hate relationship between ORM and Query Builders

46

Transcript of Love and Hate relationship between ORM and Query Builders

Page 1: Love and Hate relationship between ORM and Query Builders
Page 2: Love and Hate relationship between ORM and Query Builders

Love and Hate between ORM and

Query Builders

Page 3: Love and Hate relationship between ORM and Query Builders

Myself

‣ Love Web Apps

‣ 25 years of coding

‣ Entrepreneur

‣ I buy Software

My Startup

‣ Focused on Coders

‣ Learning curve

‣ Open-source

‣ Service/Paid

About Me

Page 4: Love and Hate relationship between ORM and Query Builders

Make your first lines of code really matter.

Agile Toolkit

Page 5: Love and Hate relationship between ORM and Query Builders

Download

Page 6: Love and Hate relationship between ORM and Query Builders

Result

Page 7: Love and Hate relationship between ORM and Query Builders

Make UI better

Page 8: Love and Hate relationship between ORM and Query Builders

How

Agile UI Agile Data

Web Framework

DB

High-level Code

HTML CSSWidgets

Page 9: Love and Hate relationship between ORM and Query Builders

How

Page 10: Love and Hate relationship between ORM and Query Builders

Database Access Layer search quest

Page 11: Love and Hate relationship between ORM and Query Builders

Query

‣ Control

‣ Advanced Features

‣ Multi-Record

ORM

‣ 10+ tables

‣ SoftDelete, Audit

‣ Domain Model

Database Interaction Today

Page 12: Love and Hate relationship between ORM and Query Builders

MapperEntity Query

ORM: How it should work

Database

Business Logic

ORM

Page 13: Love and Hate relationship between ORM and Query Builders

MapperEntity SQL Query

The Reality

SQL Database

Business Logic

ORM

no ORM

Page 14: Love and Hate relationship between ORM and Query Builders

SQL Query

Why Bother…

SQL Database

Business Logic no ORM

Page 15: Love and Hate relationship between ORM and Query Builders

Love Compatibility

Page 16: Love and Hate relationship between ORM and Query Builders

Love Compatibility

Field Entity Relation Join

Raw SQL

Aliases

Query

Expression

ORM Components

Que

ry C

ompo

nent

s

Page 17: Love and Hate relationship between ORM and Query Builders

Love Compatibility

Field Entity Relation Join

Raw SQL no some no no

Aliases some some no some

Query no yes no some

Expression no no some no

ORM Components

Que

ry C

ompo

nent

s

Page 18: Love and Hate relationship between ORM and Query Builders

Business Opportunity

Page 19: Love and Hate relationship between ORM and Query Builders

Database Access Framework that combine benefits of

ORM and Queries

Agile Data

Page 20: Love and Hate relationship between ORM and Query Builders

Agile Data

Field Entity Relation Join

Raw SQL both ways both ways both ways yes

Aliases yes yes yes yes

Query both ways yes both ways both ways

Expression both ways yes both ways yes

ORM Components

Que

ry C

ompo

nent

s

Page 21: Love and Hate relationship between ORM and Query Builders

Simple DemoaddExpression

Page 22: Love and Hate relationship between ORM and Query Builders

Agile Data

Field Entity Relation Join

Raw SQL both ways both ways both ways yes

Aliases yes yes yes yes

Query both ways yes both ways both ways

Expression both ways yes both ways yes

ORM Components

Que

ry C

ompo

nent

s

Page 23: Love and Hate relationship between ORM and Query Builders

PersistenceLine Query

Agile Data

Database

Business Logic

Page 24: Love and Hate relationship between ORM and Query Builders

PersistenceLine Query

Agile Data

Database

Business Logic

Page 25: Love and Hate relationship between ORM and Query Builders

PersistenceLine Query

Agile Data

Database

Business Logic

Invoice

Page 26: Love and Hate relationship between ORM and Query Builders

Referencesand formulas

Page 27: Love and Hate relationship between ORM and Query Builders

Agile Data

Field Entity Reference Join

Raw SQL both ways both ways both ways yes

Aliases yes yes yes yes

Query both ways yes both ways both ways

Expression both ways yes both ways yes

ORM Components

Que

ry C

ompo

nent

s

Page 28: Love and Hate relationship between ORM and Query Builders

DataSet

Page 29: Love and Hate relationship between ORM and Query Builders

Project

DataSet $m = new Project($db);

Page 30: Love and Hate relationship between ORM and Query Builders

Project

$m = new Project($db); $m->addCondition( 'client_id', 1 );

client=1

Page 31: Love and Hate relationship between ORM and Query Builders

Project

$m = new Project($db); $m->addCondition( 'client_id', 1 ); $m->addCondition( 'is_cancelled', false );

client=1client=1 is_cancelled = 0

Page 32: Love and Hate relationship between ORM and Query Builders

Just a tip of an iceberg!

Page 33: Love and Hate relationship between ORM and Query Builders

Agile Data

Field Entity Relation Join

Raw SQL both ways both ways both ways yes

Aliases yes yes yes yes

Query both ways yes both ways both ways

Expression both ways yes both ways yes

ORM Components

Que

ry C

ompo

nent

s

Page 34: Love and Hate relationship between ORM and Query Builders

Entity to Query Builder

$q = $client->action('field', ['industry']);

$q->field('count(*)', 'c'); $q->group('industry');

$data = $q->get();

Mapper creates Query which you can then group and tweak for generating aggregated report

Page 35: Love and Hate relationship between ORM and Query Builders

Raw into Field

$client->addExpression( 'balance', 'coalesce([total_invoice]-[total_paid],0)' );

Define custom SQL code for your field.

Page 36: Love and Hate relationship between ORM and Query Builders

Query into Field

$client->addExpression('last_sale', function($m){ return $m->refLink('Invoice') ->setLimit(1) ->setOrder('date desc') ->action('field',['total']); }, 'type'=>'money' );

Convert Entity into Query then use inside Field / Expression.

Page 37: Love and Hate relationship between ORM and Query Builders

Expression into Field

$book->addExpression('display_name', [ '[title] (by [])', $book->refLink('author_id') ->action('field', ['name']) ]);

Fetch author's name from related entity into a new Expression

Page 38: Love and Hate relationship between ORM and Query Builders

Join on Expression

$invoice->hasOne('client_id', new Client()) ->addField('client_country_id', 'country_id');

$invoice->join('country', 'client_country_id') ->addFields([ 'country_short_code'=>'short_code', 'country_is_eu'=>'is_eu', 'country'=>'name'

If you need country data inside your Invoice report, but country_id

is defined through Client.

Page 39: Love and Hate relationship between ORM and Query Builders

Aliasing

Page 40: Love and Hate relationship between ORM and Query Builders

Self-referencingclass Folder extends \atk4\data\Model { public $table = 'folder'; public function init() { parent::init(); $this->addField('name');

$this->hasMany('SubFolder', [ new Folder(), 'their_field'=>'parent_id'] )->addField('count', [ 'aggregate'=>'count', 'field'=>$this->expr('*')] );

$this->hasOne('parent_id', new Folder()) ->addTitle(); } }

Page 41: Love and Hate relationship between ORM and Query Builders

Unique Aliases

select `id`,`name`,

(select count(*) from `folder` `S` where `parent_id` = `folder`.`id`) `count`,

`folder`.`parent_id`,

(select `name` from `folder` `p` where `id` = `folder`.`parent_id`) `parent` from `folder`

Page 42: Love and Hate relationship between ORM and Query Builders

Union

$q_inv = $client->ref('Invoice') ->action('field',['date','total']); $q_pay = $client->ref('Payment') ->action('field',['date','paid']);

foreach($db->expr( 'select * from ([] UNION []) u1', $q_inv, $q_pay ) as $row) { /// }

Page 43: Love and Hate relationship between ORM and Query Builders

And the best part..

Page 44: Love and Hate relationship between ORM and Query Builders

Deep Traversal

$author->withID(20) ->ref('App') ->ref('Review') ->ref('user_id') ->ref('country_id') ->action('field', ['name']) ->field('count(*)') ->group('name') ->get(); // 1 QUERY!!

There are only 2 basic relation type. OneToMany. OneToOne. BUT!!

Page 45: Love and Hate relationship between ORM and Query Builders

git.io/ad

✓Works in any framework / PHP app ✓ Lightweight and Agile ✓ Integrates with UI frameworks (like ATK) ✓ Commercial Support

Agile Data is Open-Source

Page 46: Love and Hate relationship between ORM and Query Builders

✓ ACL, Audit, Undo, Logging, .. ✓ File Management ✓ Import/Export utility ✓ RestAPI server

Commercial Extensions

git.io/ad