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

    Join Date
    Dec 2004
    Posts
    2,998
    Rep Power
    375

    Getting/using child primary_key in parent class?


    i have a class called common, it has a function:

    PHP Code:

     
    public function save(){    
         return isset(
    $this->order_id)? $this->update() : $this->create();
      } 
    now this order_id will change depending on each child class, for users it could be user_id, orders could be order_id.. so how do i "get" the primary key from child table and use it here?

    here is a sample of my child.

    PHP Code:
    protected static $table_name ="orders";
        protected static 
    $db_fields = array('userId','orderId''status''orderDate','dispatchDate');
        public 
    $userId;
        public 
    $orderId;
        public 
    $status;
        public 
    $orderDate;
        public 
    $dispatchDate;
        
    //public $paymentDate; should be the same as orderDate!! 
    i was thinking to add another attribute called primary_key so public private_key = order_id and then use this in the parent class somehow to set/use the actual attribute public order_id ?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,998
    Rep Power
    375
    i seemed to have solved this one too.
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    You could also make $db_fields a more descriptive array with metadata like primary key (remembering there can be more than one field in a table's primary key if your database is properly designed), data type, validation restrictions, etc.
    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. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,998
    Rep Power
    375
    thanks as always. do you mean:

    $db_fields = array (
    user_id => (primary_key => true, validation => "numbers"...)
    )

    or

    $db_fields = array (
    'fields' = > array ( 'user_id', first_name.....),
    'primary_key => array (user_id, username)
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    I was talking about the first (though obiously with correct syntax). Once you have an array like that with metadata slots already established you can start doing really neat things like rolling your own ORM system.
    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.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,998
    Rep Power
    375
    thanks. I think i am a bit far aqway from rolling my own ORM.. only just started OOP (well without anyone's help!)
  12. #7
  13. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Another more static means of doing it:

    PHP Code:
    abstract class Common {
        abstract public function 
    getPrimaryKeyValue();

        public function 
    save(){     
            
    $value $this->getPrimaryKeyValue();
            return !
    is_null($value)? $this->update() : $this->create(); 
        }  
    }
    class 
    Users extends Common {
        public 
    $user_id;
        public function 
    getPrimaryKeyValue(){
            return 
    $this->user_id;
        }

    Be careful not to go too dynamic in how your classes work. Some of what you're doing are fun concepts to explore, but some of the simple things become increasingly complex--like performing validation of the data passed into your model objects.

    Edit:

    Also keep in mind that you might eventually end up with database tables that are identified by the unique combination of multiple IDs.
    Last edited by dmittner; July 10th, 2013 at 10:32 AM.

IMN logo majestic logo threadwatch logo seochat tools logo