Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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()?



8.1k

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.phalconphp.com/en/latest/api/Phalcon%5Mvc%5Model%5Validator%5Uniqueness.html