• 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
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
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
• 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
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
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.
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
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
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
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
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
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
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
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
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
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
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
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.
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
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
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
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
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
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 %>
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
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!)