Skip to content
Jeff Johns edited this page Feb 4, 2014 · 11 revisions
ModelExtendsTablePath
Labels_ModelPlain_Modellabels/application/models/labels_model.php

Loading the model

$this->load->model('labels_model', 'labels');

Properties

PropertyVisibilityDefault ValueDescription
$sortPubliccreated_on ASCThe default sort direction for reads.

Methods

__construct - Public

Called automatically which in turn calls the parent constructor and sets the model $data_types properties.


create - Public

Used to create new records in the labels table. There are two types of labels, smart and label. The latter is a system-level only, normal label. The smart label can be a default system-level smart label or a user defined system-label.

Arguments (Overall)

VariableTypeDefaultRequiredDescription
$optionsarrayarray()YesAn associative array that contains the column names and the values for the record to be created. Array keys are the column names for each value.

Arguments (System-Level Normal Label)

If creating a system-level normal label below are the fields that can be submitted.

VariableTypeDefaultRequiredDescription
$options.nameStringN/AYesThe name of the label.
$options.slugStringN/AYesThe unique slug used for lookups. To easily create this use the [data helper method of `generateSlug`](Helpers-Data.md).
$options.activeNumber1NoSet to 1 for active, or 0 for inactive.

Arguments (Smart Labels)

If creating a system-level or user-defined smart labels below are the fields that can be submitted.

VariableTypeDefaultRequiredDescription
$options.smart_label_idStringN/AYesThe `labels.label_id` of the normal label to apply when this rule is matched.
$options.domainStringN/AYesThe FQDN (IE: google.com)
$options.pathStringNullNoThe path to match with the domain.
$options.smart_keyMD5N/AYesThe MD5 hash of the domain + path used for easy lookups. To easily create this use the [data helper method of `getSmartLabelInfo`](Helpers-Data.md).
$options.activeNumber1NoSet to 1 for active, or 0 for inactive.
$options.user_idNumberNullNoNull for system-level smart label or a valid `users.user_id` for ownership.

Example (Normal Labels)

$name = 'Read';
$slug = generateSlug($name);
// Attempt to create normal label$this->load->model('labels_model', 'labels');
$label = $this->labels->create(array('name' => $name, 'slug' => $slug));
// If label was createdif (isset($label->label_id)) {
// Good to go
}
// Some sort of validation errorelseif (isset($label['errors']) {
// Validation errors were found
}
// Some sort of database write error, check logselse {
// will return false
}

Example (Smart Labels - User Defined)

$url = 'http://amazon.com/gp/product';
$smart_info = getSmartLabelInfo($url);
// Attempt to create the smart label$this->load->model('labels_model', 'labels');
$label = $this->labels->create(array(
'domain' => $smart_info['domain'],
'path' => $smart_info['path'],
'smart_key' => $smart_info['key'],
'user_id' => $this->user_id
));
// If label was createdif (isset($label->label_id)) {
// Good to go
}

Example (Smart Labels - System Defined)

$url = 'http://youtube.com/watch';
$smart_info = getSmartLabelInfo($url);
// Attempt to create the smart label// Do NOT pass the user_id$this->load->model('labels_model', 'labels');
$label = $this->labels->create(array(
'domain' => $smart_info['domain'],
'path' => $smart_info['path'],
'smart_key' => $smart_info['key']
));
// If label was createdif (isset($label->label_id)) {
// Good to go
}

formatResults - Private

Since the labels table actually stores 3 different types of labels, we need to format the data a bit before it is returned. By formatting each row, it will set the type (smart or label) and if smart add some definitions for settings (smart_label_name, smart_label_slug and smart_label_id), each of those are inherited from the parent (normal label) the smart label applies when the rule is matched.

Arguments

VariableTypeDefaultRequiredDescription
$labelsobjectN/AYesThe complete list of labels to be formatted and returned.

Example

$labels = $this->formatResults($labels);

readComplete - Public

Used to read all label data and return it in a nicely formatted structure. You can always use $this->labels->read to get the raw data from the table anytime you wish.

Arguments

VariableTypeDefaultRequiredDescription
$wheremixedN/AYesIf numeric the where clause will be filled in with the id submitted and the correct column, otherwise it will just use the where clause you submitted.
$limitinteger1NoThe max records to return.
$pageinteger1NoThe current page of records to return. Used as an offset in the LIMIT statement.
$startintegernullNoThe start position to return records from.

Example

// Return all info for labels.label_id = 15// info will be accessible like $label->label_id since it's a single item$this->load->model('labels_model', 'labels');
$label = $this->labels->readComplete(15);
if (! isset($label->label_id)) {
// Good to go
}
else {
// Handle your errors
}
// Return all normal labels$this->load->model('labels_model', 'labels');
$labels = $this->labels->readComplete("labels.user_id IS NULL AND labels.smart_key IS NULL");
if ($labels !== false) {
foreach ($labelsas$label) {
// Doowutchalike
}
}
else {
// Handle your errors
}
// Return 5 system smart labels$this->load->model('labels_model', 'labels');
$labels = $this->labels->readComplete("labels.user_id IS NULL AND labels.smart_key IS NOT NULL", 5);
if ($labels !== false) {
foreach ($labelsas$label) {
// Doowutchalike
}
}
else {
// Handle your errors
}
// Return all user defined system smart labels$this->load->model('labels_model', 'labels');
$labels = $this->labels->readComplete("labels.user_id='56' AND labels.smart_key IS NOT NULL");
if ($labels !== false) {
foreach ($labelsas$label) {
// Doowutchalike
}
}
else {
// Handle your errors
}

Clone this wiki locally