Welcome Guest, Not a member yet? Register   Sign In
Using where()->find() doesn't work as per my request!
#1

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

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()); 
Reply
#3

(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?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB