Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Who am I A Dutchy in Kiwiland October 12th, 2016 • Wellington • Simon Erkelens ● Hello, my name is Simon ● I’m a software developer ● I work for SilverStripe ● Ninja Unicorns ● Twitter: @Firesphere ● GitHub: https://github.com/firesphere ● These slides will be on https://speakerdeck.com/firesphere ● Dutch ● I have a cat! (Her name is Sylvester Marika) ● You know it’s me, when you see Hans ● Don’t bother trying to pronounce my surname ● I’m not anti-social. Just not user-friendly Hans, my RDD tool. She’s a cow Little miss Sylvester/Marika

Slide 3

Slide 3 text

Notes in advance Because I had to make some relational things October 12th, 2016 • Wellington • Simon Erkelens ● I am trying to explain ORM, therefore, I had to add some weird things that either require mapping, or not? ● I’m assuming you all have a working copy of the StripeGirls website, so you can code along, or write some code yourself ● If you don’t, ask for help getting it set up correctly. We’re here to help and learn ● I have a Dutch accent. Sorry, can’t do much about that ● This is a bit technical. If you don’t understand, don’t be afraid to ask ● I have a whole set of images I often use in presentations. If you’ve seen one of my presentations before, you might recognise some of them

Slide 4

Slide 4 text

A dive into (SilverStripe) ORM October 12th, 2016 • Wellington • Simon Erkelens Because humans are developers too https://twitter.com/Una/status/743382603210891264

Slide 5

Slide 5 text

ORM Object Relational Mapping October 12th, 2016 • Wellington • Simon Erkelens ● What is ORM? ● Object Oriented ● Relational ● Mapping ● ORM is not Database! ● ORM applies Relational logic to given information ○ It does not have to come from the database! (Although it often does) ● The relational logic is defined by the Mapping given or created ● The mapping is defined in the Object itself

Slide 6

Slide 6 text

DRY Why use an ORM? October 12th, 2016 • Wellington • Simon Erkelens ● Don’t ● Don’t again ● Repeat ● Repeat again ● Yourself ● Yourself again ● How not to do it ● Stuff it all in a single object ● Stuff everything in a single object

Slide 7

Slide 7 text

So, what’s the big deal? Object Relational Mapping October 12th, 2016 • Wellington • Simon Erkelens ● Efficiency ● Reusability ● Readability ● Independently readable ● Applicable to other objects if needed ● Neatly grouped (in the database) ● Object Oriented ● Relational ● Mappings The opposite of DRY is not as compelling in real life

Slide 8

Slide 8 text

How not to do it Please don’t do this? October 12th, 2016 • Wellington • Simon Erkelens class StripeGirl extends DataObject { private static $db = array( 'Name' => 'Varchar(255)', 'Email' => 'Varchar(255)', 'Address' => 'Text', ); }

Slide 9

Slide 9 text

The only thing worse is serialisation Serialising user input is like opening your backend to the world. October 12th, 2016 • Wellington • Simon Erkelens https://www.owasp.org/images/8/8c/RemoteCodeExecutionInWordPress-OW ASPBeNeLux-Tom_Van_Goethem.pdf Yep, serialisation is that bad.

Slide 10

Slide 10 text

