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

You're modeling it wrong

You're modeling it wrong

Marijn Huizendveld

September 14, 2012
Tweet

More Decks by Marijn Huizendveld

Other Decks in Programming

Transcript

  1. OOP

  2. <?php final class Product { private $id; private $name; public

    function __construct($name) { $this->name = $name; } public function getId() { return $this->id; } public function getName() { return $this->id; } }
  3. Ja!

  4. Problemen •Betekenisarm model •Niet intuïtief voor de gebruiker •Het model

    is beperkt en niet contextueel •Complexe queries zijn moeilijk •Waardevolle data gaat continue verloren
  5. <?php class CustomerReadModel { private $id; private $name; private $address;

    private $gender; public function getName() { return $this->name; } public function getAddress() { return $this->address; } public function getGender() { return $this->gender; } }
  6. <?php class Customer { private $id; private $name; private $address;

    private $gender; public function setName($name) { $this->name = $name; } public function setAddress(Address $address) { $this->address = $address; } public function setGender($gender) { $this->gender = $gender; } }
  7. <?php class Customer { private $id; private $name; private $address;

    private $gender; public function __construct($name, $gender, $address) { $this->name = $name; $this->gender = $gender; $this->address = $address; } public function moveToNewLivingAddress(Address $newAddress) { $this->address = $newAddress; } }
  8. <?php /** * Publications are the contextual point for all

    other objects * * @author Marijn Huizendveld <[email protected]> * @version $Revision: 163 $ changed by $Author: buroknapzak $ * * @copyright De Baas Media (2010) */ class kluPublicationPeer extends BasekluPublicationPeer { /** * Select the publications that have recently been updated. * * @param string $arg_connection * * @return array */ static public function retrieveRecentlyUpdatedForDomain ($arg_domainUnifiedResourceName = NULL, $arg_connection = NULL) { $sql = "SELECT * FROM (SELECT `publications`.`id` AS ID " . ", `publications`.`name` AS NAME " . ", `publications`.`unified_resource_name` AS UNIFIED_RESOURCE_NAME " . ", `publications`.`domain_unified_resource_name` AS DOMAIN_UNIFIED_RESOURCE_NAME " . ", `publications`.`introduction` AS INTRODUCTION " . ", `publications`.`created_at` AS CREATED_AT " . ", MAX(`revision_requests`.`applied_at`) AS UPDATED_AT " . ", `publications`.`deleted_at` AS DELETED_AT " . "FROM `revision_requests` " . "LEFT JOIN `updates` " . "ON (`updates`.`id` = `revision_requests`.`update_id`) " . "LEFT JOIN `update_collections` " . "ON `updates`.`collection_unified_resource_name` = `update_collections`.`unified_resource_name` " . "LEFT JOIN `publications` " . "ON (`publications`.`id` = `revision_requests`.`publication_id`) " . "WHERE `updates`.`revision_request_id` IS NULL " . "AND `publications`.`deleted_at` IS NULL " . "AND `publications`.`domain_unified_resource_name` = ? " . "AND `update_collections`.`publish_at` <= NOW() " . "AND `update_collections`.`publish_until` > NOW() " . "GROUP BY `revision_requests`.`publication_id` " . "ORDER BY `updated_at` DESC) " . "`publications` ORDER BY `updated_at` DESC"; $connection = NULL === $arg_connection ? Propel::getConnection(self::DATABASE_NAME) : $arg_connection; $statement = $connection->prepareStatement($sql); return self::populateObjects($statement->executeQuery(array(0 => $arg_domainUnifiedResourceName), ResultSet::FETCHMODE_NUM)); } }
  9. <?php /** * Publications are the contextual point for all

    other objects * * @author Marijn Huizendveld <[email protected]> * @version $Revision: 163 $ changed by $Author: buroknapzak $ * * @copyright De Baas Media (2010) */ class kluPublicationPeer extends BasekluPublicationPeer { /** * Select the publications that have recently been updated. * * @param string $arg_connection * * @return array */ static public function retrieveRecentlyUpdatedForDomain ($arg_domainUnifiedResourceName = NULL, $arg_connect { $sql = "SELECT * FROM recently_updated WHERE domain = ? ORDER BY `updated_at` DESC"; $connection = NULL === $arg_connection ? Propel::getConnection(self::DATABASE_NAME) : $arg_connection; $statement = $connection->prepareStatement($sql); return self::populateObjects($statement->executeQuery(array(0 => $arg_domainUnifiedResourceName), ResultSe } }
  10. Samenvatting •Splitsen heeft het model betekenis gegeven •De interface is

    nu task-oriented •Het model kent de context van de data •Complexe queries zijn geëlimineerd