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.

Seems like _exists() method of Model class does not work for me

What are the unique columns, the function _exists in Model class is verifying?



7.4k

I can create and insert multiple events with the same data over and over again using create() method of the Model class :(



7.4k

How can i add columns by which it will verify, if such record exists or not?



7.1k
Accepted
answer

It checks if model exists by its primary key. So if you use Auto Increment in MySQL or Sequence in PostgreSQL as PK and do not set it, model will always create new row in DB.

If you want to check if some model column is unique, use Uniqueness validator.



7.4k
edited Apr '16

@davihu Thanx, it works, but still it does not work like i need. :) Here is my code:

    $this->validate(
     new Uniqueness(
                                    array(
                                            "field"   => "room_number",
                                            "field"   => "start_date",
                                            "field"   => "event_time",  
                                    "message" => "This appt is already scheduled. Please choose another Room, Date or Time"
                                    )
                    )
                          );

     if ($this->validationHasFailed() == true) 
     {
                return false;
          }

But i want it to work only if all 3 fields are indentical to the previous records. Can you advise? Thnx



7.4k

Already found the solution in the forum. Thanx