API/Record

From aMember Pro Documentation
Revision as of 07:26, 28 June 2011 by Alex (Talk | contribs)

Jump to: navigation, search

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;
    }
}