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

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?



14.9k

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



14.9k

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



9.3k
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.



14.9k
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



14.9k

Already found the solution in the forum. Thanx