Moodle APIs 3.9
Moodle 3.9.13+ (Build: 20220325)
Public Member Functions | Static Public Member Functions | Public Attributes | Static Protected Member Functions | List of all members
data_object Class Reference
Inheritance diagram for data_object:
completion_aggregation completion_completion completion_criteria completion_criteria_completion completion_criteria_activity completion_criteria_course completion_criteria_date completion_criteria_duration completion_criteria_grade completion_criteria_role completion_criteria_self completion_criteria_unenrol

Public Member Functions

 __construct ($params=null, $fetch=true)
 Constructor. More...
 
 delete ()
 Deletes this object from the database. More...
 
 get_record_data ()
 Returns object with fields and values that are defined in database. More...
 
 insert ()
 Records this object in the Database, sets its id to the returned value, and returns that value. More...
 
 load_optional_fields ()
 Makes sure all the optional fields are loaded. More...
 
 notify_changed ($deleted)
 Called immediately after the object data has been inserted, updated, or deleted in the database. More...
 
 update ()
 Updates this object in the Database, based on its object variables. More...
 
 update_from_db ()
 Using this object's id field, fetches the matching record in the DB, and looks at each variable in turn. More...
 

Static Public Member Functions

static fetch ($params)
 Finds and returns a data_object instance based on params. More...
 
static fetch_all ($params)
 Finds and returns all data_object instances based on params. More...
 
static fetch_all_helper ($table, $classname, $params)
 Factory method - uses the parameters to retrieve all matching instances from the DB. More...
 
static set_properties (&$instance, $params)
 Given an associated array or object, cycles through each key/variable and assigns the value to the corresponding variable in this object. More...
 

Public Attributes

int $id
 
array $optional_fields = array()
 Array of optional fields with default values - usually long text information that is not always needed. More...
 
array $required_fields = array('id')
 
string $table
 
Array $unique_fields = array()
 

Static Protected Member Functions

static fetch_helper ($table, $classname, $params)
 Factory method - uses the parameters to retrieve matching instance from the DB. More...
 

Constructor & Destructor Documentation

◆ __construct()

data_object::__construct (   $params = null,
  $fetch = true 
)

Constructor.

Optionally (and by default) attempts to fetch corresponding row from DB.

If $fetch is not false, there are a few different things that can happen:

  • true: load corresponding row from the database, using $params as the WHERE clause
  • DATA_OBJECT_FETCH_BY_KEY: load corresponding row from the database, using only the $id in the WHERE clause (if set), otherwise using the columns listed in $this->unique_fields.
  • array(): load corresponding row from the database, using the columns listed in this array in the WHERE clause
Parameters
array$paramsrequired parameters and their values for this data object
mixed$fetchif false, do not attempt to fetch from the database, otherwise see notes

Member Function Documentation

◆ delete()

data_object::delete ( )

Deletes this object from the database.

Return values
boolsuccess

◆ fetch()

static data_object::fetch (   $params)
static

Finds and returns a data_object instance based on params.

This function MUST be overridden by all deriving classes.

Parameters
array$paramsassociative arrays varname => value
Exceptions
coding_exceptionThis function MUST be overridden
Return values
data_objectinstance of data_object or false if none found.

Reimplemented in completion_aggregation, completion_completion, completion_criteria_completion, completion_criteria_activity, completion_criteria_course, completion_criteria_date, completion_criteria_duration, completion_criteria_grade, completion_criteria_role, completion_criteria_self, and completion_criteria_unenrol.

◆ fetch_all()

static data_object::fetch_all (   $params)
static

Finds and returns all data_object instances based on params.

This function MUST be overridden by all deriving classes.

Parameters
array$paramsassociative arrays varname => value
Exceptions
coding_exceptionThis function MUST be overridden
Return values
arrayarray of data_object instances or false if none found.

Reimplemented in completion_aggregation, completion_criteria_completion, and completion_criteria.

◆ fetch_all_helper()

static data_object::fetch_all_helper (   $table,
  $classname,
  $params 
)
static

Factory method - uses the parameters to retrieve all matching instances from the DB.

@final

Parameters
string$tableThe table name to fetch from
string$classnameThe class that you want the result instantiated as
array$paramsAny params required to select the desired row
Return values
mixedarray of object instances or false if not found

◆ fetch_helper()

static data_object::fetch_helper (   $table,
  $classname,
  $params 
)
staticprotected

Factory method - uses the parameters to retrieve matching instance from the DB.

@final

Parameters
string$tableThe table name to fetch from
string$classnameThe class that you want the result instantiated as
array$paramsAny params required to select the desired row
Return values
objectInstance of $classname or false.

◆ get_record_data()

data_object::get_record_data ( )

Returns object with fields and values that are defined in database.

Return values
stdClass

◆ insert()

data_object::insert ( )

Records this object in the Database, sets its id to the returned value, and returns that value.

If successful this function also fetches the new object data from database and stores it in object properties.

Return values
intPK ID if successful, false otherwise

◆ load_optional_fields()

data_object::load_optional_fields ( )

Makes sure all the optional fields are loaded.

If id present (==instance exists in db) fetches data from db. Defaults are used for new instances.

◆ notify_changed()

data_object::notify_changed (   $deleted)

Called immediately after the object data has been inserted, updated, or deleted in the database.

Default does nothing, can be overridden to hook in special behaviour.

Parameters
bool$deletedSet this to true if it has been deleted.

◆ set_properties()

static data_object::set_properties ( $instance,
  $params 
)
static

Given an associated array or object, cycles through each key/variable and assigns the value to the corresponding variable in this object.

@final

Parameters
data_object$instance
array$params

◆ update()

data_object::update ( )

Updates this object in the Database, based on its object variables.

ID must be set.

Return values
boolsuccess

◆ update_from_db()

data_object::update_from_db ( )

Using this object's id field, fetches the matching record in the DB, and looks at each variable in turn.

If the DB has different data, the db's data is used to update the object. This is different from the update() function, which acts on the DB record based on the object.

Return values
boolTrue for success, false otherwise.

Member Data Documentation

◆ $optional_fields

array data_object::$optional_fields = array()

Array of optional fields with default values - usually long text information that is not always needed.

If you want to create an instance without optional fields use: new data_object($only_required_fields, false);


The documentation for this class was generated from the following file: