Core Class

Model - Core Module Class

This is the code that does the bulk of the work within the module. The naming convention for the file and class is simple and following it gives some simple benefits. For example, for the Todos module, the main class name is CTodo while the filename is todos.class.php. By using this naming convention, the class will automatically be added via the autoloader as necessary without any explicit include/require calls.

Further, most Models will extend the core w2p_Core_BaseObject class. While this may initially look like a God Object, it only consolidates the data access and validation of objects, not all kinds of unrelated aspects. The core class of some Modules don’t need this data access directly so they won’t extend this object.

One example is the Project Importer Module which instead calls the appropriate classes on its own. In that sort of case, the rest of this information on the Model does not apply.

public method: __construct()

Return Type: Null

We do not support the PHP4-style method of object constructor where the class name can be used as the constructor. In most cases, a module’s constructor will be as simple as this:

public function __construct()
{
    parent::__construct('todos', 'todo_id');
}

The two parameters are the primary table of the module and that table’s primary key. Nothing further will be needed normally.

public method: check()

Return Type: Array

As of v3.0, this method is deprecated in favor of using isValid() instead. Fundamentally they do much the same thing but isValid() is called automatically by the w2p_Core_BaseObject and will return a boolean. If the value is true, then the object is valid and can be saved. If the value is false (aka invalid), you can use getErrors() to inspect the cause(s).

public method: isValid()

Return Type: Boolean

This public method performs object validation and returns a boolean denoting if the object is valid (true) or not (false). It is called automatically by the w2p_Core_BaseObject’s store() method, so you never have to explicitly call it yourself. Below is an example from the CLinks class (found in ./modules/links/links.class.php):

Note: The w2p_Core_BaseObject provides a default isValid() method which always return true. If you don’t need specific validations, you don’t need to add your own.

public function isValid()
{
    $baseErrorMsg = get_class($this) . '::store-check failed - ';

    if ('' == trim($this->link_name)) {
        $this->_error['link_name'] = $baseErrorMsg . 'link name is not set';
    }
    if (7 >= strlen(trim($this->link_url))) {
        $this->_error['link_url'] = $baseErrorMsg . 'link url is not set';
    }
    if (0 == (int) $this->link_owner) {
        $this->_error['link_owner'] = $baseErrorMsg . 'link owner is not set';
    }

    return (count($this->_error)) ? false : true;
}

If you need the resulting validation errors, you can get the error array with the following code:

$errorArray = $item->getError();

public method: load()

Return Type: Boolean

Note: *Before the item is actually loaded, the system does a canView() check to make sure the user can view the item. If the check fails, this method returns false without attempting to load the item.

This public method simply returns the object with only the fields from the corresponding database row populated. It doesn’t perform any joins or additional lookups and basically provides a Lazy Loading pattern. Most of the time a module will simply inherit this method and not override it so this is optional.

public method: loadFull() - deprecated

This method fully loads the specific object with all of the fields provided by joins, lookups, etc. Due to the joins and lookups, it will be slower than the simple load() method and should be used sparingly if possible. It is also optional.

Other methods

There can be any number of additional methods included of any visibility level desired on the core class. Other than the ones specified above, absolutely no others are necessary. Though, we have quite a few pieces of built-in functionality that you can use by implementing some of the Hook System. For example: