php classes and data retrieval

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello!
I have a database and php classes and I'm not sure the best way I should be retrieving the data.

as below, if 'other' (line 9) is stored in the database as an id of a record in another table,
should my getData() method:

a. return the id as part of the MyObj object and whatever function
that called it is responsible for matching the id to the name?
or
b. should it look up the value from within the class?

(I read something about JOIN or stuff beyond SELECT --sorry i'm kinda just getting into this-- could bring a database to it's knees)

Also I thought that with OOP you are supposed to keep the classes independent of each other, but I was thinking if I need a MyObj I would want all the data in a ready usable form and not a mix of data and database ids.

I'm not sure what is the best practice or even if there is a correct one, but suggestions would be appreciated!

-robert


here is my set up:

DATABASE:
myobj_table with id, name, other as columns
other_table with id, name as columns

PHP CLASSES:
   1: <?php
   2:
   3: /* base object data structure */
   4: class MyObj {
   5:
   6:   function MyObj($id, $name, $other) {
   7:     $this->id = $id;
   8:     $this->name = $name;
   9:     $this->other = $other; // ?? ID OR VALUE ??
  10:   }
  11:
  12:   // getters/setters
  13:
  14: }
  15:
  16:
  17: /* create/update/delete database methods */
  18: class MyObjData {
  19:
  20:   var $_id;
  21:   var $_items;
  22:
  23:   function MyObjData($id) {
  24:     $this->_id = $id;
  25:   }
  26:
  27:   function getData() {
28: $query = "select * from myobject_table where id='" . $this- >_id . "'"; 29: $records = getDatabaseRecords($query); // some db action: returns an array of records
  30:     foreach ($records as $record) {
  31:       $data = new MyObj(
  32:           $record['id']
  33:         , $record['name']
  34:         , $record['other']
  35:         );
  36:       $this->_items[] = $data;
  37:       unset($data);
  38:     }
  39:     return $this->_items;
  40:   }
  41:
  42:   // other methods
  43:
  44: }
  45:
  46:
  47:
  48: ?>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux