zoukankan      html  css  js  c++  java
  • yii 数据库 Active Record

    // 查找满足指定条件的结果中的第一行
    $post=Post::model()->find($condition,$params);
    // 查找具有指定主键值的那一行
    $post=Post::model()->findByPk($postID,$condition,$params);
    // 查找具有指定属性值的行
    $post=Post::model()->findByAttributes($attributes,$condition,$params);
    // 通过指定的 SQL 语句查找结果中的第一行
    $post=Post::model()->findBySql($sql,$params);
    $criteria=new CDbCriteria;
    $criteria->select='title';  // 只选择 'title' 列
    $criteria->condition='postID=:postID';
    $criteria->params=array(':postID'=>10);
    $post=Post::model()->find($criteria); // $params 不需要了
    $post=Post::model()->find(array(
        'select'=>'title',
        'condition'=>'postID=:postID',
        'params'=>array(':postID'=>10),
    ));

    信息: 当一个查询条件是关于按指定的值匹配几个列时,我们可以使用 findByAttributes()。我们使$attributes 参数是一个以列名做索引的值的数组。在一些框架中,此任务可以通过调用类似findByNameAndTitle 的方法实现。虽然此方法看起来很诱人, 但它常常引起混淆,冲突和比如列名大小写敏感的问题。

    当有多行数据匹配指定的查询条件时,我们可以通过下面的 findAll 方法将他们全部带回。 每个都有其各自的 find 方法,就像我们已经讲过的那样。

    // 查找满足指定条件的所有行
    $posts=Post::model()->findAll($condition,$params);
    // 查找带有指定主键的所有行
    $posts=Post::model()->findAllByPk($postIDs,$condition,$params);
    // 查找带有指定属性值的所有行
    $posts=Post::model()->findAllByAttributes($attributes,$condition,$params);
    // 通过指定的SQL语句查找所有行
    $posts=Post::model()->findAllBySql($sql,$params);
    
    
    如果没有任何东西符合查询条件,findAll 将返回一个空数组。这跟 find 不同,find 会在没有找到什么东西时返回 null。
    
    除了上面讲述的 find 和 findAll 方法,为了方便,(Yii)还提供了如下方法:
    // 获取满足指定条件的行数
    $n=Post::model()->count($condition,$params);
    // 通过指定的 SQL 获取结果行数
    $n=Post::model()->countBySql($sql,$params);
    // 检查是否至少有一行复合指定的条件
    $exists=Post::model()->exists($condition,$params);

     

  • 相关阅读:
    day13-web前端之JS与JQuery
    day16-python项目Django框架之基础
    day12-HTML、CSS与blog页面讲解
    day11-MySQL数据操作索引备份触发器
    day10-python并发编程之多线程协程及MySQL
    day9-python并发编程之多进程多线程
    day8-异常处理与网络编程
    面向对象实战扩展(课外阅读)
    day7-面向对象之继承组合多态封装等
    python学习大纲
  • 原文地址:https://www.cnblogs.com/sanpoye/p/3700965.html
Copyright © 2011-2022 走看看