Login +

Reference Guide / SLS_FrontModel

SLS_FrontModel1 is the superclass of each Model, it contains all generic methods for manipulating the Model layer.

Accessibility

At Model level, you have direct access through inheritance:

$model = new Db_Model();

Extraction – Change

Get Keys / Values

You can extract all keys / values through the getParams([$fk=false]) function:

$model->getParams();
array() {
    ["key_1"]=> string(6) "value1",
    ["key_2"]=> string(6) "value2",
    …
};

If you want to also extract keys / values from all tables linked by foreign keys :

$fk = true; // Parameter

If you want to extract keys / values only from specified tables linked by foreign keys :

$fk = array("table1","tableN"); // Parameter

Get Keys

If you want to extract only keys, use the getColumns() function:

$model->getColumns();

Get Keys updated

If you want to extract only keys updated, use the getParamsUpdated() function:

$model->getParamsUpdated();

Get Key

If you want to extract a particular column, use the magic getter __get($key) :

$model->__get("key1");

Set Key

If you want to change the value of a column, use the magic setter __set($key, $value)

$model->__set("key1", "value1");

This method is not recommended because it does not make any test before allocation, focus on specific setters

Handling

Recordset binding

To map your instance of Model on a recordset in particular, use getModel($pk) function:

$model->getModel($id);

The set of variables of your Model classes will be retrieved from the table entry described by its primary key.

Insert

To insert a new record from your current Model, use the create() function:

$model->create([$id]);
If your primary key is not auto-incremented, you can specify yourself the next id to assign.

Update

To save the current Model and reflect the changes on the table entry, use the save() function:

$model->save();

Delete

To delete the current Model from table, use the delete() function:

$model->delete();

This action is irreversible.

Clear

To reset the current model, use the clear() function:

$model->clear();

Errors

All errors

To retrieve all errors raised by the current Model, use the getErrors() function:

$model->getErrors();

Specific key

To get an error on a particular column, use the getError($key) function:

$model->getError($key);

Utilities

Select

To search for N entries in a table, use the searchModels() function:

$model->searchModels($table,
                     $join,
                     $where,
                     $group,
                     $order,
                     $limit);

Where:

  • $table is a string containing the name of the table (if empty, table of current Model)
$table = 'article';
  • $join is a single or multiple array to describe the natural join / inner join

For a natural join :

$join = array('author', 'job');

For a join on a particular column:

$join = array(array('table'   => 'author',
                    'column' => 'author_id'));
  • $where is a multiple array containing all necessary clauses
$where = array('column' => 'author_id',
               'value'  => 1,
               'mode'   => 'equal');

Modes availables are:
- "like"
- "notlike"
- "beginwith"
- "endwith"
- "equal"
- "notequal"
- "lt"
- "le"
- "ge"
- "gt"
- "null"
- "notnull"
- "in"

  • $group is a simple array listing all the columns on which group
$group = array('article_id');
  • $order is an array showing multiple columns on which sorting and their order
$order = array(array('column' => 'article_date',
                     'order'  => 'desc'));

or:

$order = array('article_date' => 'desc');
  • $limit is a simple array indicating offsets limitations of the query
$limit = array('start'  => 0,
               'length' => 10);

Count

To count the number of entries in a table (or recordsets), use the countModels() function:

$model->countModels($table,
                    $join,
                    $where,
                    $group);

Parameters are the same as the searchModels() function.

Delete

By analogy, to remove N entries in a table, use the deleteModels() function:

$model->deleteModels($table,
                     $join,
                     $where);

Parameters are the same as the searchModels() & deleteModels() functions

Exec

If you want to run the query of your choice (at your own risk), use the exec() function:

$model->exec("SHOW FULL COLUMNS FROM `article`");

Transaction Begin

If you want to begin a transaction, use the beginTransaction() function:

$model->beginTransaction();

Transaction Commit

If you want to validate a transaction, use the commitTransaction() function:

$model->commitTransaction();

Transaction Rollback

If you want to cancel a transaction, use the rollbackTransaction() function:

$model->rollbackTransaction();

Custom SQL function

If you want to call a SQL function of the current Model, use the callSqlFunction() function:

$model->callSqlFunction($functionName, $param1, …, $paramN);

XML building

Current recordset

If you want to easily getting the XML of the current recordset binded by the getModel($id) function, you can call the toXML() function:

if ($news->getModel(118) === true)
    $xml = $news->toXML($xml);

Will produce:

