Mokoala v4.0

§ Working With Data » Getting Data

The following code demonstrates how you would get an instance of the 'user' module (Which holds user data) and return all records belonging to that module.

$user_module = MK_RecordModuleManager::getFromType('user');
$users = $user_module->getRecords();

Obviously this could return 1000's of records. In order to limit how many records are returned you can use the MK_Paginator class. Here we are setting the page to 1 and the number of records per page to 10, which will return the first 10 records.

After saving the returned records to $users we loop through the list printing each user's display name.

$paginator = new MK_Paginator();

$user_module = MK_RecordModuleManager::getFromType('user');
$users = $user_module->getRecords($paginator);

foreach($users as $user){
	print $user->getDisplayName()."<br />";

Getting Records

That covers getting a list of records but what if you know which record you want? You can use the MK_RecordManager::getFromId() method to accomplish this; the first parameter is the ID of the module from which you want to pull the data, the second is the ID of the record you want. The following example would get the record from the user module with the ID '1'.

An instance of MK_Record is returned by MK_RecordManager::getFromId().

$user_module = MK_RecordModuleManager::getFromType('user');
$user = MK_RecordManager::getFromId($user_module>getId(), 1);

Manipulating Data

All data stored in Mokoala's CMS (each record) is handled through an instance of MK_Record.

Let's assume there's a table with two fields; 'full_name' and 'married'. Accessing row data is performed as follows; the field 'full_name' would be accessed by calling $record->getFullName() or by calling $record->getMetaValue('full_name'). The 'get' method prefix simply returns the data stored under that field, for that record.

If you want to check if a value is set, for instance on the field 'married', you can use $record->isMarried() $record->isMetaData('married'). The 'is' method prefix casts the returned data for that record as a (boolean) value. For instance if 'married' was '0' false would be returned.

You can also use 'is' methods to set data. If you were to call $record->isMarried(true) or $record->isMetaData('married', true) this data would be set to 1/true or 0/false depending on the field type.

If you want to change the value of the 'full_name' field then you would use $record->setFullName('Matt Lowden') or $record->setMetaValue('full_name', 'Matt Lowden'). In order to save these changes to the database you would use $record->save(). The save and 'set' methods return an instance of $record so they can be strung together, as shown below.

$user_module = MK_RecordModuleManager::getFromType('user');
$user = MK_RecordManager::getFromId($user_module>getId(), 1); // `full_name` = 'Matt Lowden', `married` = '0'

	->setFullName('Matthew Lowden')

print $user->getFullName(); // Would print 'Matthew Lowden'

Authenticating Users

Basic Guides

Handling Forms

Social Sign In

Using the Dashboard

Working With Data

levitra online