idiorm常用方法大全
本文地址:http://tongxinmao.com/Article/Detail/id/25
ORM::configure('sqlite:./example.db'); ORM::configure('mysql:host=localhost;dbname=my_database'); ORM::configure('username', 'database_user'); ORM::configure('password', 'top_secret'); ORM::configure('driver_options', array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')); ORM::configure('logging', false); ORM::configure('logger', function($log_string, $query_time) { echo $log_string . ' in ' . $query_time; }); 多连接: // A named connection, where 'remote' is an arbitrary key name ORM::configure('mysql:host=localhost;dbname=my_database', null, 'remote'); ORM::configure('username', 'database_user', 'remote'); ORM::configure('password', 'top_secret', 'remote'); $person = ORM::for_table('different_person', 'remote')->find_one(5); ORM::configure('return_result_sets', true); ORM::configure('id_column', 'primary_key'); ORM::configure('id_column', array('pk_1', 'pk_2')); ORM::configure('id_column_overrides', array( 'person' => 'person_id', 'role' => 'role_id',)); You must enable logging for this setting to have any effect: ORM::get_last_query() ORM::get_query_log() $dbh=ORM::get_db(); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbh->exec(' CREATE TABLE tally( QID varchar(32) NOT NULL, AID integer NOT NULL, votes integer NOT NULL, PRIMARY KEY(QID,AID) )'); $person = ORM::for_table('person')->where('name', 'Fred Bloggs')->find_one(); $person = ORM::for_table('person')->find_one(5); $person = ORM::for_table('user_role')->find_one(array( 'user_id' => 34, 'role_id' => 10 )); $people = ORM::for_table('person')->find_many(); $females = ORM::for_table('person')->where('gender', 'female')->find_array(); $number_of_people = ORM::for_table('person')->count(); ORM::for_table('person')->find_result_set() ->set('age', 50) ->save(); foreach(ORM::for_table('person')->find_result_set() as $record) { echo $record->name; } Less than: $people = ORM::for_table('person')->where_lt('age', 10)->find_many(); Greater than: $people = ORM::for_table('person')->where_gt('age', 5)->find_many(); Less than or equal: $people = ORM::for_table('person')->where_lte('age', 10)->find_many(); Greater than or equal: $people = ORM::for_table('person')->where_gte('age', 5)->find_many(); where_id_is where_id_in where_null and where_not_null where_raw('(`age` = ? OR `age` = ?)', array(20, 25)) $people = ORM::for_table('person')->where_like('name', '%fred%')->find_many(); $people = ORM::for_table('person')->where_not_like('name', '%bob%')->find_many(); $people = ORM::for_table('person') ->where_any_is(array( array('name' => 'Joe', 'age' => 10), array('name' => 'Fred', 'age' => 20))) ->find_many(); // Creates SQL: SELECT * FROM `widget` WHERE (( `name` = 'Joe' AND `age` = '10' ) OR ( `name` = 'Fred' AND `age` = '20' )); $people = ORM::for_table('person')->where_in('name', array('Fred', 'Joe', 'John'))->find_many(); order_by_asc('name') order_by_desc order_by_expr('SOUNDEX(`name`)') group_by('name') group_by_expr("FROM_UNIXTIME(`time`, '%Y-%m')") limit(5)->offset(10) $people = ORM::for_table('person')->group_by('name')->having_not_like('name', '%bob%')->find_many(); $people = ORM::for_table('person')->select('name')->select('age')->find_many(); select('name', 'person_name') select_expr('COUNT(*)', 'count') select_many(array('first_name' => 'name'), 'age', 'height') select_expr('NOW()', 'timestamp') $distinct_names = ORM::for_table('person')->distinct()->select('name')->find_many(); join, inner_join, left_outer_join, right_outer_join, full_outer_join $results = ORM::for_table('person')->join('person_profile', array('person.id', '=', 'person_profile.person_id'))->find_many(); join('person_profile', 'person.id = person_profile.person_id') raw_join( 'JOIN (SELECT * FROM role WHERE role.name = ?)', array('person.role_id', '=', 'role.id'), 'role', array('role' => 'janitor')) ->min('height'); MIN, AVG, MAX and SUM $people = ORM::for_table('person')->raw_query('SELECT p.* FROM person p JOIN role r ON p.role_id = r.id WHERE r.name = :role', array('role' => 'janitor'))->find_many(); //join只取部分字段 $results = ORM::for_table('person') ->table_alias('p1') ->select('p1.*') ->select('p2.name', 'parent_name') ->join('person', array('p1.parent', '=', 'p2.id'), 'p2') ->find_many(); $person = ORM::for_table('person')->create(); // Returns array('first_name' => 'Fred', 'surname' => 'Bloggs', 'age' => 50) $data = $person->as_array(); // Returns array('first_name' => 'Fred', 'age' => 50) $data = $person->as_array('first_name', 'age'); $person->set_expr('updated', 'NOW()'); $person->save(); $person->id(); // $name_has_changed = $person->is_dirty('name'); $person->delete(); ->where_equal('zipcode', 55555) ->delete_many(); ->id(); ORM::get_db()->beginTransaction(); // Commit a transaction ORM::get_db()->commit(); // Roll back a transaction ORM::get_db()->rollBack(); //private
$people = ORM::for_table('person')->('SELECT p.* FROM person )
if (ORM::raw_execute('DROP TABLE my_table')) {
echo "Table dropped";
} else {
echo "Drop query failed";
}
上一篇:JavaScript base64 编码解码
下一篇:移动联通电信运营商手机号段分配