#1
  1. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,968
    Rep Power
    374

    General Class question


    Just wanted to know that if for example your database has these tables:

    table1, table1_links_table2, table2

    that you would create three CLASSES? so your classes "follow" your database?

    if this is the case say you had these tables:

    table 1 ( user_id, user_type_id......)
    table 2 (user_type_id, user_type)

    you wouldnt really manipulate the table 2 so creating a class for it would be pointless?
  2. #2
  3. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    I think you're looking at following an Object Relational Mapping (ORM) design pattern. Not something that I like, but try googling the term and see what you find
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    Why don't you like the ORM pattern? I love it.

    I would make 3 objects, but most of my code would act as though the intermediary object didn't exist, because my ORM systems always "tap through" those relationship tables, allowing $table1->table2s to be a single variable on table1 which leverages the table1_to_table2_crossreference table to create a list of table2 objects.

    And vice versa, if applicable.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    Originally Posted by ManiacDan
    Why don't you like the ORM pattern? I love it.
    * Because I see my app and my data as two distinct entities

    * Because I do not see the requirement to represent my data as an object - I treat my data as it is stored in a data store (ie database)

    * Because I have different types of data - I keep my relational data in a RDBMS, I keep my hierarchical data in structured hierarchy (XML or JSON) and I keep my files on a file system

    * Because off-the-shelf ORM solutions appear to abstract (and in some cases prevent) the true power of SQL

    * Because it adds (IMHO unnecessary) overhead

    * Because off-the-shelf ORM solutions don't seem to allow me to design my database first....I seem to have to spend a laborious amount of time writing manifest files and defining the relationships outside the relational engine just so I can bridge the gap (that i never thought was there in the first place) with some bridging code that dumbs down what is and isn't possible - all because it dangled the "this will make your life easier carrot" yet makes headaches elsewhere.

    Sure, if you're employing a bunch of developers that only know OOP and have no clue about data architecture, proper relationships or SQL then an ORM layer can help. But, IMO, it's a compromise - it would be better to have more skilled developers.

    As I'm a one-man-development team I've had to design a one-man-development-team framework (and although its far from what I'd like it to be it's pretty ****ing marvellous as it stands and meets most of my every day needs perfectly)

    Don't get me wrong, I love abstraction where abstraction and interfaces work, but just not for the sake of it.

    I have a solution that I have made myself which adapts to database changes - as I make them in the database (making developing products super agile (in the original sense of the word)) and adds in basic CRUD methods....but it doesn't attempt to represent that data as objects.

    What I have done is take an approach from the other direction - from the request perspective following ReSTful principles. My app code bridges the gap in access, permissions and formatting of data between URIs and HTTP Requests and data stored in various places
    Last edited by Northie; April 24th, 2013 at 08:39 AM.
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,968
    Rep Power
    374
    I don't understand? (northie)

    Say if i had a table called "users" with fields (id, name, email).. then since user is an "object" wouldnt i create a class user with these attributes, and then create functions to get name, etc etc..

    What would you do? not create a class?

    Thanks to both you and Maniac Dan for replying (and being helpful as always , not saying others arent, but I feel sometimes us noobs take you guys for granted..)
  10. #6
  11. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    I do have a users class, but it does not map to my database because that's not where my user is - s/he is sitting at a computer.

    Sure, things like name and settings are loaded into the class from the database and the session but i don't do

    PHP Code:
    $current_user = new User($_SESSION['user']['id']); 
    I do have things like

    PHP Code:
    \core\User::Load(); //my user class is a singleton

    \core\User::Load()->ACL($this->resourse); 

    to log a user in i have a class at

    \packages\users\login.class.php

    part of which executes

    PHP Code:
    $where = array("email_address" => $_POST['email_address']);

    $rs = \libs\models\Resources::Load('user')->read($where)->getOne();

    //$rs is then an array repesenting a dataset returned from a MySQL query executed using PDO 
    this calls a singleton which manages single instances of model classes - these are nothing more than classes/methods holding SQL statements for my various tables - each returning an array or a boolean
    Last edited by Northie; April 24th, 2013 at 12:04 PM.
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,968
    Rep Power
    374
    wow, sounds like i have LOTS more reading to do..
  14. #8
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    On the flip side, my ORM with factory patterns has things like:

    $lb = new Hardware(123);

    The __construct($id, $data = array()) in the Hardware class looks up the hardware record, determines its hardware type (load balancer in this case), and loads the load balancer constructor

    LoadBalancer::__construct($id, $data = array()) examines the data further and determines this is an arrayNetworks load balancer, OS version 6.4

    LoadBalancer_ArrayNetworks_64::__construct($id, $data=array()) examines the data, realizes there's no more classes which need to be loaded, and populates $data into the class variables. Then it checks $this->__preFetch to see if there are any "pre-fetched" ORM keys which are loaded when they object is loaded. Finding one for virtualIPs, it then checks the ORM relationship between Load Balancers and VIPs.

    The Relationship says that load balancers are linked to VIPs using a foreign key on the VIP table which points to the load balancer's Hardware ID. The query is also limited by the currently logged in user's client record. The resulting query is auto-generated (being ORM) and selects all VIPs linked to the load balancer which belong to the current user, and puts them in the $this->vips array on the load balancer class.

    All of this is well documented by an automated program which crawls the code, analyzes all the relationships and in-line comments, and builds that website.

    While I do agree with Northie that ORM, when done poorly, limits your ability to leverage the true power of SQL, but this ORM system is the most powerful and flexible data access system I've ever used.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  16. #9
  17. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    I've seen auto-documenters before, like phpdoc....but does your ORM implementation read form your database?

    That's sort of what I'm heading towards => read database, interpret relationships, generate the php (which can then be documented).

    I like the way that you're limiting by the current user, are you doing this through ACLs? (my ACL implantation is becoming cumbersome and slow)....what about god-like or full admin permssions, can they see things they don't 'own'?
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  18. #10
  19. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    It does initially, yeah. It populates base relationships based on the defined keys, but the relationships are all human-readable and we can edit them however we like (like marking them as limited to the current account or adding pass-through relationships for cross-reference tables).

    I don't work at SoftLayer anymore (haven't for 6 years), but their ORM system and the APIs on top of it are the best I've ever worked with.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo