I am using cakephp 2.6. I have payment_method fields in mysql table which store the values like 54,52,59,57 .

Now my question is that how can i check if new posted payment method id is available in the payment_method value.

Here $data['PaymentMaster']['payment_method'] = 52

$paymentDetail = $this->PaymentMaster->find('first',array('conditions'=>array('NOT' => array('payment_method'=>$data['PaymentMaster']['payment_method']))));

I want to check if value not in database then add in to database other wise fire an error that Error: The rule is contradicting with the other rule, please check the rule.

Any help would be appreciated. Thanks in advance

1 Answer 1


by Unique constraint on your payment_method field, you can make sure that payment_method is unique. If the peyment_method is already exists, it will throw a SQL Error.

Duplicate entry 'x' for key 'peyment_method'

The SQL for that would be:

ALTER TABLE YourTableName ADD UNIQUE (payment_method)

But if you mean you need to make sure this payment_method exists and the payment_method_id is in another table, you need foreign key.

ALTER TABLE YourTable ADD FOREIGN KEY `methodidCheck`(payment_method)
REFERENCES payment_method_table(payment_method_id);

Hope I got your question right

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.