CodeIgniter Forums
Using where()->find() doesn't work as per my request! - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: Using where()->find() doesn't work as per my request! (/showthread.php?tid=81234)



Using where()->find() doesn't work as per my request! - sialexsofficial - 02-08-2022

Code:
public function __construct()
    {
        parent::__construct();
        helper('my_helper');
        $this->parser = \Config\Services::parser();
        $this->ServicesModel = new \App\Models\ServicesModel();
        $this->ProviderModel = new \App\Models\ProviderModel();
    }

    public function order()
    {
        $GetId = 3;
        $GetServerId = 1;
        $GetOperator = "axis";

        if ($GetId !== null and $GetServerId !== null and $GetOperator !== null) {
            $CheckService = $this->ServicesModel->where('id', $GetId);
            if ($CheckService->get()->getNumRows() == 1) {
                dd($CheckService->find());
            } else {
                return json_encode(array('status' => false, 'msg' => 'Layanan sedang tidak tersedia silahkan ganti layanan.'));
            }
        } else {
            return json_encode(array('status' => false, 'msg' => 'Silahkan pilih operator terlebih dahulu.'));
        }
    }


above is the code snippet in my controller..I expect that generated from dd($CheckService->find()); only one, because I use the function where(),,,I want to get certain data using where,,But why does everything in the database come out?


RE: Using where()->find() doesn't work as per my request! - iRedds - 02-08-2022

The find() method without passing in an ID behaves just like the findAll() method;
Also after each query, all conditions are reset.

You can use a solution like this.
PHP Code:
$CheckService $this->ServicesModel->where('id'$GetId)->findAll();

if (
count($CheckService) === 1) {
    
dd($CheckService[0]);

// or, if the type of the result is not important (object by default)
$CheckService $this->ServicesModel->where('id'$GetId)->get();

if (
$CheckService->getNumRows() === 1) {
    
dd($CheckService->getFirstRow()); 



RE: Using where()->find() doesn't work as per my request! - sialexsofficial - 02-09-2022

(02-08-2022, 10:06 AM)iRedds Wrote: The find() method without passing in an ID behaves just like the findAll() method;
Also after each query, all conditions are reset.

You can use a solution like this.
PHP Code:
$CheckService $this->ServicesModel->where('id'$GetId)->findAll();

if (
count($CheckService) === 1) {
    dd($CheckService[0]);

// or, if the type of the result is not important (object by default)
$CheckService $this->ServicesModel->where('id'$GetId)->get();

if (
$CheckService->getNumRows() === 1) {
    dd($CheckService->getFirstRow()); 


thanks for answering my question, Is there a way to not reset every condition?