Eloquent ORM

Post on 20-Mar-2017

40 views 1 download

Transcript of Eloquent ORM

Eloquent

Laravel

b j e c t

e l a t i o n a l

a p p e r

O

R

M

M o d e l V i e w C o n t r o l l e r

– Wikipedia

«(…) a programming technique for converting data between incompatible type systems in object-oriented programming languages.»

– Fredrik

«Salvation»

Biblioteksempel

Verdal Bibliotek

Beatles

Maskeblomstfamilien

Kråkefot

bookslibrariesVerdal Bibliotek Beatles

Tabeller

booksid title year library_id

1 Beatles 1984 1

2 Maskeblomstfamilien 2003 1

3 Gutten som ville være en av gutta 1992 2

4 Amatøren 1977 2

librariesid name

1 Verdal Bibliotek

2 Levanger Bibliotek

libraries books

SQL

booksid title year library_id

1 Beatles 1984 1

2 Maskeblomstfamilien 2003 1

3 Gutten som ville være en av gutta 1992 2

4 Amatøren 1977 2

librariesid name

1 Verdal Bibliotek

2 Levanger Bibliotek

SELECT * FROM books WHERE library_id = 1

SQL

booksid title year library_id

1 Beatles 1984 1

2 Maskeblomstfamilien 2003 1

3 Gutten som ville være en av gutta 1992 2

4 Amatøren 1977 2

librariesid name

1 Verdal Bibliotek

2 Levanger Bibliotek

SELECT * FROM libraries l, books bWHERE l.id = b.library_idAND l.id = 2

SQL

booksid title year library_id

1 Beatles 1984 1

2 Maskeblomstfamilien 2003 1

3 Gutten som ville være en av gutta 1992 2

4 Amatøren 1977 2

librariesid name

1 Verdal Bibliotek

2 Levanger Bibliotek

SELECT l.name, COUNT(*) numOfBooks FROM books bINNER JOIN libraries lON b.library_id = l.idWHERE b.id IN ( SELECT id FROM books WHERE year > 1980)GROUP BY l.idORDER BY name

name numOfBooks

Levanger Bibliotek 1

Verdal Bibliotek 2

To Entiteter

bookslibraries

Titteltekst

• Masse SQL å holde styr på

• Krevende å skrive spørringer

• Utfordrende å drifte

• Potensiale for SQL-injection

Problemet

SQL-injection

mysql_query('INSERT INTO students (name) VALUES (' . $name . ')');

INSERT INTO students (name) VALUES ('Robert'); DROP TABLE students;--');

Robert'); DROP TABLE students;--

xkcdExploits of a Mom

Putting the M in MVC

class Library extends Model { public function () { return $this->hasMany( ::class); }}

books

Book

L i b r a r y

Putting the M in MVC

class Book extends Model{ public function () { return $this->belongsTo( ::class); }}

library

Library

B o o k

Find

Book::find(1);

SELECT * FROM books WHERE id = 1;

S Q L

E l o q u e n t

Where

Book::where(‘library_id’, 1)->get();

Library::find(1)->books;

SELECT * FROM books WHERE library_id = 1;

S Q L

E l o q u e n t

Has & WhereBetween

Book::has('library')->whereBetween('year', [1980, 2000])->get();

SELECT * FROM `books` WHERE EXISTS ( SELECT * FROM `libraries` WHERE `libraries`.`id` = `books`.`library_id`) AND `year` BETWEEN 1980 AND 2000;

S Q L

E l o q u e n t

Spørring på Relasjoner

Library::find(2)->books()->where('year', '<', 2000)->get();

SELECT * FROM `books` WHERE `books`.`library_id` = 2AND `books`.`library_id` IS not null AND `year` < 2000;

S Q L

E l o q u e n t

Method Chaining

Part::find(1)->car->factory->manufacturer->manager->address;

Create & Update

$book = Book::find(1);

$book->year = 1985;

$book->save();

$book = new Book;

$book->title = 'Halvbroren';$book->year = 2001;

$book->save();

C r e a t e U p d a t e

Destroy

$book = Book::find(1);

$book->destroy();

Titteltekst

• Eliminerer i stor grad SQL

• Portabilitet

• Objektorientert Query-språk

• SQL-injection usannsynlig

• Holder oss tørr (dry)

Fordeler

Takk!