Repository: codeigniter4/CodeIgniter4 (PHP)

Issue:
Bug: RawSql() triggers an error when used in combination with like() and countAllResults()
### PHP Version

8.1

### CodeIgniter4 Version

4.3.1

### CodeIgniter4 Installation Method

Composer (using `codeigniter4/appstarter`)

### Which operating systems have you tested for this bug?

Windows

### Which server did you use?

apache

### Database

_No response_

### What happened?

```php
$builder = $this->db->table('users');
$builder->select('id');
$builder->like(new RawSql("CONCAT(username, email)"), 'sky');
$count = $builder->countAllResults(FALSE);
$tableList = $builder->get()->getResultArray();
```

In Query Builder Class, if you use new RawSql() in $builder ->like(), and then use $builder ->countAllResults (FALSE); An error will be triggered when the total number of query results is obtained and then the query results are obtained.

Error prompt:
```
code: 500
file:"G:\\Code\\website\\ci4_cms\\vendor\\codeigniter4\\framework\\system\\Database\\BaseBuilder.php"
line: 3071
message: "Cannot use object of type CodeIgniter\\Database\\RawSql as array"
```


After debugging, it is found that if the countAllResults parameter is not set to FALSE, no error will be reported, but the subsequent get() cannot obtain the correct query results;



In addition, the third parameter of get() can also reset the query object. After setting, $builder ->countAllResults(); The same error is reported!

Problem conclusion: New RawSql() cannot be used in the link() before resetting values for SELECT; Or if you want to use new RawSql(), you cannot reset values for SELECTs??

### Steps to Reproduce

```php
$builder = $this->db->table('users');
$builder->select('id');
$builder->like(new RawSql("CONCAT(username, email)"), 'sky');
$count = $builder->countAllResults(FALSE);
$tableList = $builder->get(25, 0)->getResultArray();
```
or
```php
$builder = $this->db->table('users');
$builder->select('id');
$builder->like(new RawSql("CONCAT(username, email)"), 'sky');
$tableList = $builder->get(25, 0, FALSE)->getResultArray();
$count = $builder->countAllResults();
```


### Expected Output

Get the total number of query results and query result array normally

### Anything else?

_No response_


Discussion:
What's more, I don't understand what the role of new RawSql() is. Even though I have roughly looked at the source file of RawSql, I still don't understand whether it is for security protection or to generate objects for the next reuse to improve efficiency??
Thank you for reporting.

`RawSql` expresses raw SQL strings. That's all.
Normally, values passed to QueryBuilder are escaped, protected, DBPrefix-added, or otherwise manipulated.
RawSql ensures that no such processing occurs.

It does not provide any security protection. You must ensure it is safe by yourself.

I sent a PR to fix this: #7277




Patch:
diff --git a/system/Database/BaseBuilder.php b/system/Database/BaseBuilder.php
index 653f43013f15..31234f9edf1a 100644
--- a/system/Database/BaseBuilder.php
+++ b/system/Database/BaseBuilder.php
@@ -3082,6 +3082,10 @@ protected function compileWhereHaving(string $qbKey): string
                     continue;
                 }
 
+                if ($qbkey instanceof RawSql) {
+                    continue;
+                }
+
                 if ($qbkey['condition'] instanceof RawSql) {
                     $qbkey = $qbkey['condition'];
 


Test patch:
diff --git a/tests/system/Database/Live/LikeTest.php b/tests/system/Database/Live/LikeTest.php
index f192d4a0f224..fe616105a466 100644
--- a/tests/system/Database/Live/LikeTest.php
+++ b/tests/system/Database/Live/LikeTest.php
@@ -11,6 +11,7 @@
 
 namespace CodeIgniter\Database\Live;
 
+use CodeIgniter\Database\RawSql;
 use CodeIgniter\Test\CIUnitTestCase;
 use CodeIgniter\Test\DatabaseTestTrait;
 use Tests\Support\Database\Seeds\CITestSeeder;
@@ -121,4 +122,46 @@ public function testLikeSpacesOrTabs()
         $this->assertCount(1, $spaces);
         $this->assertCount(1, $tabs);
     }
+
+    /**
+     * @see https://github.com/codeigniter4/CodeIgniter4/issues/7268
+     */
+    public function testLikeRawSqlAndCountAllResultsAndGet()
+    {
+        $builder = $this->db->table('job');
+
+        if ($this->db->DBDriver === 'OCI8') {
+            $key = new RawSql('"name"');
+        } else {
+            $key = new RawSql('name');
+        }
+
+        $builder->like($key, 'Developer');
+        $count   = $builder->countAllResults(false);
+        $results = $builder->get()->getResult();
+
+        $this->assertSame(1, $count);
+        $this->assertSame('Developer', $results[0]->name);
+    }
+
+    /**
+     * @see https://github.com/codeigniter4/CodeIgniter4/issues/7268
+     */
+    public function testLikeRawSqlAndGetAndCountAllResults()
+    {
+        $builder = $this->db->table('job');
+
+        if ($this->db->DBDriver === 'OCI8') {
+            $key = new RawSql('"name"');
+        } else {
+            $key = new RawSql('name');
+        }
+
+        $builder->like($key, 'Developer');
+        $results = $builder->get(null, 0, false)->getResult();
+        $count   = $builder->countAllResults();
+
+        $this->assertSame(1, $count);
+        $this->assertSame('Developer', $results[0]->name);
+    }
 }