OK, I know the title of this post is a bit long, but it does put it clearly what I had been trying to achieve using CakePHP.
Let’s say you have a Product model with unique index Field1Field2 made up of 2 fields (field1 and field2) and each time you insert a new record into Product, you want to make sure that only the unique combination of field1 and field2 is saved. How would you do that?
The short answer is to refer to the following code:
if ($this->Product->isUnique(array('Product.field1'=>$field1, 'Product.field2'=>$field2), false)) { $this->Product->save($this->data); }
The key here is the second parameter of isUnique, which is set to false in this case. It makes sure that the model will only save our data only if the combination of field1 AND field2 is unique.
This is the closest that I can get to doing a MySQL equivalent of mysql insert ignore in CakePHP for tables with unique index
Hope that helps. Suggestions welcomed. And Happy Baking!
Leave a Reply