0

$object = new myClass($name);

If I use "new" to create a new object in the application for saving to the db, then what method do I use to retrieve existing objects from the db?

Is there a standard procedure for handling truly new objects vs. getting existing ones from the db?

EDIT:

Would it be a bad idea to have a constructor that changes based on the parameter?

i.e.

__construct($param) {
  if(is_numeric($param)) {
    // get existing data from DB
  } else {
    // set name for new object
    $this->name = $param;
  }
}

1 Answer 1

1

Normally you'd want a Collection class to instantiate your new objects.

Because one db query per record is wasteful.

So you'll have a class that emulates the db record and a class that emulates a list of records.

 class collection {
     protected $filters = array('ids' => array(), 'published' => true, 'name' => '');
     protected $records = array();
     public function __construct($filters) {
         // validate $filters content here
         $this->filters = array_merge($this->filters, $filters);
         $query = $this->generateQueryUsing($this->filters);
         $pdos = $pdo->query($query);

         foreach($pdos->fetch(PDO::FETCH_CLASS, 'Record', array(NULL, FALSE)) AS $item)
            $this->records[] = $item;

         foreach($pdos->fetch() AS $item)
            $this->records[] = new Record($item, FALSE); // Record::__construct($data, $is_new = TRUE)


     }
 }
Sign up to request clarification or add additional context in comments.

7 Comments

So you're saying that using the "new" keyword should be used for individually created new records, and the Collection class should be used to retrieve sets of existing records?
Basically yes, but keep in mind that new relates to the object stored in memory by PHP, and its constructor is called even if you create the object in the collection class... so if new === insert you get duplicates.
Currently I'm using new, then a separate member function save(). And to get existing data I'm using a static method getBy($method,$value) which returns an array of $this objects. I just don't know if I'm being totally weird or kind of normal.
I have a dream of persistent language features. I think you do too.
I'm working on a framework that logs the use of new/unset in order to INSERT/DELETE and logs the changes of property values in order to UPDATE. I make no calls to any get/save/delete functions within my framework. You have to know that class is persistent in the db and use unset if you don't want it INSERT-ed. All INSERT/UPDATE/DELETE takes place transactionally at the end of the script the data is queued. so basically new Product('the name'); will eventually insert the data and return the ID.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.