Home > Backend Development > PHP Tutorial > Detailed examples of Yii model query techniques based on arrays and objects, yiimodel_PHP tutorial

Detailed examples of Yii model query techniques based on arrays and objects, yiimodel_PHP tutorial

WBOY
Release: 2016-07-12 09:02:19
Original
710 people have browsed it

Detailed examples of Yii's array- and object-based Model query techniques, yiimodel

This article describes Yii's array- and object-based Model query techniques with examples. Share it with everyone for your reference, the details are as follows:

For a Model Post, there are the following 4 query methods, which return objects or object arrays.

//查找满足指定条件的结果中的第一行 find the first row satisfying the specified condition
$post=Post::model()->find($condition,$params);
//查找具有指定主键值的那一行 find the row with the specified primary key
$post=Post::model()->findByPk($postID,$condition,$params);
//查找具有指定属性值的行 find the row with the specified attribute values
$post=Post::model()->findByAttributes($attributes,$condition,$params);//未找到返回null
//通过指定的SQL 语句查找结果中的第一行 find the first row using the specified SQL statement
$post=Post::model()->findBySql($sql,$params);

Copy after login

If the find method finds a row that meets the query conditions, it will return a Post instance whose attributes contain the values ​​of the corresponding columns in the data table row. Then we can read the loaded value like a normal object property, such as echo $post->title;. The find method will return null if nothing is found in the database using the given query criteria.

When calling find, we use $condition and $params to specify the query conditions. Here $condition can be a WHERE string in the SQL statement and $params is an array of parameters whose values ​​should be bound to the placeholders in $condation. For example: Suppose we query the data with postID = 10

// find the row with postID=10
$post=Post::model()->find('postID=:postID', array(':postID'=>10));

Copy after login

The condition $condition is the where part in our sql. What about the parameters? They are passed through params, but the name is added with ":".

YII has a CDbCriteria class to construct queries. If we query the title with a postId of 10, CdbCriteria is constructed like this

$criteria=new CDbCriteria;
$criteria->select='title'; // only select the 'title' column
$criteria->condition='postID=:postID';
$criteria->params=array(':postID'=>10);
$post=Post::model()->find($criteria); // $params is not needed

Copy after login

An alternative to CDbCriteria is to pass an array to the find method. The keys and values ​​of the array respectively correspond to the attribute names and values ​​of the criterion. The above example can be rewritten as follows:

$post=Post::model()->find(array(
  'select'=>'title',
  'condition'=>'postID=:postID',
  'params'=>array(':postID'=>10),
));

Copy after login

Of course also applies to findAll()

self::$_items[$type]=array();
$models=self::model()->findAll(array(
  'condition'=>'type=:type',
  'params'=>array(':type'=>$type),
  'order'=>'position',
));

Copy after login

When a query condition is about matching several columns by specified values, we can use findByAttributes(). We make the $attributes parameter an array of values ​​indexed by column name.
$attributes in findByAttributes is the name of the field. How to query if the title is abc? See below
Copy code The code is as follows: Post::model()->findByAttributes(array('title'=>'abc'))

Other methods:

1. $admin=Admin::model()->findAll($condition,$params);

This method is to query a collection based on a condition, such as:
Copy code The code is as follows: findAll("username=:name",array(":name"=>$username));
2. $admin=Admin::model()->findAllByPk($postIDs,$condition,$params);
findAllByPk($id,"name like ':name' and age=:age" ,array(':name'=>$name,'age'=>$age));
This method is to query a collection based on the primary key. Multiple primary keys can be used, such as:
Copy code The code is as follows: findAllByPk(array(1,2));
3. $admin=Admin::model()->findAllByAttributes($attributes,$condition,$params);

This method is to query a collection based on conditions, which can be multiple conditions. Put the conditions into an array, such as:
Copy code The code is as follows: findAllByAttributes(array('username'=>'admin'));
4. $admin=Admin::model()->findAllBySql($sql,$params);

This method is to query an array based on a SQL statement, such as:
Copy code The code is as follows: findAllBySql("select *from admin where username=:name",array(':name'=>'admin'));

2. Method of querying objects

1. $admin=Admin::model()->findByPk($postID,$condition,$params);

Query an object based on the primary key, such as: Copy code The code is as follows: findByPk(1);
2. $row=Admin::model()->find($condition,$params);

