php mysql orm_初探PHP ORM框架Doctrine

概述

环境:debian

Doctrine是PHP的一个ORM框架,symfony的默认用的ORM就是Doctrine。

安装Composer

首先安装Composer,Composer是PHP依赖管理工具,类似Debian的apt,Java的Maven,安装说明可参考:http://docs.phpcomposer.com/00-intro.html

mkdir composer

curl -sS https://getcomposer.org/installer | php

这样就把composer.phar下载了到了composer文件夹了。

为了目录根清晰,我们新建一个和composer同级的目录doctrine.

cd .. && mkdir doctrine && cd doctrine

在doctrine文件夹里,编写依赖文件composer.json,内容如下:

{

"require": {

"doctrine/orm": "2.4.*",

"symfony/yaml": "2.*"

},

"autoload": {

"psr-0": {

"": "src/"

}

}

}

安装依赖../composer/composer.phar install

有时会报异常:

[Composer\Downloader\TransportException]

Your configuration does not allow connection to

http://packagist.org. See https://getcomposer.org/doc/06-config.md#secure-http

for details.

可以多试几次,如果还不行可以尝试添加一个配置项:

{

"require": {

"doctrine/orm": "2.4.*",

"symfony/yaml": "2.*"

},

"autoload": {

"psr-0": {

"": "src/"

}

},

"config": {

"secure-http": false

}

}

安装完依赖后,如果你尝试运行 vendor/bin/doctrine,会得到类似这样的提示:

You are missing a “cli-config.php” or “config/cli-config.php” file in your

project, which is required to get the Doctrine Console working. You can use the

following sample as a template:

use Doctrine\ORM\Tools\Console\ConsoleRunner;

// replace with file to your own project bootstrap

require_once ‘bootstrap.php’;

// replace with mechanism to retrieve EntityManager in your app

$entityManager = GetEntityManager();

return ConsoleRunner::createHelperSet($entityManager);

告诉缺少cli-config.php文件,于是添加如下两个文件bootstrap.php和cli-config.php:

use Doctrine\ORM\Tools\Setup;

require_once "vendor/autoload.php";

// Create a simple "default" Doctrine ORM configuration for XML Mapping

$isDevMode = true;

$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src"), $isDevMode);

// or if you prefer yaml or annotations

//$config = Setup::createXMLMetadataConfiguration(array(__DIR__."/config/xml"), $isDevMode);

//$config = Setup::createYAMLMetadataConfiguration(array(__DIR__."/config/yaml"), $isDevMode);

// database configuration parameters

/*$conn = array(

'driver' => 'pdo_sqlite',

'path' => __DIR__ . '/db.sqlite',

);*/

$conn = array(

'driver' => 'pdo_mysql',

'user' => 'root',

'password' => '',

'dbname' => 'foo',

);

// obtaining the entity manager

$entityManager = \Doctrine\ORM\EntityManager::create($conn, $config);

<?php

// cli-config.php

require_once "bootstrap.php";

$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(

'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($entityManager)

));

return $helperSet;

创建数据库和表

配置好bootstrap.php中的数据库以及目录(src),编写entity,如Product.php

/**

* @Entity @Table(name="products")

*/

class Product

{

/** @Id @Column(type="integer") @GeneratedValue */

protected $id;

/** @Column(type="string") */

protected $name;

public function getId()

{

return $this->id;

}

public function getName()

{

return $this->name;

}

public function setName($name)

{

$this->name = $name;

}

}

先登录Mysql创建数据库foo

create database foo;

然后可以执行

vendor/bin/doctrine orm:schema-tool:create

即可创建数据库表Products.

自主封装的PHP ORM框架,面向对象的PDO数据库操作,API框架,支持Get/Post/Put/Delete多种请求方式。 代码示例: <?php use Models\User; require '../application.php'; require '../loader-api.php'; //适合查询,如:获取用户列表或者单个用户信息 execute_request(HttpRequestMethod::Get, function() { $action = request_action(); //判断是否存在 if ($action == 1) { list($type, $value) = filter_request(array( request_int('type', 1, 2, 3), //1.用户名 2.邮箱 3.手机号 request_string('value'))); $type_field_map = array( 1 => User::$field_username, 2 => User::$field_email, 3 => User::$field_phone ); if ($type == 2 && !is_email($value) || $type == 3 && !is_mobilephone($value)) { die_error(USER_ERROR, $type_field_map[$type]['name'] . '格式无效'); } $user = new User(); $user->set_where_and($type_field_map[$type], SqlOperator::Equals, $value); $result = $user->exists(create_pdo()); echo_result($result ? 1 : 0); //存在返回1,不存在返回0 } //查询单条信息 if ($action == 2) { list($userid) = filter_request(array( request_userid())); //查询单条数据 $user = new User($userid); //set_query_fields可以指定查询字段,下面两种写法均可 //$user->set_query_fields('userid, username, email'); //$user->set_query_fields(array(User::$field_userid, User::$field_username, User::$field_email)); //还可设置where条件进行查询 //$user->set_where_and(User::$field_status, SqlOperator::Equals, 3); //$user->set_where_and(User::$field_truename, SqlOperator::IsNullOrEmpty); //$user->set_where_and(User::$field_age, SqlOperator::In, array(27, 29)); //$user->set_where_and(User::$field_regtime, SqlOperator::LessThan, '-6 month'); //创建数据库连接 $db = create_pdo(); $result = $user->load($db, $user); //也可以用Model类的静态方法 //$result = Model::load_model($db, $user, $user); if (!$result[0]) die_error(PDO_ERROR_CODE, '获取用户信息时数据库错误'); if (!$user) di
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值