PHP code example of shen2 / mdo
1. Go to this page and download the library: Download shen2/mdo library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
shen2 / mdo example snippets
e 'MDO/Adapter.php';
equire 'MDO/TableSelect.php';
'localhost', // 主机名
'username' => 'username', // 用户名
'password' => 'password', // 密码
'dbname' => 'dbname', // 数据库名
'port' => 3306, // 端口号,可省略
'profiler' => false, // 是否启用Profiler,默认false
'persistent'=> true, // 是否持久化连接
'charset' => 'utf8', // 默认字符集
'fetchMode' => PDO::FETCH_ASSOC,//PDO的默认fetch模式
);
$db = new MDO\Adapter($configs);
MDO\DataObject::setDefaultAdapter($db);
$configs = array(
'socket' => '/var/run/mysqld/mysqld.sock',
'host' => 'localhost', // 为了让最终host参数能变成 p:localhost,只有这样才能生效
'persistent'=> true,
//...
class User extends MDO\DataObject{
protected static $_schema = 'dbname'; // 数据库的名字,默认是null,代表当前数据库
protected static $_name = 'pre_users'; // 数据表的名字
protected static $_primary = array('user_id'); // 主键字段,必须是数组
protected static $_sequence = true; // 主键是否是自增的
protected static $_defaultValues = array(
); // 默认数据,可以省略
// 你自己的方法...
}
protected static $_primary = array('site_id', 'user_id');
protected static $_identity = 1; // 1代表$_primary[1],即’user_id‘是自增主键
$select = User::select();
// select * from `pre_users`;
$select = User::select()
->where('name like ?', '小钢炮');
// select * from `pre_users` where name like '小钢炮';
$select = User::select()
->where('gender = ?', 'male')
->where('age > ?', 40);
// select * from `pre_users` where gender = 'male' and age > 40;
$select = User::select()
->order('created_at desc');
// select * from `pre_users` order by created_at desc;
$select = User::select()
->order('name asc')
->order('email asc');
// select * from `pre_users` order by name asc, email asc;
$select = User::selectCol('count(*)');
// select count(*) from `pre_users`;
$select = User::selectCol(array('user_id', 'name', 'email'));
// select user_id, name, email from `pre_users`;
$select = User::selectCol(array('id' => 'user_id', 'name', 'email'));
// select user_id as id, name, email from `pre_users`;
Post::select(true)
->joinInner('pre_users', 'pre_users.user_id = pre_posts.author_id', array('uid'=>'user_id', 'date'=>'updated'))
->where(...)
// select `pre_posts`.*, `pre_users`.user_id as id, `pre_users`.updated as date from `pre_posts` inner join pre_users on pre_users.user_id = pre_posts.author_id;
echo $select->assemble();
$userList = User::select()
->where('created_at > ?', '2012-12-21')
->order('image_count desc')
->limit(5)
->fetchAll();
$user = User::select()
->where('email_hash = md5(?)', 'd269c7b5b75e3f6fd794e68e889b5daa')
->fetchRow();
$count = User::selectCol('count(*)')
->where('created_at > ?', '2012-12-21')
->fetchOne();
$user = User::find(40)->current();
$userList = User::find(array(1,2,3,4,5));
$relationship = Friendship::find(123,456)->current();
$fixedArray = $select->fetchAll()->fetch();