From aMember Pro Documentation
Revision as of 07:03, 17 November 2011 by Alex-scott (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:

 * 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';
     * 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;

To define new model, you just need to define these 2 classes and put them to include path, and set $_key, $_table variables to table class. That is all. After that, you can automatically run different operations on these classes. For example:

$ban = Am_Di::getInstance()->banRecord; // create new instance of Ban class
$ban->type = 'ip';
$ban->value = "";
echo $ban->pk(); 
echo $ban->ban_id; // the same output - primary key
$ban->comment = "new comment";
$ban->update(); // commit changes to database
$ban->refresh(); // reload record from database - it happens automatically after update and insert
$ban->delete(); // delete record from database
$banTable = Am_Di::getInstance()->banTable; // get singleton instance of BanTable class
$banTable->load(11); // loads Ban record with ban_id = 11, throws exception if not found
$banTable->findFirstByType('ip'); // @return first Ban with type = 'ip' or null if not null
$banTable->findBy(array('type'=>'ip', 'ip' => '')); // @return array of Ban records
$banTable->deleteByValue(''); // delete records WHERE value=''
$banTable->selectObjects("SELECT * FROM ?_ban WHERE type='ip'"); // @return array of Ban records

Am_Record_WithData, Am_Table_WithData

To store additional and optional data for table records, we have defined special class and routines. If table and record subclassed from .._WithTable parents, there are special functions available. Quick samples:

Invalid language.

You need to specify a language like this: <source lang="html4strict">...</source>

Supported languages for syntax highlighting:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic

$user = Am_Di::getInstance()->userTable->findFirstByEmail('xx@example.com');
$user->data()->set('xx', 'XX');
$user->update(); // updates user record and related "data()" records or ...
$user->data()->update() // updates only "data()" records
// this will create a record in ?_data table with name='xx' and value='XX' and linked to user table record with given id

$user = Am_Di::getInstance()->userTable->findFirstByEmail('xx@example.com');
echo $user->data()->get('xx'); // will output "XX"