Skip to content
Vedmaka edited this page Aug 25, 2013 · 3 revisions

constructor

Creates instance of empty Model or loads model from database if id provided.

<?
 //Create new entity
 $object = new Model_Object();
 //Load from db
 $obejct = new Model_Object( $id );
?>

find

Search database for entries with specified parameters. Search for all entries if no parameters passed.

Note: entity fields will be autoloaded only when accesed, no full loading happens on fetch. Only ids.

<?
 //Select all entries
 $allObjects = Model_Object::find();
 //Select specified entries
 $objects = Model_Object::find( array('type' => 'something') );
 //Inline condition
 $obejcts = Model_Object::find( array('age > 0') );
 //Select with options
 $objects = Model_Object::find( array('age' => 1), array('LIMIT'=>10, 'ORDER BY'=>'age DESC') );
?>

##save Saves entity into database.

<?
 $obejcts = Model_Object::find();
 foreach($objects as $object) {
  $object->age = 1;
  $object->save();
 }
?>

count

Counts selected objects.

<?
 //Count all entries
 $objetsCount = Model_Object::count();
 //Counn with condition
 $objetsCount = Model_Object::count( array('age > 0') );
?>

validate

This function intended to use in pair with special pages or other forms related to model. It allow you easy process incoming data and form or update model based on this data.

For this work incoming POST data field should have same names as model fields.

<?
 //Somewhere in code working with form posted data

 $id = $wgRequest->getVal('id');
  //Update object
  $object = Model_Obejct::find($id);
  //Object fields will be modified from POST data after validate call.
  if( $object->validate() ) {
    $obejct->save();
  }

 <...>

 //Create new object
 $object = new Model_Obejct();
 //Object fields will be filled from POST data after validate call.
 if( $object->validate() ) {
    $obejct->save();
 }
?>

If array passed as parameter, validation in check if passed fields exist in post data and return false, if they not.

<?
 <..>
 $object->validate( array('name','password') );
?>

delete

Removes entity from database.

<?
 $objects = Model_Object::find( 10 );
 $obejcts[0]->delete();
?>

#getId Returns id of entity.

<?
 $objects = Model_Object::find( 10 );
 $id = $obejcts[0]->getId(); //returns 10
?>
Clone this wiki locally