Fabriq Framework Docs

This page displays the APIs for database interactions that are made available by the /core/DatabasepgSQL.class.php file's Fabriq class. Each function available for use is listed below with examples of how to use the function.

The default database connection is created automatically by the Fabriq framework. Connections to other databases can be made by instantiating a new instance of the Database class.

As of version 1.1, MySQL is the default database type for use with Fabriq. For information on using MySQL, see the DatabaseMySQL.class.php page.

add($obj, $index = null)

Parameters

  • obj - the object to be added to the collection
  • index (optional) default value: null - the position to insert the object into the collection

Description

The add function adds an object into the collection. If no index is given, the object is added to the end of the collection, otherwise, the object is inserted into the collection at the given index.

Example(s)

// example adding an object to the end of a collection
$myCollection->add($myObject);
// example adding an object to index 3 of the collection
$myCollection->add($myObject, 3);

count()

Description

This function returns the number of objects in the collection.

Example(s)

$myCollection = new MyModels();
// add some data to the collection
echo "<p>There are " . $myCollection->count() . " items in the collection.</p>";

Return type: integer

create()

Description

Adds the contents of the current object (or the first object in the collection) to the database. If the object is successfully inserted into the database, the ID of the newly inserted data is returned. If the insert fails, the function returns FALSE.

Example

$myObject = new MyModels();
$myObject->col1 = 'abcde';
$myObject->col2 = 123;
$myObject->id = $myObject->create();

Return type: integer

createAt($index)

Parameters

  • index - the index of the object in the collection to be inserted into the database

Description

Adds the contents of the object at the given index to the database. If the object is successfully inserted into the database, the ID of the newly inserted data is returned. If the insert fails, the function returns FALSE.

Example

$myCollection = new MyModels();
// add some objects to the collection
// add the 4th object of the collection to the database (index 3)
$myCollection->createAt(3);

Deprecated in version: 0.10

Removed in version: 0.12

Return type: integer

destroy($index = NULL)

Parameter

  • index (optional) default value: NULL - the index at which to delete a record from the database

Description

Deletes a record from the database at the ID at the given index of the collection. If no index is given, the ID of the first item in the collection is deleted from the database.

Example

// find an object from the database and delete
$myObject = new MyModels();
$myObject->find(123);
$myObject->destroy();

fields()

Description

Returns an array of fields to make it easier to use the Database::prepare_select() function.

Example(s)

// select data for the collection
$sql = "SELECT " . $this->fieldsStr() . " FROM {$this->db_table};";
$this->fill($db->prepare_select($sql, $this->fields()));

Added in version: 0.10

Return type: array

fieldsStr()

Description

Builds a string of the fields to select from the database.

Example(s)

// select data for the collection
$sql = "SELECT " . $this->fieldsStr() . " FROM {$this->db_table};";
$this->fill($db->prepare_select($sql, $this->fields()));

Added in version: 0.10

Return type: string

fill($arr)

Parameters

  • arr - array to fill the collection with

Description

This function creates objects with the values from the given array to fill the collection with.

Example

// fill a collection with results pulled from the database
class MyModels extends Model {
  // constructor
 
  // my custom find function
  function myFind() {
    global $db;
 
    $query = "SELECT * FROM mymodels;";
    $fields = array_merge($this->attributes, array('id', 'created', 'updated'));
    $this->fill($db->prepare_select($query, $fields));
  }
}

find($query)

Parameters

  • query - the ID of the object to be selected or the accepted string values to search. Acceptable string values are first, last, and all.

Description

Selects the item at the given ID or by using one of the accepted string values. The acceptable string values are first, last, and all which select the first item, the last item, or all items in the database and orders them by ID.

NOTE: If you are using a version of Fabriq prior to 1.0, your copy of the find function will accept more parameters. To future proof your code, please update any references that use the longer, old version of find to use a custom find function that you define in your model classes making use of the Database class' prepare_select() function.

Example(s)

// select an item by ID
$myObject->find(123);
// select the first item in the database
$myObject->find('first');
// select the last item in the database
$myObject->find('last');
// select all of the items in the database
$myCollection->find('all');

Return type: boolean

remove($index)

Parameters

  • index - the index of the item in the collection to remove

Description

Removes the item from the collection at the given index and shifts all elements in the collection after it down a position to fill the gap. The removed object is returned.

Example(s)

// remove the 5th item in the collection (the item at index 4)
$obj = $myCollection->remove(4);

Return type: object

update()

Description

The update function updates the first object in the collection. If your model is currently only storing one object, that is the object that gets updated. The function returns the number of rows affected.

Example(s)

$myObject = new MyModel(123);
$myObject->col1 = 'zyxw';
$myObject->update();

Return type: integer

updateAt($index)

Parameters

  • index - the index of the item to be updated

Description

The update function updates the object at the given index in the collection. The function returns the number of rows affected.

Example(s)

$myObject = new MyModel();
// select some items and then update one of them
$myObject[123]->col1 = 'zyxw';
$myObject->update(123);

Deprecated in version: 0.10

Removed in version: 0.12

Return type: integer

__construct($attributes, $db_table = null, $id_name = 'id')

Parameters

  • attributes - an array listing the attributes of the model. If the model is being used to map to a database table, the list of attributes should match the database columns (with the exception of the ID, updated, and created columns).
  • db_table (optional) default: null - if the model is being used to map to a database table, the db_table value should be set to the name of the database table being mapped to.
  • id_name (optional) default: 'id' - if the model is being used to map to a database table and the ID column has a different name than id, the third parameter is the name of the ID column.

Description

The base model class' constructor function sets up the details for the model. Each model that you use in your Fabriq based web applications and websites should extend the base Model class. Your models constructor functions need to call the base Model's constructor function to set up details of the model. You may also accept an optional ID value in the constructor function of your models and call the find function to short cut creating and selecting an object.

Because models can store a collection of objects, it is a convention to give your models a plural name (ex: Dogs instead Dog).

Example(s)

// example with a short cut for creating and selecting
// if an id is passed in when the object is created, it is automatically
// selected from the database
class MyModels extends Model {
  public function __construct($id = NULL) {
    parent::__construct(array('col1', 'col2', 'col3'), 'mymodels');
    if ($id != NULL) {
      $this->find($id);
    }
  }
}
// example with no shortcut and an id column named
// something other than id
class MyModels extends Model {
  public function __construct() {
    parent::__construct(array('col1', 'col2', 'col3'), 'mymodels', 'myidcol');
  }
}
// example of a model that does not connect to a database
class MyModels extends Model {
  public function __construct() {
    parent::__construct(array('col1', 'col2', 'col3'));
  }
}