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

How to create a record and avoid duplication?

It is data table that store the users' rating data

CREATE TABLE `rates` (
  `users_id` int(10) unsigned NOT NULL,
  `flag` varchar(16) NOT NULL DEFAULT '',
  `some_id` int(10) unsigned NOT NULL,
  PRIMARY KEY `uid_flag_sid` (`users_id`, `flag`, `some_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

// example data
+----------+-------+---------+
| users_id | flag  | some_id |
+----------+-------+---------+
|    1     | topic |    1    |
|    1     | reply |    1    |
+----------+-------+---------+

The sample data shows that a user with id=1 has given scores to the topic with id=1 and the reply with id=1

Then, how to insert a new record into this table and avoid the duplication? just $rate->save()?



31.3k

Thanks!

this example shows one field, how about two fields?

public function validation()
  {
      $this->validate(new Uniqueness(array(
          "field"   => "email",
          "message" => "Value of field 'email' is already present in another record"
      )));
      if ($this->validationHasFailed() == true) {
          return false;
      }
  }

Hey,

check this https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Model_Validator_Uniqueness.html



31.3k

The answer is here, made by Nikolay Mihaylov :https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Model_Validator_Uniqueness.html sorry for mistake