Welcome Guest, Not a member yet? Register   Sign In
No like in getResultArray
#1

Hello, I previously used query builder $db->query("MY_QUERY");
Now decided to try the patterns method $builder->like(), in which I get something wrong... The error concerns the output function, but why?

Call to undefined method CodeIgniter\Database\MySQLi\Builder::getResultArray()

Controller
PHP Code:
   $model = new \Modules\Par\Models\ParModel();
 
   $par $this->request->getPost('test');
 
   $data = [
 
     'query'  => $model->searchPart($par),
      ...
 
   ];
 
   echo view('Modules\Par\Views\viewTest'$data); 

Model
PHP Code:
 public function searchPart($data) {
 
   return $this->db->table('par')->like('title'$data);
 
 

View
PHP Code:
foreach ($query->getResultArray() as $row)
{
 
       echo $row['title'];
 
       echo $row['body'];


Thanks!
Reply
#2

(08-08-2018, 02:00 PM)Nome Wrote: Hello, I previously used query builder $db->query("MY_QUERY");
Now decided to try the patterns method $builder->like(), in which I get something wrong... The error concerns the output function, but why?

Call to undefined method CodeIgniter\Database\MySQLi\Builder::getResultArray()

Controller
PHP Code:
   $model = new \Modules\Par\Models\ParModel();
 
   $par $this->request->getPost('test');
 
   $data = [
 
     'query'  => $model->searchPart($par),
 
     ...
 
   ];
 
   echo view('Modules\Par\Views\viewTest'$data); 

Model
PHP Code:
 public function searchPart($data) {
 
   return $this->db->table('par')->like('title'$data);
 
 

View
PHP Code:
foreach ($query->getResultArray() as $row)
{
 
       echo $row['title'];
 
       echo $row['body'];


Thanks!

I would first start with the Class and check the return values to ensure that the script returns exactly what you expect then continue to the next variable:
PHP Code:
$model = new \Modules\Par\Models\ParModel();

echo 
'<pre>';
print_r($model);
echo 
'</pre>';
die; 
Reply
#3

(08-08-2018, 11:18 PM)John_Betong Wrote: I would first start with the Class and check the return values to ensure that the script returns exactly what you expect then continue to the next variable:
PHP Code:
$model = new \Modules\Par\Models\ParModel();

echo 
'<pre>';
print_r($model);
echo 
'</pre>';
die; 

This only added questions). Why is my request written in the manual triggered and this one is not? After all, the requested data exists, there is a table `par` and a column `title`... Should an empty response cause an error?

Code:
  [lastQuery:protected] =>
           [connID] =>
           [resultID] =>
           [protectIdentifiers] => 1
           [reservedIdentifiers:protected] => Array
               (
                   [0] => *
               )

           [likeEscapeStr] =>  ESCAPE '%s'
           [likeEscapeChar] => !
           [dataCache] => Array
               (
               )
Reply
#4

Where what was the print_r(...); statement inserted?

It looks as though the connection to the database failed.
Reply
#5

(08-09-2018, 02:09 AM)John_Betong Wrote: Where what was the print_r(...); statement inserted?

It looks as though the connection to the database failed.

Oh sure...

Code:
Modules\Parts\Models\PartsModel Object
(
   [table:protected] => par
   [primaryKey:protected] => pid
   [useTimestamps:protected] => 1
   [validationRules:protected] => Array
       (
       )

   [validationMessages:protected] => Array
       (
           [number] => Array
               (
                   [is_natural] => Ups i did it again.
               )

           [0] =>
       )

   [pager] =>
   [DBGroup:protected] =>
   [returnType:protected] => array
   [useSoftDeletes:protected] =>
   [allowedFields:protected] => Array
       (
       )

   [dateFormat:protected] => datetime
   [createdField:protected] => created_at
   [updatedField:protected] => updated_at
   [tempUseSoftDeletes:protected] =>
   [deletedField:protected] => deleted
   [tempReturnType:protected] => array
   [protectFields:protected] => 1
   [db:protected] => CodeIgniter\Database\MySQLi\Connection Object
       (
           [DBDriver] => MySQLi
           [deleteHack] => 1
           [escapeChar] => `
           [mysqli] =>
           [DSN:protected] =>
           [port:protected] => 3306
           [hostname:protected] => localhost
           [username:protected] => root
           [password:protected] =>
           [database:protected] => host_ls
           [subdriver:protected] =>
           [DBPrefix:protected] => ls_
           [pConnect:protected] =>
           [DBDebug:protected] => 1
           [cacheOn:protected] =>
           [cacheDir:protected] =>
           [charset:protected] => utf8
           [DBCollat:protected] => utf8_general_ci
           [swapPre:protected] =>
           [encrypt:protected] =>
           [compress:protected] =>
           [strictOn:protected] =>
           [failover:protected] => Array
               (
               )

           [lastQuery:protected] =>
           [connID] =>
           [resultID] =>
           [protectIdentifiers] => 1
           [reservedIdentifiers:protected] => Array
               (
                   [0] => *
               )

           [likeEscapeStr] =>  ESCAPE '%s'
           [likeEscapeChar] => !
           [dataCache] => Array
               (
               )

           [connectTime:protected] =>
           [connectDuration:protected] =>
           [pretend:protected] =>
           [transEnabled] => 1
           [transStrict] => 1
           [transDepth:protected] => 0
           [transStatus:protected] => 1
           [transFailure:protected] =>
           [aliasedTables:protected] => Array
               (
               )

       )
Reply
#6

(This post was last modified: 08-09-2018, 09:40 AM by John_Betong. Edit Reason: Mentioned missing images )

Try this because it works for me:
PHP Code:
 $db = \Config\Database::connect();

 
 $tmp $data['title'];
 
 
  $sql 
= <<< ____TMP
    SELECT
      `xrl`, `title`, `memo`
    FROM 
      `jokes`
    WHERE
      `xrl` 
    LIKE
      "%
$tmp%"      
____TMP;

 
 $query   $db->query$sql );
 
 $data['items'] = $query->getResultArray(); 
Online demo:

// list of jokes
  https://johns-jokes.cf/home?app=4

// select any joke title from above
// images have yet to be displayed because URLs in database and link to another domain
Reply
#7

(08-09-2018, 08:40 AM)John_Betong Wrote: Try this because it works for me:
PHP Code:
 $db = \Config\Database::connect();

 
 $tmp $data['title'];
 
 
  $sql 
= <<< ____TMP
    SELECT
      `xrl`, `title`, `memo`
    FROM 
      `jokes`
    WHERE
      `xrl` 
    LIKE
      "%
$tmp%"      
____TMP;

 
 $query   $db->query$sql );
 
 $data['items'] = $query->getResultArray(); 

Thank you!

Search on the request works, I wrote about this in the beginning. I'm curious how to do this through $builder->like()...
Reply
#8

@Nome

>>> Search on the request works, I wrote about this in the beginning.
>>> I'm curious how to do this through $builder->like()...

I tried without success and think maybe $builder->like() has not been implemented yet.

Perhaps @kilishan or another user could suggest a solution.
Reply
#9

(This post was last modified: 08-10-2018, 12:54 AM by puschie. Edit Reason: added code )

the like function return a $this, and the builder class does not have the getResultArray functions.
you need to execute the query first ( you have no action applied, so ci use select as default. to execute your builder ->get() ) https://bcit-ci.github.io/CodeIgniter4/d...cting-data

public function searchPart($data) {
return $this->db->table('par')->like('title', $data)->get();
}
Reply




Theme © iAndrew 2016 - Forum software by © MyBB