<?phpusePhalcon\Mvc\Model\Validator\ExclusionInasExclusionIn;classSubscriptorsextends\Phalcon\Mvc\Collection{publicfunctionvalidation(){// Old, deprecated syntax, use new one below$this->validate(newExclusionIn(["field"=>"status","domain"=>["A","I"],]));if($this->validationHasFailed()==true){returnfalse;}}}

<?php$robot=newRobots();$robot->type="mechanical";$robot->name="Astro Boy";$robot->year=1952;if($robot->save()===false){echo"Umh, We can't store robots right now ";$messages=$robot->getMessages();foreach($messagesas$message){echo$message;}}else{echo"Great, a new robot was saved successfully!";}

<?phpuse\Phalcon\Mvc\Model\MessageasMessage;classRobotsextends\Phalcon\Mvc\Model{publicfunctionbeforeSave(){if($this->name==="Peter"){$message=newMessage("Sorry, but a robot cannot be named Peter");$this->appendMessage(message);}}}

protected prepareCU ()

Shared Code for CU Operations
Prepares Collection

public save ()

Creates/Updates a collection based on the values in the attributes

public create ()

Creates a collection based on the values in the attributes

public createIfNotExist (array $criteria)

Creates a document based on the values in the attributes, if not found by criteria
Preferred way to avoid duplication is to create index on attribute

<?php$robot=newRobot();$robot->name="MyRobot";$robot->type="Droid";// Create only if robot with same name and type does not exist$robot->createIfNotExist(["name","type",]);

<?php// What's the first robot in the robots table?$robot=Robots::findFirst();echo"The robot name is ",$robot->name,"\n";// What's the first mechanical robot in robots table?$robot=Robots::findFirst([["type"=>"mechanical",]]);echo"The first mechanical robot name is ",$robot->name,"\n";// Get first virtual robot ordered by name$robot=Robots::findFirst([["type"=>"mechanical",],"order"=>["name"=>1,],]);echo"The first virtual robot name is ",$robot->name,"\n";// Get first robot by id (_id)$robot=Robots::findFirst([["_id"=>new\MongoId("45cbc4a0e4123f6920000002"),]]);echo"The robot id is ",$robot->_id,"\n";

public static find ([array $parameters])

Allows to query a set of records that match the specified conditions

<?php// How many robots are there?$robots=Robots::find();echo"There are ",count($robots),"\n";// How many mechanical robots are there?$robots=Robots::find([["type"=>"mechanical",]]);echo"There are ",count(robots),"\n";// Get and print virtual robots ordered by name$robots=Robots::findFirst([["type"=>"virtual"],"order"=>["name"=>1,]]);foreach($robotsas$robot){echo$robot->name,"\n";}// Get first 100 virtual robots ordered by name$robots=Robots::find([["type"=>"virtual",],"order"=>["name"=>1,],"limit"=>100,]);foreach($robotsas$robot){echo$robot->name,"\n";}