CodeIgniter Forums
How can I use findAll with select in model? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: How can I use findAll with select in model? (/thread-78980.html)



How can I use findAll with select in model? - asrahi - 04-04-2021

Hi!
I try to like this, but it's error of SQL syntax.



PHP Code:
$model model('MyModel');

//It's Okay!
$model->where('column1','value')->findAll();

//It's error
$model->select('column1, column2')->findAll(); 



Im trying to use builder, like this

PHP Code:
$builder $model->builnder();
$builder->select('column1, column2');
...
$model->findAll(); 



the error message is 
(SELECT COUNT(*) FROM TABLE WHERE IDX = ....' at line 1


Can I use select method with findAll in model?
if I can't, what I do another option?

Thank you


RE: How can I use findAll with select in model? - [email protected] - 04-06-2021

Code:
// Method inside your controller

    public function dbtest()
    {
        $model = model('MyModel');
        
        $builder = $model->builder();
        $builder->select('column1, column2');

        $result = $model->findAll();
        var_dump($result);        
    }

This is tested and produces a result set without error.

However, after getting your code to work you would be better off creating a method in your model and returning your results rather than manipulating the database logic from within your controller.


RE: How can I use findAll with select in model? - nurmyrat - 04-08-2021

find()
Returns a single row where the primary key matches the value passed in as the first parameter:
$user = $userModel->find($user_id);

The value is returned in the format specified in $returnType.
You can specify more than one row to return by passing an array of primaryKey values instead of just one:
$users = $userModel->find([1,2,3]);

If no parameters are passed in, will return all rows in that model’s table, effectively acting like findAll(), though less explicit.


Please read the docs.