您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

yii怎么连数据库

2024/7/27 6:48:55发布42次查看
yii怎么连数据库?
深入理解yii2.0之连接数据库
yii使用pdo(php date object)连接各种各样的数据库,因此,几乎所有主流的数据库,yii都可以 很好地提供支持。这也是一个成熟框架所应具有的广泛适用性。
推荐学习:yii框架
在对数据库进行任何操作之前,都必须先与数据库服务器建立连接。在yii应用中,有一个专门的核心 组件(component)用于处理数据库连接,我们很容易可以在配置文件中找到他:
'components' => [ 'db' => [ 'class' => 'yii\db\connection', 'dsn' => 'mysql:host=localhost;dbname=yii2advanced', 'username' => 'root', 'password' => '', 'charset' => 'utf8', ], // ... ...],// ... ...
这里有人肯定已经猜到了,yii用 yii\db\connection 来表示数据库连接。这个connection实现了 对于pdo的一个简单封装,并掩盖了各种数据库的区别,实现了一个统一的开发接口。这样,使得你在 编程过程中,可以忽略绝大多数的数据库兼容问题,可以更加专注于功能开发。比如,你不用再担心在 mysql下不能使用money类型的字段等等。
数据库schema
说到实现connection独立于各种数据库,就不得不提到数据库schema。yii提供了各种主流的数据库 schema,你甚至可以自己写一个schema以适用自己独特的数据库管理系统(dbms)。与schema有关的类 有这么几个:
yii\db\schema 抽象类,用于描述各种不同的dbms的schema。
yii\db\tableschema 用于描述表结构。
yii\db\columnschema 用于描述字段信息。
yii\db\pgsql, yii\db\mysql, yii\db\sqlite, yii\db\mssql, yii\db\oci, yii\db\cubird 下的各种schema,用于具体描述各种dbms。
在 yii\db\connection 中,有一个 $schemamap 数组,用于建立pdo数据库驱动与具体的 schema 类间的映射关系:
public $schemamap = [ 'pgsql' => 'yii\db\pgsql\schema', // postgresql 'mysqli' => 'yii\db\mysql\schema', // mysql 'mysql' => 'yii\db\mysql\schema', // mysql 'sqlite' => 'yii\db\sqlite\schema', // sqlite 3 'sqlite2' => 'yii\db\sqlite\schema', // sqlite 2 'sqlsrv' => 'yii\db\mssql\schema', // newer mssql driver on ms windows hosts 'oci' => 'yii\db\oci\schema', // oracle driver 'mssql' => 'yii\db\mssql\schema', // older mssql driver on ms windows hosts 'dblib' => 'yii\db\mssql\schema', // dblib drivers on gnu/linux (and maybe other oses) hosts 'cubrid' => 'yii\db\cubrid\schema', // cubrid];
我们可以认为yii默认情况下支持上述数组中的10种dbms(6个schema),这在绝大多数情况下, 是完全足够的。万一你使用了超出这一范围的dbms,在确保兼容的情况下,你可以自己写一个schema, 使yii可以支持该dbms。
schema基类
yii\db\schema 是一个抽象类,具体的实现依赖于针对不同dbms的6个子类schema。擒贼先擒王, 读代码先读基类,我们就先来看看这个 yii\db\schema 吧:
abstract class schema extends object{ // 预定义16种基本字段类型,这16种类型是与dbms无关的,具体到特定的dbms时,yii会自动 // 转换成合适的数据库字段类型。 const type_pk = 'pk'; const type_bigpk = 'bigpk'; const type_string = 'string'; const type_text = 'text'; const type_smallint = 'smallint'; const type_integer = 'integer'; const type_bigint = 'bigint'; const type_float = 'float'; const type_decimal = 'decimal'; const type_datetime = 'datetime'; const type_timestamp = 'timestamp'; const type_time = 'time'; const type_date = 'date'; const type_binary = 'binary'; const type_boolean = 'boolean'; const type_money = 'money'; // 加载表schema,需要子类具体实现 abstract protected function loadtableschema($name); // ... ...}
yii\db\schema 一上来就先针对各dbms间差异最明显的字段数据类型进行统一,提供了16种基本的 字段类型。这16种类型与dbms无关,在具体到特定的dbms时,yii会自动转换成合适的数据库字段类型 。我们在编程中,若需要指定字段类型,就使用这16种。这样的话,就不用考虑使用的类型具体的dbms 是否支持的问题了。
这16种类型看着就知道是什么意思,我们就不展开讲了。
yii\db\schema::loadtableschema() 是整个基类中最重要的一语句了,他定义了一个函数,用于 加载表的schema,需要由子类针对特定的dbms实现。这里,我们以 yii\db\mysql\schema 子类为 例来讲解:
class schema extends \yii\db\schema{ // 定义一个数据类型的映射关系 public $typemap = [ 'tinyint' => self::type_smallint, 'bit' => self::type_integer, 'smallint' => self::type_smallint, 'mediumint' => self::type_integer, 'int' => self::type_integer, 'integer' => self::type_integer, 'bigint' => self::type_bigint, 'float' => self::type_float, 'double' => self::type_float, 'real' => self::type_float, 'decimal' => self::type_decimal, 'numeric' => self::type_decimal, 'tinytext' => self::type_text, 'mediumtext' => self::type_text, 'longtext' => self::type_text, 'longblob' => self::type_binary, 'blob' => self::type_binary, 'text' => self::type_text, 'varchar' => self::type_string, 'string' => self::type_string, 'char' => self::type_string, 'datetime' => self::type_datetime, 'year' => self::type_date, 'date' => self::type_date, 'time' => self::type_time, 'timestamp' => self::type_timestamp, 'enum' => self::type_string, ];}
yii\db\mysql\schema 先是定义了一个映射关系,这个映射关系是mysql数据库的字段类型与前面 我们提到的16种基本数据类型的映射关系。也就是说,基于mysql的schema,使用mysql的字段类型,会 转换成统一的16种基本数据类型。
表信息(table schema)
yii\db\tableschema 类用于描述数据表的信息:
class tableschema extends object{ public $schemaname; // 所属的schema public $name; // 表名,不包含schema部分 public $fullname; // 表的完整名称,可能包含一个schema前缀。 public $primarykey = []; // 主键 public $sequencename; // 主键若使用sequence,该属性表示序列名 public $foreignkeys = []; // 外键 public $columns = []; // 字段 // ... ...}
从上面的代码来看, yii\db\tableschema 比较简单。上述的属性看一看就大致可以了解是干什么 用的。这里我们点一点,了解下就可以了。
列信息(column schema)
yii\db\columnschema 类用于描述一个字段的信息,让我们来看一看:
class columnschema extends object{ public $name; // 字段名 public $allownull; // 是否可以为null /** * @var string abstract type of this column. possible abstract types include: * string, text, boolean, smallint, integer, bigint, float, decimal, datetime, * timestamp, time, date, binary, and money. */ public $type; // 字段的类型 /** * @var string the php type of this column. possible php types include: * `string`, `boolean`, `integer`, `double`. */ public $phptype; // 字段类型对应的php数据类型 /** * @var string the db type of this column. possible db types vary according to the type of dbms. */ public $dbtype; public $defaultvalue; // 字段默认值 public $enumvalues; // 若字段为枚举类型,该属性用于表示可供枚举的值 /** * @var integer display size of the column. */ public $size; public $precision; // 若字段为数值,该属性用于表示精度 /** * @var integer scale of the column data, if it is numeric. */ public $scale; /** * @var boolean whether this column is a primary key */ public $isprimarykey; // 是否是主键 public $autoincrement = false; // 是否是自增长字段 /** * @var boolean whether this column is unsigned. this is only meaningful * when [[type]] is `smallint`, `integer` or `bigint`. */ public $unsigned; // 是否是unsigned,仅对支持的类型有效 public $comment; // 字段描述信息 /** * converts the input value according to [[phptype]] after retrieval from the database. * if the value is null or an [[expression]], it will not be converted. * @param mixed $value input value * @return mixed converted value */ public function phptypecast($value) { if ($value === '' && $this->type !== schema::type_text && $this->type !== schema::type_string && $this->type !== schema::type_binary) { return null; } if ($value === null || gettype($value) === $this->phptype || $value instanceof expression) { return $value; } switch ($this->phptype) { case 'resource': case 'string': return is_resource($value) ? $value : (string) $value; case 'integer': return (int) $value; case 'boolean': return (bool) $value; case 'double': return (double) $value; } return $value; } /** * converts the input value according to [[type]] and [[dbtype]] for use in a db query. * if the value is null or an [[expression]], it will not be converted. * @param mixed $value input value * @return mixed converted value. this may also be an array containing the value as the first element * and the pdo type as the second element. */ public function dbtypecast($value) { // the default implementation does the same as casting for php but it should be possible // to override this with annotation of explicit pdo type. return $this->phptypecast($value); }}
以上就是yii怎么连数据库的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product