Check first if id exist, if does not exist, only save, if exist, do not save

hello World,

i like to save data but before need to check if similar id exists, if it exists do not save, else save

            $result['order_item_ids'] = $order_product->order_item_id;
            $result['user_id'] = $mpCon['user_id'];
            $result['order_id'] = $order_product->order_id;
            foreach($invoices as $invoice){
                if ($invoice->order_id != $result['order_id']) {
                    $good = $this->Invoices->newEntity();
                    $save= $this->Invoices->patchEntity($good, $result);

but, it still save data into the table causing duplicates

which part i need to rectify in the if statement?



Can you explain a little bit more detailed what you try to achieve.

It seems you are inside your InvoicesController and try so save invoices to a order.
But how does your $invoices array get built? What is $result? What data is present inside your request and what should happen?

hi, Kevin,

i am trying to save Invoices (field) which is from (another) Orders Table by way of syncing into Invoices Table, then the Invoice can be viewed by PDF mode to be downloaded.

when clicked sync button, only the new invoices will be saved, if invoices order_id exists in Invoices Table, it will not save.

the Orders Table is arrays, and Invoices also arrays, i try to check if Invoices will save the order_id which is only new.

Maybe do it manually? Just ask order tale if there are your id and make decision?

$order = $this->Orders->find()->where(['your_id' => $id])->first();
if ($order) {//no save}
else {//save}

ps. use debug($result) and check what is inside and does it even can work in your approach.