We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Problem with UNIQUE Keys, \Phalcon\Mcv\Model\save() and \Phalcon\Mcv\Model\update()

Hi, I'm Carlos, and I write you guys because in both the last and the new Phalcon Version (phalcon_x86_VC11_php5.5.0_1.3.2) there is a small error that I would like to ask you about.

I have a table with the following columns: MODULE, ACTION and CONTROLLER. This table is not allowd to have two identical records , so you would expect that all three elements are part of the primary key. This would be ideal, but Phalcon does not support the modification of an item which is either primary key or part of it (at least not with the Class Phalcon_Mvc_Model which we use) so I came up with the idea of creating an index for each element of the table. Then, to solve the problem that there can not be two identical records, I thought about to create a UNIQUE key entity that contains the three elements, so that the three key elements have the function of Primary Key even though they are not actually. This idea worked and Phalcon allows modify records that are UNIQUE. The problem arises when you try to add or modify a record (with save or update from Phalcon_Mvc_Model Class) resulting in two identical records and of course the internal table structure does not allow it because of the fact that the key is UNIQUE. In that case the save or update method crashs somewhere and the program does not run anymore. I've written down the query to get the table that I've spoken about. Hopefully I've explained everything right but tell me and I give you details if they are needed. Thank you in advance.

CREATE TABLE md_rights ( RIGHTS_ID INT(11) NOT NULL AUTO_INCREMENT, MODULE VARCHAR(84) NOT NULL, CONTROLLER VARCHAR(84) NOT NULL, PRIMARY KEY (RIGHTS_ID), UNIQUE INDEX MODULE (MODULE, CONTROLLER, ACTION) )

greeting