SlideShare a Scribd company logo
1 of 44
Download to read offline
ORM & Entity
Relations
Mahmut Bayrı <mahmutbayri@yahoo.com>
ORM Nedir?
Object-relational mapping
Object to Relational Mapping veriyi
uyumlu olmayan nesneye yönelik
programlama dili tip sistemine çevirmek
için kullanılan bir yazılım tekniğidir.
Genel Bakış
String sql = "SELECT ... FROM persons WHERE id = 10";
DbCommand cmd = new DbCommand(connection, sql);
Result res = cmd.Execute();
String name = res[0]["FIRST_NAME"];
Genel Bakış - ORM API
Person p = repository.GetPerson(10);
String name = p.getFirstName();
ORM Kullanmanın
Avantajları / Dezavantajları
ORM kullanımı zaman
kazancı sağlar
● Veri modelinizi tek bir yerde yapın, bakımı,
güncellemesi ve tekrar kullanımı daha
kolaydır.
● Sizi MCV kodlamaya zorlar, sonuçta
kodunuz daha temiz yapar.
● SQL yazmak zorunda kalmazsınız.
ORM Kütüphanesi
kullanımı daha esnektir
● Yazdığınız dile uygundur.
● DB sistemini soyutlar, istediğinizde
değiştirebilirsiniz.
● Model, uygulamanızın geri kalanıyla zayıf bağlıdır,
değiştirebilir ve başka yerlerde kullanabilirsiniz.
● OOP kullanımının avantajlarını sorunsuzca
kullanırsınız.
ORM sorunlu olabilir
● ORM kütüphaneleri kolay araçlar değildir.
● Kurulumunu yapmanız gerekir.
● Bazen büyük projelerde kendi SQL’iniz daha iyi
olabilir.
● Yeni programcılar için tuzak olabilir.
ORM Yaklaşımları
● Table Data Gateway
● Row Data Gateway
● Data Mapper
● Active Record
ORM Yaklaşımları
Table Data Gateway
Her tablo bir Table Gateway tarafından temsil edilir.
ORM Yaklaşımları
Row Data Gateway
Her örnek query yoluyla dönen bir satıra karşılık gelir.
ORM Yaklaşımları
Data Mapper
Kalıcı saklanacak yer ile domain model ayrıştırılır.
ORM Yaklaşımları
Active Record
Her örnek tablodaki bir kayda karşılık gelir.
ORM Paketleri
● C++: LiteSQL, ODB
● Java: Hibernate, EclipseLink, jOOQ, ...
● .NET: Entity Framework, ..
● PHP: Doctrine, Propel, Redbean, Eloquent, ..
● Python: Django, Peewee ORM, ..
● Ruby: ActiveRecord
Eloquent ORM
Kullanışlı ve basit ActiveRecord uygulaması
Miras Özellikler
Eloquent ORM
● Builder
IlluminateDatabaseEloquentBuilder
● Collection
IlluminateDatabaseEloquentCollection
Önemli Özellikler
Eloquent ORM
● Implementations
ArrayAccess, Arrayable, Jsonable, JsonSerializable, QueueableEntity, UrlRoutable
● Events / Observers
● Hydration
● Eager Loading
Constraining Eager Loads, Lazy Eager Loading
● Query Scopes
Global Scopes, Local Scopes
ER Model
Entity-Relationship
Model
Gerçek hayattaki nesneler ve
aralarındaki ilişkileri, bağlantıları
tasvir eden bir modelleme
tekniğidir.
Barker's Notation
Performs
Entities
Barker's Notation
Relationships
Barker's Notation
one to one
one to many
many to many
Örnek 1
Barker's Notation
Örnek 2
Barker's Notation
Born in
Birth of place
Eloquent:
İlişkilendirmeler
Veritabanı tabloları çoğunlukla birbiri ile
ilişkilidir. Örneğin, bir blog gönderisi
birçok yorum içerebilir veya bir sipariş
sipariş verem kullanıcıyla ilişkili olabilir.
İlişkilendirme Tipleri
Eloquent: İlişkilendirmeler
● One To One
● One To Many
● Many To Many
● Has Many Through
● Polymorphic Relations
One To One: ER Model
Bire bir bağlantı. (1:1)
One To One: Eloquent
class User extends Model
{
public function phone()
{
return $this->hasOne(Phone::class);
}
}
class Phone extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
One To One: Kullanım
$user = User::find(1);
$user->phone()->getResults();
//object(RelationsPhone)
$phone = Phone::find(1);
$phone->user()->getResults();
//object(RelationsUser)
One To Many: ER Model
Birden çoğa bağlantı. (1:n)
One To Many: Eloquent
class User extends Model
{
public function posts()
{
return $this->hasMany(Post::class);
}
}
class Post extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
One To Many: Kullanım
$user = User::find(1);
$user->posts()->getResults();
//object(IlluminateDatabaseEloquentCollection)
$post = Post::find(1);
$post->user()->getResults();
//object(RelationsUser)
Many To Many: ER Model
Çoktan çoğa bağlantı. (n:n)
Many To Many: Eloquent
class User extends Model
{
public function roles()
{
return $this->belongsToMany(Role::class);
}
}
class Role extends Model
{
public function users()
{
return $this->belongsToMany(User::class);
}
}
Many To Many: Kullanım
$user = User::find(1);
$user->roles()->getResults();
//object(IlluminateDatabaseEloquentCollection)
$role = Role::find(1);
$role->users()->getResults();
//object(IlluminateDatabaseEloquentCollection)
Polymorphic Relations: ER
Model
Bire bir bağlantı (1:1). Permision tablosu değişik verileri saklayabilir.
Polymorphic Relations
class User extends Model
{
public function permissions()
{
return $this->morphOne(Permission::class, 'permissible');
}
}
class Permission extends Model
{
public function permissible()
{
return $this->morphTo();
}
}
Polymorphic Relations
$user = User::find(1);
$user->permissions()->getResults();
//object(RelationsPermission)
$permision = Permission::find(1);
$permision->permissible()->getResults();
//object(RelationsUser)
Has Many Through:Model
Çoktan çoğa (n:n). Service, User üzerinden Post tablosuna bağlı
Has Many Through: Eloquent
class Service extends Model
{
public function posts()
{
return $this->hasManyThrough(Post::class, User::class);
}
}
class User extends Model
{
public function posts()
{
return $this->hasMany(Post::class);
}
}
class Post extends Model
{
}
Has Many Through: Kullanım
$service = Service::find(1);
$service->posts()->getResults();
//object(IlluminateDatabaseEloquentCollection)
ORM & ER Relation Özet
Teşekkürler
Sunumdaki ilişkilerle ilgili kısımların kodları:
https://github.com/mahmutbayri/eloquent-orm-tutorial

More Related Content

Featured

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

ORM - Entitiy Relations.pdf