Difference between revisions of "API/Record"

From aMember Pro Documentation
Jump to: navigation, search
(Created page with "There are 2 classes that offers easy access to database records and tables: ''Am_Record'' and ''Am_Table''")
 
Line 1: Line 1:
 
There are 2 classes that offers easy access to database records and tables: ''Am_Record'' and ''Am_Table''
 
There are 2 classes that offers easy access to database records and tables: ''Am_Record'' and ''Am_Table''
 +
 +
 +
There is an example of model code with an utility function:
 +
 +
<source>
 +
<?php
 +
/**
 +
* Class represents records from table ban
 +
* {autogenerated}
 +
* @property int $ban_id
 +
* @property string $type
 +
* @property string $value
 +
* @property string $comment
 +
* @see Am_Table
 +
*/
 +
class Ban extends Am_Record {
 +
}
 +
 +
class BanTable extends Am_Table {
 +
    protected $_key = 'ban_id';
 +
    protected $_table = '?_ban';
 +
    protected $_recordClass = 'Ban';
 +
 +
    /**
 +
    * Check if params matches the records in "ban" table
 +
    *
 +
    * @param array $params like (array('ip' => 'xx', 'email'=>'xx',
 +
    *              'login' => 'xxx')
 +
    * @return array() if ok, array of keys matched like array('ip','login')
 +
    */
 +
    function findBan(array $params)
 +
    {
 +
        $db = $this->_db;
 +
        $where = array();
 +
        foreach ($params as $k => $v)
 +
            $where[] = sprintf("(`type` = %s AND %s LIKE `value` )",
 +
                $db->escape($k), $db->escape($v));
 +
        if (!$where) return array();
 +
        $arr = $db->selectCol($sql = "SELECT DISTINCT `type` FROM ?_ban WHERE " . join(" OR ", $where));
 +
        return $arr;
 +
    }
 +
}
 +
</source>

Revision as of 07:26, 28 June 2011

There are 2 classes that offers easy access to database records and tables: Am_Record and Am_Table


There is an example of model code with an utility function:

<?php
/**
 * Class represents records from table ban
 * {autogenerated}
 * @property int $ban_id 
 * @property string $type 
 * @property string $value 
 * @property string $comment 
 * @see Am_Table
 */
class Ban extends Am_Record {
}
 
class BanTable extends Am_Table {
    protected $_key = 'ban_id';
    protected $_table = '?_ban';
    protected $_recordClass = 'Ban';
 
    /**
     * Check if params matches the records in "ban" table
     * 
     * @param array $params like (array('ip' => 'xx', 'email'=>'xx', 
     *              'login' => 'xxx')
     * @return array() if ok, array of keys matched like array('ip','login')
     */
    function findBan(array $params)
    {
        $db = $this->_db;
        $where = array();
        foreach ($params as $k => $v)
            $where[] = sprintf("(`type` = %s AND %s LIKE `value` )", 
                $db->escape($k), $db->escape($v));
        if (!$where) return array();
        $arr = $db->selectCol($sql = "SELECT DISTINCT `type` FROM ?_ban WHERE " . join(" OR ", $where));
        return $arr;
    }
}