Better…. (Sorry, couldn’t get it bigger on screen) October 12th, 2016 • Wellington • Simon Erkelens Did I say “RELATIONAL” already? Spot the error class StripeGirl extends DataObject { private static $db = array( 'Name' => 'Varchar(255)', 'Email' => 'Varchar(255)', 'Relationship' => 'Varchar(255)', 'Street' => 'Varchar(255)', 'Number' => 'Int', 'Area' => 'Varchar(255)', 'City' => 'Varchar(255)', 'Country' => 'Varchar(255)', 'MailingStreet' => 'Varchar(255)', 'MailingNumber' => 'Int', 'MailingArea' => 'Varchar(255)', 'MailingCity' => 'Varchar(255)', 'MailingCountry' => 'Varchar(255)', 'HomePhone' => 'Varchar(15)', 'WorkPhone' => 'Varchar(15)', 'MobilePhone' => 'Varchar(15)', );

Slide 11

Slide 11 text

Almost, but what about relations? I mean…. “RELATIONAL” is part of ORM October 12th, 2016 • Wellington • Simon Erkelens ● Indeed, relations are missing => thus, no option to get M for Mapping yet ● What can be made into a relation in the previous object declaration ● Address? ● Phone numbers? ● Relationship status? ● What about Gender? ● Or country? ○ I might be over-abstracting here Unicorns are awesome by the way

Slide 12

Slide 12 text

So, what relations to apply Relations are awesome (usually) October 12th, 2016 • Wellington • Simon Erkelens ● People live at the same place ● Phone numbers aren’t groupable ● Relationship status is (a) t(h)ough (one) ● And first names are ● Speed versus reusability

Slide 13

Slide 13 text

So, what relations to apply Relations are awesome (usually) October 12th, 2016 • Wellington • Simon Erkelens ● People live at the same place ● Some of you have roommates, right? ● Phone numbers aren’t groupable ● Relationship status is (a) t(h)ough (one) ● And first names are ● Speed versus reusability

Slide 14

Slide 14 text

So, what relations to apply Relations are awesome (usually) October 12th, 2016 • Wellington • Simon Erkelens ● People live at the same place ● Some of you have roommates, right? ● Phone numbers aren’t groupable ● Relationship status is (a) t(h)ough (one) ○ But is it sensible? ● And first names are ● Speed versus reusability

Slide 15

Slide 15 text

So, what relations to apply Relations are awesome (usually) October 12th, 2016 • Wellington • Simon Erkelens ● People live at the same place ● Some of you have roommates, right? ● Phone numbers aren’t groupable ● Relationship status is (a) t(h)ough (one) ○ But is it sensible? ● And first names are ○ Do you think that’s sensible? ● Speed versus reusability

Slide 16

Slide 16 text

So, what relations to apply Relations are awesome (usually) October 12th, 2016 • Wellington • Simon Erkelens ● People live at the same place ● Some of you have roommates, right? ● Phone numbers aren’t groupable ● Relationship status is (a) t(h)ough (one) ○ But is it sensible? ● And first names are ○ Do you think that’s sensible? ● Speed versus reusability ● Yes, it will add another database query sometimes ● Easier to use in other places in your code ○ Unless you over-abstract

Slide 17

Slide 17 text

Setup a logical structure Because an ORM based system, relies on logic October 12th, 2016 • Wellington • Simon Erkelens ● Draw it out. What makes sense and what doesn’t ○ You can do this in your head usually, but sometimes drawing it out for real helps! ● Define shareable properties ● A person has an address which can be shared ● A person has a name which can’t be shared ● A relationship status is personal, but there aren’t much. What’s smart here? ● A country is a personal part of the Address

Slide 18

Slide 18 text

Setup a logical structure Because an ORM based system, relies on logic October 12th, 2016 • Wellington • Simon Erkelens ● Draw it out. What makes sense and what doesn’t ● Define shareable properties ● A person has an address which can be shared ○ That’s a useful thing. That’s Object Oriented thinking ● A person has a name which can’t be shared ● A relationship status is personal, but there aren’t much. What’s smart here? ● A country is a personal part of the Address

Slide 19

Slide 19 text

Setup a logical structure Because an ORM based system, relies on logic October 12th, 2016 • Wellington • Simon Erkelens ● Draw it out. What makes sense and what doesn’t ● Define shareable properties ● A person has an address which can be shared ○ That’s a useful thing. That’s Object Oriented thinking ● A person has a name which can’t be shared ○ So, even though there might be 10 people named Annie, it’s still unique to the person ● A relationship status is personal, but there aren’t much. What’s smart here? ● A country is a personal part of the Address

Slide 20

Slide 20 text

Setup a logical structure Because an ORM based system, relies on logic October 12th, 2016 • Wellington • Simon Erkelens ● Draw it out. What makes sense and what doesn’t ● Define shareable properties ● A person has an address which can be shared ○ That’s a useful thing. That’s Object Oriented thinking ● A person has a name which can’t be shared ○ So, even though there might be 10 people named Annie, it’s still unique to the person ● A relationship status is personal, but there aren’t much. What’s smart here? ○ Enumeration instead of a new Object is a good idea for these things ● A country is a personal part of the Address

Slide 21

Slide 21 text

Setup a logical structure Because an ORM based system, relies on logic October 12th, 2016 • Wellington • Simon Erkelens ● Draw it out. What makes sense and what doesn’t ● Define shareable properties ● A person has an address which can be shared ○ That’s a useful thing. That’s Object Oriented thinking ● A person has a name which can’t be shared ○ So, even though there might be 10 people named Annie, it’s still unique to the person ● A relationship status is personal, but there aren’t much. What’s smart here? ○ Enumeration instead of a new Object is a good idea for these things ● A country is a personal part of the Address ○ Except if you want to have additional information, e.g. used currency, as part of the country

Slide 22

Slide 22 text

October 12th, 2016 • Wellington • Simon Erkelens Credits to Randall Munroe, creator of XKCD This is XKCD 1134 Logic Boat! Or a cabbage, for that matter. The goat makes sense. Goats are fine.

Slide 23

Slide 23 text

But it’s still not ORM Mapping! October 12th, 2016 • Wellington • Simon Erkelens ● Now, there’s an Object Oriented, Relational approach ● But in the end, it’s about Mapping ● Map an address to another object ● Map a relationship status to a set of defaults ○ Or not? It completely depends on the situation ● Map a country to an address I was told to add kittens to my presentations. So, here’s the kitten.

Slide 24

Slide 24 text

What about “mapping” Map map map map map October 12th, 2016 • Wellington • Simon Erkelens ● A map is a logical pointer to a shared object, instead of duplicating ● It keeps all your objects tidy and readable ● Your code is Object Oriented and logically structured ● This pointer is indexed when the entire relation is saved into a database

Slide 25

Slide 25 text

What about “mapping” Map map map map map October 12th, 2016 • Wellington • Simon Erkelens ● A map is a logical pointer to a shared object, instead of duplicating ● It’s like saying “this person’s address is in the Address database, at point X” ○ And the address points to a country with more information about the country ● It keeps all your objects tidy and readable ● Your code is Object Oriented and logically structured ● This pointer is indexed when the entire relation is saved into a database

Slide 26

Slide 26 text

Wait, you said “Indexed” A little sidetrack here October 12th, 2016 • Wellington • Simon Erkelens ● Indexes are like a link on a webpage, they (almost) instantly resolve to the mapped object ● An index is a quick way to search inside the database ● Indexes speed up your database queries a lot ● Indexes are key to what the Mapping is when data is stored in the database ● If you are interested in indexes and how they help, I need to refer you to my friend Philipp Krenn. He’s a database genius. ● https://vimeo.com/144108480

Slide 27

Slide 27 text

Mapping in SilverStripe Map map map map map October 12th, 2016 • Wellington • Simon Erkelens ● Relations ● has_one ● belongs_to ● has_many ● many_many ● belongs_many_many ● Don’t think yourself, let the framework do the mapping ● Understand what a mapping means in your code ● When doing $StripeGirl->Address(); You’re addressing the ORM

Slide 28

Slide 28 text

Mapping in SilverStripe Map map map map map October 12th, 2016 • Wellington • Simon Erkelens ● Relations ● has_one ● belongs_to ● has_many ● many_many ● belongs_many_many ● Don’t think yourself, let the framework do the mapping ● Understand what a mapping means in your code ● Preferably, before you dive in and make mistakes ● When doing $StripeGirl->Address(); You’re addressing the ORM

Slide 29

Slide 29 text

Mapping in SilverStripe Map map map map map October 12th, 2016 • Wellington • Simon Erkelens ● Relations ● has_one ● belongs_to ● has_many ● many_many ● belongs_many_many ● Don’t think yourself, let the framework do the mapping ● Understand what a mapping means in your code ● Preferably, before you dive in and make mistakes ● When doing $StripeGirl->Address(); You’re addressing the ORM ● The SilverStripe ORM will find the StripeGirl::AddressID and fetch the related address, based on the AddressID, returning an Address object ● Or, in SilverStripe Templating engine, you can use <% with $Address %>

Slide 30

Slide 30 text

What makes SilverStripe better? There’s no perfect solution. October 12th, 2016 • Wellington • Simon Erkelens ● The mapping maps. What else do you want? ● The easy way to quickly setup your mapping configuration ● The static naming is clear, you can’t go wrong ● You don’t have to use additional code to create the mapping yourself

Slide 31

Slide 31 text

What makes SilverStripe better? There’s no perfect solution. But Stripey McMapface is quite nice! October 12th, 2016 • Wellington • Simon Erkelens ● The mapping maps. What else do you want? ● The easy way to quickly setup your mapping configuration ● The static naming is clear, you can’t go wrong ● You don’t have to use additional code to create the mapping yourself ● For example, but not limited to: ○ Annotations ○ YML ○ XML (oh god no!)

Slide 32

Slide 32 text

Thank you! @Firesphere https://github.com/Firesphere [email protected] https://speakerdeck.com/firesphere https://casa-laguna.net October 12th, 2016 • Wellington • Simon Erkelens