Eloquent ORM

26
Eloquent

Transcript of Eloquent ORM

Page 1: Eloquent ORM

Eloquent

Page 2: Eloquent ORM

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

Page 3: Eloquent ORM

– Wikipedia

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

Page 4: Eloquent ORM

– Fredrik

«Salvation»

Page 5: Eloquent ORM

Biblioteksempel

Verdal Bibliotek

Beatles

Maskeblomstfamilien

Page 6: Eloquent ORM

Kråkefot

bookslibrariesVerdal Bibliotek Beatles

Page 7: Eloquent ORM

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

Page 8: Eloquent ORM

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

Page 9: Eloquent ORM

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

Page 10: Eloquent ORM

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

Page 11: Eloquent ORM

To Entiteter

bookslibraries

Page 12: Eloquent ORM
Page 13: Eloquent ORM

Titteltekst

• Masse SQL å holde styr på

• Krevende å skrive spørringer

• Utfordrende å drifte

• Potensiale for SQL-injection

Problemet

Page 14: Eloquent ORM

SQL-injection

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

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

Robert'); DROP TABLE students;--

Page 15: Eloquent ORM

xkcdExploits of a Mom

Page 16: Eloquent ORM

Putting the M in MVC

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

books

Book

L i b r a r y

Page 17: Eloquent ORM

Putting the M in MVC

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

library

Library

B o o k

Page 18: Eloquent ORM

Find

Book::find(1);

SELECT * FROM books WHERE id = 1;

S Q L

E l o q u e n t

Page 19: Eloquent ORM

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

Page 20: Eloquent ORM

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

Page 21: Eloquent ORM

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

Page 22: Eloquent ORM

Method Chaining

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

Page 23: Eloquent ORM

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

Page 24: Eloquent ORM

Destroy

$book = Book::find(1);

$book->destroy();

Page 25: Eloquent ORM

Titteltekst

• Eliminerer i stor grad SQL

• Portabilitet

• Objektorientert Query-språk

• SQL-injection usannsynlig

• Holder oss tørr (dry)

Fordeler

Page 26: Eloquent ORM

Takk!