Skip to content

Commit 3d7a62b

Browse files
committed
Fix _findByCondition() scope
1 parent 4239481 commit 3d7a62b

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ OUTLINE
3838
- [Timestamps](#timestamps)
3939
- [Database Connection](#database-connection)
4040
- [Basic Usage](#basic-usage)
41-
- [Methods](#method)
41+
- [Methods](#methods)
4242
- [find()](#find)
4343
- [Query Builder Implementation](#query-builder-implementation)
4444
- [reset()](#reset)
@@ -56,7 +56,7 @@ OUTLINE
5656
- [Updates](#update)
5757
- [Deletes](#deletes)
5858
- [Accessing Data](#accessing-data)
59-
- [Methods](#method-1)
59+
- [Methods](#methods-1)
6060
- [findone()](#findone)
6161
- [findAll()](#findall)
6262
- [save()](#save)
@@ -105,7 +105,7 @@ The Model would defined database coonnections and table itself.
105105
```php
106106
$records = $this->Posts_model->find()
107107
->where('is_public', '1')
108-
->limit(0,25)
108+
->limit(25)
109109
->order_by('id')
110110
->get()
111111
->result_array();
@@ -340,7 +340,7 @@ Create an existent CI Query Builder instance with Model features for query purpo
340340
```php
341341
$records = $this->Model->find()
342342
->where('is_public', '1')
343-
->limit(0,25)
343+
->limit(25)
344344
->order_by('id')
345345
->get()
346346
->result_array();

src/Model.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Base Model
77
*
88
* @author Nick Tsai <myintaer@gmail.com>
9-
* @version 2.9.0
9+
* @version 2.9.1
1010
* @see https://github.com/yidas/codeigniter-model
1111
*/
1212
class Model extends \CI_Model implements \ArrayAccess
@@ -992,10 +992,12 @@ protected function _findByCondition($condition=NULL)
992992
// Reset Query if condition existed
993993
if ($condition) {
994994
$this->_dbr->reset_query();
995+
$query = $this->find();
996+
} else {
997+
// Support for previous find(), no need to find() again
998+
$query = $this->_dbr;
995999
}
9961000

997-
$query = $this->find();
998-
9991001
// Check condition type
10001002
if (is_array($condition)) {
10011003

@@ -1018,6 +1020,12 @@ protected function _findByCondition($condition=NULL)
10181020
/* Single Primary Key */
10191021
$query->where($this->_field($this->primaryKey), $condition);
10201022
}
1023+
else {
1024+
// No condition situation needs to enable where protection
1025+
$sql = $this->_dbr->get_compiled_select('', false); // No reset query
1026+
if (stripos($sql, 'where')===false)
1027+
throw new Exception("You could not write Model without any condition! Use ->where('1') at least.", 400);
1028+
}
10211029

10221030
return $query;
10231031
}

0 commit comments

Comments
 (0)