>使用步骤
>>创建数据库
>>配置数据库连接
>>创建活动记录的模型
>>创建操作
>>创建渲染用的视图
>>运行
>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系列
>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