<View>
    <news>
        <news_id><![CDATA[118]]></news_id>
        <pk_lang><![CDATA[en]]></pk_lang>
        <news_title><![CDATA[  fake_news_title ]]></news_title>
        <news_content><![CDATA[<p>news content en</p>]]></news_content>
        <news_excerpt><![CDATA[<p>Fake excerpt</p>]]></news_excerpt>
        <news_more><![CDATA[<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>]]></news_more>
        <news_date><![CDATA[2012-06-08 17:45:32]]></news_date>
        <news_published><![CDATA[true]]></news_published>
        <news_photo><![CDATA[news/new_york_4_640345cff7.jpg]]></news_photo>
        <news_url><![CDATA[http://www.sillysmart.org]]></news_url>
        <article_category_id><![CDATA[1]]></article_category_id>
        <user_id><![CDATA[4]]></user_id>
    </news>
</View>

  • 1st parameter $xml is the XML modified by the current action
  • 2nd parameter $options allows to specify transformations on some columns
$options = array("news_excerpt" => array("php:strip_tags",
                                         "SLS_String:trimStringToLength:100"),
                 "news_date"    => array("SLS_Date:getDate:MONTH_LITTERAL",
                                         "php:ucwords"),
                 "news_photo"   => "SLS_String:getUrlFileImg:_0",
                 "news_title"   => "php:trim");

Each column can undergo many transformations as necessary (in the order they are
declared).

The formalism of the transformation can be of two types:
- Prefix "php": using a native function php (no class)
- Name of the class: using a static function of the class of your choice.

In both cases, the following parameters correspond to the function name and all the parameters of the latter.
If the function does not exist or it is called with an insufficient number of parameters, it raises an error.
If the function is called with too many parameters, it raises a warning.

  • 3rd parameter $fks can specify whether you want to retrieve related tables. These possible values ​​are exactly the same as for the function getParams(): default false to any joint, true to all related tables or array wanted tables.
$fks = array('user');
  • 4th parameter $nodeName can specify the name by hand to form the XML node.
    The default is the table name to be used.
$nodeName = 'my_news';

With these parameters filled, the resulting XMLshould look like:

<View>
    <my_news>
        <news_id><![CDATA[118]]></news_id>
        <pk_lang><![CDATA[en]]></pk_lang>
        <news_title><![CDATA[fake_news_title]]></news_title>
        <news_content><![CDATA[<p>news content en</p>]]></news_content>
        <news_excerpt><![CDATA[Fake excerpt]]></news_excerpt>
        <news_more><![CDATA[<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>]]></news_more>
        <news_date><![CDATA[08 June 2012]]></news_date>
        <news_published><![CDATA[true]]></news_published>
        <news_photo_original><![CDATA[http://www.sillysmart.org/Public/Files/news/new_york_4_640345cff7.jpg]]></news_photo_original>
        <news_photo_0><![CDATA[http://www.sillysmart.org/Public/Files/news/new_york_4_640345cff7_0.jpg]]></news_photo_0>
        <news_url><![CDATA[http://www.sillysmart.org]]></news_url>
        <article_category_id><![CDATA[1]]></article_category_id>
        <user_id><![CDATA[4]]></user_id>
        <user_email><![CDATA[guillaume.chatton@wandi.fr]]></user_email>           
        <user_name><![CDATA[Chatton]]></user_name>
        <user_firstname><![CDATA[Guillaume]]></user_firstname>
        <user_date><![CDATA[2011-11-23 17:02:34]]></user_date>
    </my_news>
</View>

Current collection of recordsets

If you want to easily getting the XML for a PDO collection of recordsets (obtained by the call to the function searchModels() eg), you can call the pdoToXML() function:

$all_news = $news->searchModels("news",array(),array(0 => array("column" => "user_id",
                                                                "value"  => 4,
                                                                "mode"   => "equal")));
$xml = $news->pdoToXML($xml, $all_news);

Will produce:

<View>
    <newss>
        <news>
            <news_id><![CDATA[4]]></news_id>
            <pk_lang><![CDATA[en]]></pk_lang>
            <news_title><![CDATA[Test]]></news_title>
            <news_content><![CDATA[<p>test</p>]]></news_content>
            <news_excerpt><![CDATA[<p>excerpt</p>]]></news_excerpt>
            <news_more><![CDATA[]]></news_more>
            <news_date><![CDATA[2011-08-23 18:40:24]]></news_date>
            <news_published><![CDATA[true]]></news_published>
            <news_photo><![CDATA[news/nyc_8b08c5343f.jpg]]></news_photo>
            <news_url><![CDATA[]]></news_url>
            <article_category_id><![CDATA[4]]></article_category_id>
            <user_id><![CDATA[4]]></user_id>
        </news>
        <news>
            <news_id><![CDATA[118]]></news_id>
            <pk_lang><![CDATA[en]]></pk_lang>
            <news_title><![CDATA[  fake_news_title ]]></news_title>
            <news_content><![CDATA[<p>news content en</p>]]></news_content>
            <news_excerpt><![CDATA[<p>Fake excerpt</p>]]></news_excerpt>
            <news_more><![CDATA[<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>]]></news_more>
            <news_date><![CDATA[2012-06-08 17:45:32]]></news_date>
            <news_published><![CDATA[true]]></news_published>
            <news_photo><![CDATA[news/new_york_4_640345cff7.jpg]]></news_photo>
            <news_url><![CDATA[http://www.sillysmart.org]]></news_url>
            <article_category_id><![CDATA[1]]></article_category_id>
            <user_id><![CDATA[4]]></user_id>
        </news>
    </newss>
</View>
  • 1st parameter $xml is the XML modified by the current action
  • 2nd parameter $recordsets is the array of PDO recordsets (often returned by searchModels() or SLS_SQL4::select())
  • 3rd parameter $options allows to specify transformations on some columns in the same way than toXML() function.
  • 4th parameter $nodeNames can specify the name by hand to form the XML node.
    The default name is 'tablenames/tablename'.

Abstraction

- If you want to know if your current Model is multilingual:

$model->isMultilanguage();

- If you want to retrieve the alias of the database of current Model:

$model->getDatabase();

- If you want to retrieve the name of the column corresponding to the primary key:

$model->getPrimaryKey();

- If you want to retrieve the table name of the current Model :

$model->getTable();

- If you want to get the next auto_increment id:

$model->giveNextId();

- If you want to know if the couple key / value is unique in the current Model:

$model->isUnique($key, $value);

- If you want to retrieve the table comment of the current Model :

$model->getTableComment($table);

- If you want to assign the table comment of the current Model:

$model->setTableComment($table, $comment);

- If you want to retrieve the comment of a column of the current Model:

$model->getColoumnComment($column);

- If you want to assign the comment of a column of the current Model:

$model->setColoumnComment($column, $comment);

Related :

References

  1. SLS_FrontModel - Mother class of models
  2. SLS_String - Tool SLS_String - String Treatment
  3. SLS_Date - Tool SLS_Date - Dates Handling
  4. SLS_SQL - SQL_Sql class - SQL Mother