**YII** 系列七Database

>使用步骤


>>创建数据库

>>配置数据库连接

>>创建活动记录的模型

>>创建操作

>>创建渲染用的视图

>>运行


>foreach循环是PHP语句,在html语句中的穿插如下:


<span style="font-size:14px;"><?php foreach ($countries as $country): ?>
    <li>
        <?= Html::encode("{$country->name} ({$country->code})") ?>:
        <?= $country->population ?>
    </li>
<?php endforeach; ?></span>
//巧妙地解决了php中输出html语句需要使用html组成的字符串的问题

>&model= new user();与$query=User::find()生成的结果是不同的


前者是得到一个对象,可以通过调用属性和方法,进行操作;前者的道德是Activerecord

后者形成的是一个对象数组,本质是数组,可以调用数组的相关方法。后者得到的是Activequery


>数据库对象的使用activeForm


作用:it is the base class for classex representing relational data in terms of objects;

>>返回数据库和表的一些性质

activeAttributes()返回性质的名称在当前场景中

attributes() return the list of all attribute names of the model;

equals($record) returns a value indicatino whether the fiven active record is the same as the current one

primaryKey()返回主键名称,放在数组中

tabelName()返回表格的名称declares the name of the database table associated withe this AR class 


>>delete系列

delete() the table row corresponding to this active record; attention:veforeDelete();afertDelete()

deleteall($condition='',$params=[]) delete rows in the table usein the provided conditions,参数使用where中的参数

deleteInternal()  deletes an ActiveRecord without considering transction.


>>find系列:

find() creates an activequery instance fro query prupose.能够调用yii\db\activequery和yii\db\active\queryinterface中的方法,进行限定

findByCondition($condition,$one) finds ActiveRecord instances by the geiven condition.

findAll() returns a list of active record models that match the specified primary key values or a set of column valust;参数可以是a scalar value/an array scalar values/an array of name-value pairs.

findOne()returns a single(the first to be queryed) active record model instance by a primary key or an array of column values

findBySql($aql,$params=[ ])使用sql语句进行查询,其中$sql是sql字符串


>>get系列

getDb returns the database connerction used by thiis AR class.

getTableSchema() returns the schema infrormation of the DV tavle associated with AR class


>>insert系列

insert($runValidation= true,$attributes =null) inserts a row into the associated database table usering the attribute values of this record.

insertInternal() inserts an Active Recored into DB without considering transaction.

updata系列

update($runvalidation =true,$attributeNames = null)
updateAll(['status'=>1],'status=2')updates the whole table using the provided attribute values and conditions 
updateAllConters() updates the whole table using the provided counter changes and conditioons.

>get到一个新的覆盖原函数的方法


<span style="font-size:14px;">class Customer extends ActiveRecord
{
    public static function find()
    {
        return parent::find()->where(['deleted' => false]);
    }
}

// Use andWhere()/orWhere() to apply the default condition
// SELECT FROM customer WHERE `deleted`=:deleted AND age>30
$customers = Customer::find()->andWhere('age>30')->all();

// Use where() to ignore the default condition
// SELECT FROM customer WHERE age>30
$customers = Customer::find()->where('age>30')->all();</span>
//使用parent调用覆盖之前的函数,有点意思


>yii\helpers\html(补充上一节中类的常用属性)



>类使用的进度表


>>yii\web\controller

>>yii\db\baseActiveRecord

>>yii\db\ActiveRecord

>>yii\db\activeQuery

>>yii\data\pagination

>>yii\widgets\LinkPager

>>yii\web\controller

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值