• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I use findAll with select in model?

#1
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
Reply

#2
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.
Reply

#3
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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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