Welcome to the Cookbook

loading...

3.7.3.6 read()

このセクションには保留されている変更があります. More information about translations

read($fields, $id)

read() is a method used to set the current model data (Model::$data)--such as during edits--but it can also be used in other circumstances to retrieve a single record from the database.

$fields is used to pass a single field name, as a string, or an array of field names; if left empty, all fields will be fetched.

$id specifies the ID of the record to be read. By default, the currently selected record, as specified by Model::$id, is used. Passing a different value to $id will cause that record to be selected.

read() always returns an array (even if only a single field name is requested). Use field to retrieve the value of a single field.

function beforeDelete($cascade) {
   ...
   $rating = $this->read('rating'); // gets the rating of the record being deleted.
   $name = $this->read('name', $id2); // gets the name of a second record.
   $rating = $this->read('rating'); // gets the rating of the second record.
   $this->id = $id3; //
   $this->read(); // reads a third record
   $record = $this->data // stores the third record in $record
   ...
}
  1. function beforeDelete($cascade) {
  2. ...
  3. $rating = $this->read('rating'); // gets the rating of the record being deleted.
  4. $name = $this->read('name', $id2); // gets the name of a second record.
  5. $rating = $this->read('rating'); // gets the rating of the second record.
  6. $this->id = $id3; //
  7. $this->read(); // reads a third record
  8. $record = $this->data // stores the third record in $record
  9. ...
  10. }

Notice that the third call to read() fetches the rating of the same record read before. That is because read() changes Model::$id to any value passed as $id. Lines 6-8 demonstrate how read() changes the current model data.