Query a set of data based on a condition, there may be multiple data, but it only returns the first row of data, such as:
Copy code The code is as follows: find('username=:name',array(':name'=>'admin'));
3. $admin=Admin::model()->findByAttributes($attributes,$condition,$params);

This method is to query a set of data based on conditions, which can be multiple conditions. Put the conditions into the array, and it will also query the first piece of data, such as:
Copy code The code is as follows: findByAttributes(array('username'=>'admin'));
4. $admin=Admin::model()->findBySql($sql,$params);

This method is to query a set of data based on SQL statements. It also queries the first piece of data, such as:
Copy code The code is as follows: findBySql("select *from admin where username=:name",array(':name'=>'admin'));
5. Put together a method to obtain SQL, and query an object based on find

$criteria=new CDbCriteria;
$criteria->select='username'; // only select the 'title' column
$criteria->condition='username=:username';
$criteria->params=array(':username=>'admin');
$post=Post::model()->find($criteria); // $params is not needed

Copy after login

3. Query the number and determine whether the query has results

1. $n=Post::model()->count($condition,$params);

该方法是根据一个条件查询一个集合有多少条记录,返回一个int型数字,如
复制代码 代码如下:count("username=:name",array(":name"=>$username));
2、$n=Post::model()->countBySql($sql,$params);

该方法是根据SQL语句查询一个集合有多少条记录,返回一个int型数字,如
复制代码 代码如下:countBySql("select *from admin where username=:name",array(':name'=>'admin'));
3、$exists=Post::model()->exists($condition,$params);
该方法是根据一个条件查询查询得到的数组有没有数据,如果有数据返回一个true,否则没有找到

四、添加的方法

$admin=new Admin;
$admin->username=$username;
$admin->password=$password;
if($admin->save()>0){
  echo "添加成功";
}else{
  echo "添加失败";
}

Copy after login

五、修改的方法

1、Post::model()->updateAll($attributes,$condition,$params);

$count = Admin::model()->updateAll(array('username'=>'11111','password'=>'11111'),'password=:pass',array(':pass'=>'1111a1'));
if($count>0){
  echo "修改成功";
}else{
  echo "修改失败";
}

Copy after login

2、Post::model()->updateByPk($pk,$attributes,$condition,$params);

$count = Admin::model()->updateByPk(1,array('username'=>'admin','password'=>'admin'));
$count = Admin::model()->updateByPk(array(1,2),array('username'=>'admin','password'=>'admin'),'username=:name',array(':name'=>'admin'));
if($count>0){
  echo "修改成功";
}else{
  echo "修改失败";
}

Copy after login

$pk代表主键,可以是一个也可以是一个集合,$attributes代表是要修改的字段的集合,$condition代表条件,$params传入的值

3、Post::model()->updateCounters($counters,$condition,$params);

$count =Admin::model()->updateCounters(array('status'=>1),'username=:name',array(':name'=>'admin'));
if($count>0){
  echo "修改成功";
}else{
  echo "修改失败";
}

Copy after login

array('status'=>1)代表数据库中的admin表根据条件username='admin',查询出的所有结果status字段都自加1

六、删除的方法

1、Post::model()->deleteAll($condition,$params);

$count = Admin::model()->deleteAll('username=:name and password=:pass',array(':name'=>'admin',':pass'=>'admin'));
      $id=1,2,3
      deleteAll('id in(".$id.")');删除id为这些的数据
if($count>0){
  echo "删除成功";
}else{
  echo "删除失败";
}

Copy after login

2、Post::model()->deleteByPk($pk,$condition,$params);

$count = Admin::model()->deleteByPk(1);
$count = Admin::model()->deleteByPk(array(1,2),'username=:name',array(':name'=>'admin'));
if($count>0){
  echo "删除成功";
}else{
  echo "删除失败";
}

Copy after login

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

您可能感兴趣的文章:

  • PHP YII框架开发小技巧之模型(models)中rules自定义验证规则
  • Yii2使用小技巧之通过 Composer 添加 FontAwesome 字体资源
  • YiiFramework入门知识点总结(图文教程)
  • PHP的Yii框架的常用日志操作总结
  • Yii学习总结之数据访问对象 (DAO)
  • Yii学习总结之安装配置
  • YII路径的用法总结
  • Yii使用技巧大汇总

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1085884.htmlTechArticleYii基于数组和对象的Model查询技巧实例详解,yiimodel 本文实例讲述了Yii基于数组和对象的Model查询技巧。分享给大家供大家参考,具体如下:...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template