Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ORM & SilverStripe ORM

Firesphere
October 12, 2016
94

ORM & SilverStripe ORM

Firesphere

October 12, 2016
Tweet

Transcript

  1. 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
  2. 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
  3. A dive into (SilverStripe) ORM October 12th, 2016 • Wellington

    • Simon Erkelens Because humans are developers too https://twitter.com/Una/status/743382603210891264
  4. 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
  5. 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
  6. 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
  7. 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', ); }
  8. 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.
  9. 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)', );
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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.
  22. 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.
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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 %>
  29. 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
  30. 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!)