• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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, 03: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; 
CI4 Develop Site - progressing nicely - Thank you Smile
https://johns-jokes.cf/pictures

Reply

#3
(08-09-2018, 12:18 AM)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.
CI4 Develop Site - progressing nicely - Thank you Smile
https://johns-jokes.cf/pictures

Reply

#5
(08-09-2018, 03: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
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
CI4 Develop Site - progressing nicely - Thank you Smile
https://johns-jokes.cf/pictures

Reply

#7
(08-09-2018, 09: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.
CI4 Develop Site - progressing nicely - Thank you Smile
https://johns-jokes.cf/pictures

Reply

#9
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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.