예제 #1
0
파일: model.php 프로젝트: svlt/back
 /**
  * Load by ID directly if a number is passed
  * @param  string|array  $filter
  * @param  array         $options
  * @param  integer       $ttl
  * @return mixed
  */
 function load($filter = NULL, array $options = NULL, $ttl = 0)
 {
     if (is_numeric($filter)) {
         return parent::load(array("id = ?", $filter), $options, $ttl);
     } else {
         return parent::load($filter, $options, $ttl);
     }
 }
예제 #2
0
 /**
  * Load by ID directly if a string is passed
  * @param  int|array  $filter
  * @param  array         $options
  * @param  integer       $ttl
  * @return mixed
  */
 function load($filter = NULL, array $options = NULL, $ttl = 0)
 {
     if (is_numeric($filter)) {
         return parent::load(array("id = ?", $filter), $options, $ttl);
     } elseif (is_array($filter)) {
         return parent::load($filter, $options, $ttl);
     }
     throw new Exception("\$filter must be either int or array.");
 }
예제 #3
0
파일: SqlMapper.php 프로젝트: swcug/bzfshop
 /**
  * 只返回一条记录
  *
  * @return 符合查询条件的第一条记录
  *
  * @param array $filter  查询条件,比如 array('name=? and age >?', 'xxx',18)
  * @param array $options 选项条件,比如 array('group'=>NULL,'order'=>NULL,'limit'=>0,    'offset'=>0)
  * @param int   $ttl     缓存时间
  * */
 function loadOne($filter = null, array $options = null, $ttl = 0)
 {
     if (!isset($options)) {
         $options = array();
     }
     /** 按照主键排序,确保每次的查询都是稳定的
      *
      *  一些数据库,比如 PostgreSql ,记录每更新一次,这条记录就会被移动到表的尾部,
      *  如果你 select * from table where ... limit 1
      *  比如你有 10 条记录,即使你的查询条件是一样的,但是每次返回的结果可能是不一样的,取决于哪条记录在表的前面
      *
      *  所以我们这里加上按照主键排序,确保每次查询结果都是一样的
      *
      * */
     if (!isset($options['order'])) {
         $orderBy = '';
         foreach ($this->fields as $key => $field) {
             if ($field['pkey']) {
                 $orderBy .= ($orderBy ? ' , ' : '') . $key . ' asc ';
             }
         }
         if ($orderBy) {
             $options += array('order' => $orderBy);
         }
     }
     $options += array('limit' => 1);
     return parent::load($filter, $options, $ttl);
 }