prevent duplicate for Composite key columns - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: prevent duplicate for Composite key columns (/showthread.php?tid=81914) |
prevent duplicate for Composite key columns - Ahmed Haroon - 05-20-2022 working environment: XAMPP on Windows 10 have Unique Index - Composite: Code: ALTER TABLE `sales`.`brand_categories` when user will save a duplicate entry for the combination of both columns, database will throw error. but is there a builtin validation rule which is suitable for it like is_unique. if yes please guide How To. if no builtin validation rule then how i can write a custome validation rule and prevent duplicate in this case please help. in Model have validation rule (this is just sample what i can do -as per my basic knowledge- for uniqueness of a single column) Code: protected $validationRules = [ regards RE: prevent duplicate for Composite key columns - kenjis - 05-21-2022 `is_unique` supports only one column. Code: is_unique[table.field,ignore_field,ignore_value] Create your own rule. See https://codeigniter4.github.io/userguide/libraries/validation.html#creating-custom-rules RE: prevent duplicate for Composite key columns - Ahmed Haroon - 05-22-2022 (05-21-2022, 05:24 PM)kenjis Wrote: `is_unique` supports only one column. thanks for reply @kenjis ... but i can't figure it out How To ? how i can do this validation? how i can create a custom validation for this situation? please help. regards RE: prevent duplicate for Composite key columns - Ahmed Haroon - 05-23-2022 at last, i added a column brandcategories, in form i hide it (display:none) and assign value using javascript code as brand_id + category_id in Model added a validation "is_unique" for this new column. its working fine. its a way around when i can't have a solution without adding this extra column in table. regards. RE: prevent duplicate for Composite key columns - kenjis - 05-24-2022 If you want to create a custom rule, see the `is_unique` code. https://github.com/codeigniter4/CodeIgniter4/blob/daff2d77800430833ae3cedfba115448dcbfdc11/system/Validation/Rules.php#L126 RE: prevent duplicate for Composite key columns - Ahmed Haroon - 05-24-2022 (05-24-2022, 12:16 AM)kenjis Wrote: If you want to create a custom rule, see the `is_unique` code.thanks @kenjis will check it, hope i can understand it. regards |