Download the PHP package shen2/mypdo without Composer

On this page you can find all versions of the php package shen2/mypdo. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package mypdo

MyPDO

MyPDO是一个用php编写的依赖PDO的ORM类(Object Relational Mapping),通过它你可以用面向对象的方式来写sql语句,也可以以面向对象的方式来操作每一条数据库记录。

MyPDO基于Zend Framework 1.x版本中的Zend_Db组件修改而来,经过彻底的修改和优化之后,去掉了对Zend Framework其他组件的依赖。能达到很高的性能,同时给你的编程带来极大的便利。

MyPDO由沈振宇开发,先后用于图虫网和多说网的服务器端php程序,经过2年多生产环境的考验,可靠稳定。

特性

性能方面

编程模式

希望MyPDO能让你的php编程变得更优雅,更快乐。

局限性

环境和依赖

类的基本说明

初始化方法

出于性能的考虑,即使你使用了autoload机制,仍然建议你显式地主动require以上那些php类定义文件,这样可以减少发动autoload的次数,避免不必要的性能浪费。

DataObject类

DataObject是对数据表的抽象,DataObject的静态方法相当于对数据表的操作,实例化的DataObject是数据库记录对象,调用DataObject的动态方法,相当于对数据库记录的操作。 DataObject其实是一个抽象的基类,真正使用的时候需要将它派生成自己的类。

如果你的数据表是双重主键,并且第二重主键是自增的,可以写成:

protected static $_primary = array('site_id', 'user_id');
protected static $_identity = 1;                //  1代表$_primary[1],即’user_id‘是自增主键

MyPDO\Select 的用法

通常,我们通过已经派生的MyPDO\DataObject类的静态方法select()来创建 MyPDO\Select 实例。

$select = User::select();
// select * from `pre_users`;

where()

为了避免出现sql注入漏洞,所有包含变量的where条件,都应使用?来进行转义。

$select = User::select()
    ->where('name like ?', '小钢炮');
// select * from `pre_users` where name like '小钢炮';

如果有多个where条件,以and相连,直接调用多次where()就可以了。

$select = User::select()
    ->where('gender = ?', 'male')
    ->where('age > ?', 40);
// select * from `pre_users` where gender = 'male' and age > 40;

order()

order()方法的参数就是排序字段名,asc/desc直接写在字符串里。

$select = User::select()
    ->order('created_at desc');
// select * from `pre_users` order by created_at desc;

如果有多个order字段,直接调用多次order函数就可以了。

$select = User::select()
    ->order('name asc')
    ->order('email asc');
// select * from `pre_users` order by name asc, email asc;

limit()

第一个参数是limit的字段数,第二个参数是offset(默认是0,可省略)

$select = User::select()
    ->order('name asc')
    ->order('email asc')
    ->limit(10, 30);
// select * from `pre_users` order by name asc, email asc limit 10 offset 30;

指定 select 的字段

如果你想获得指定的字段,而不需要所有字段(*),可以使用selectCol函数,比如:

$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`;

join()

例如现在有 pre_users 表和 pre_posts 表,我们要把posts表的查询结果和users表join。可以写成:

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;

assemble()

有时候你不确定写出的MyPDO\Select对象在执行的时候会转换成什么SQL语句,可以使用assemble()方法预览SQL语句

echo $select->assemble();

fetch封装方法

每个数据表类有四种种常用的调用方式:fetchAll(), fetchRow(), fetchOne() 和 find()

fetchAll()

$userList = User::select()
    ->where('created_at > ?', '2012-12-21')
    ->order('image_count desc')
    ->limit(5)
    ->fetchAll();

返回值是一个MyPDO\Statement,可以直接进行foreach迭代或者count()

fetchRow()

获取一行的方法

$user = User::select()
    ->where('email_hash = md5(?)', 'd269c7b5b75e3f6fd794e68e889b5daa')
    ->fetchRow();

不需要额外写limit(1),因为fetchRow()方法会自动给sql语句增加 limit 1

fetchOne()

如果结果集是单行单列的,用fetchOne()可以直接得到这个值

$count = User::selectCol('count(*)')
    ->where('created_at > ?', '2012-12-21')
    ->fetchOne();

find()

主键查询肯定是用得最广泛的,使用find()方法可以简化主键查询的过程。

$user = User::find(40)->current();

这样就可以查询到主键为40的user对象,如果记录不存在,返回值是null。

如果想通过多个ID一次查询多条记录,可以写成:

$userList = User::find(array(1,2,3,4,5));

返回值是一个MyPDO\Statement,可以直接foreach迭代。注意,结果集中的对象顺序未必和find的参数相同,结果集中的对象数量也可能小于find的参数。

find()还支持多重主键的查询:

$relationship = Friendship::find(123,456)->current();

MyPDO\Statement

$select->fetchAll()的返回值是一个MyPDO\Statement对象,这个对象未必含有查询的结果集,而有可能结果还在数据库中进行处理。

只有当你主动调用count(), fetch(),或者对它进行foreach迭代的时候,程序才会强制等待mysql数据库返回全部查询结果。

如果这给你带来了困扰,可以使用fetch()方法,获得真正的结果数据集(一个SplFixedArray对象):

$fixedArray = $select->fetchAll()->fetch();

All versions of mypdo with dependencies

PHP Build Version
Package Version
Requires php Version >=5.4.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package shen2/mypdo contains the following files

Loading the files please wait ....