implements Phalcon\Mvc\EntityInterface, Phalcon\Mvc\CollectionInterface, Phalcon\Di\InjectionAwareInterface, Serializable
This component implements a high level abstraction for NoSQL databases which works with documents
Phalcon\Mvc\Collection constructor
Sets a value for the _id property, creates a MongoId object if needed
Returns the value of the _id property
Sets the dependency injection container
Returns the dependency injection container
Sets a custom events manager
Returns the custom events manager
Returns the models manager related to the entity instance
Returns an array with reserved properties that cannot be part of the insert/update
Sets if a model must use implicit objects ids
Sets collection name which model should be mapped
Returns collection name mapped in the model
Sets the DependencyInjection connection service name
Returns DependencyInjection connection service
Retrieves a database connection
Reads an attribute value by its name
echo $robot->readAttribute("name");
Writes an attribute value by its name
$robot->writeAttribute("name", "Rosey");
Returns a cloned collection
Returns a collection resultset
Perform a count over a resultset
Executes internal hooks before save a document
Executes internal events after save a document
Executes validators on every validation call
use Phalcon\Mvc\Model\Validator\ExclusionIn as ExclusionIn; class Subscriptors extends \Phalcon\Mvc\Collection { public function validation() { // Old, deprecated syntax, use new one below $this->validate( new ExclusionIn( [ "field" => "status", "domain" => ["A", "I"], ] ) ); if ($this->validationHasFailed() == true) { return false; } } }
use Phalcon\Validation\Validator\ExclusionIn as ExclusionIn; use Phalcon\Validation; class Subscriptors extends \Phalcon\Mvc\Collection { public function validation() { $validator = new Validation(); $validator->add("status", new ExclusionIn( [ "domain" => ["A", "I"] ] ) ); return $this->validate($validator); } }
Check whether validation process has generated any messages
use Phalcon\Mvc\Model\Validator\ExclusionIn as ExclusionIn; class Subscriptors extends \Phalcon\Mvc\Collection { public function validation() { $this->validate( new ExclusionIn( [ "field" => "status", "domain" => ["A", "I"], ] ) ); if ($this->validationHasFailed() == true) { return false; } } }
Fires an internal event
Fires an internal event that cancels the operation
Cancel the current operation
Checks if the document exists in the collection
Returns all the validation messages
$robot = new Robots(); $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 ($messages as $message) { echo $message; } } else { echo "Great, a new robot was saved successfully!"; }
Appends a customized message on the validation process
use \Phalcon\Mvc\Model\Message as Message; class Robots extends \Phalcon\Mvc\Model { public function beforeSave() { if ($this->name === "Peter") { $message = new Message( "Sorry, but a robot cannot be named Peter" ); $this->appendMessage(message); } } }
Shared Code for CU Operations Prepares Collection
Creates/Updates a collection based on the values in the attributes
Creates a collection based on the values in the attributes
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
$robot = new Robot(); $robot->name = "MyRobot"; $robot->type = "Droid"; // Create only if robot with same name and type does not exist $robot->createIfNotExist( [ "name", "type", ] );
Creates/Updates a collection based on the values in the attributes
Find a document by its id (_id)
// Find user by using \MongoId object $user = Users::findById( new \MongoId("545eb081631d16153a293a66") ); // Find user by using id as sting $user = Users::findById("45cbc4a0e4123f6920000002"); // Validate input if ($user = Users::findById($_POST["id"])) { // ... }
Allows to query the first record that match the specified conditions
// 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";
Allows to query a set of records that match the specified conditions
// 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 ($robots as $robot) { echo $robot->name, "\n"; } // Get first 100 virtual robots ordered by name $robots = Robots::find( [ [ "type" => "virtual", ], "order" => [ "name" => 1, ], "limit" => 100, ] ); foreach ($robots as $robot) { echo $robot->name, "\n"; }
Perform a count over a collection
echo "There are ", Robots::count(), " robots";
Perform an aggregation using the Mongo aggregation framework
Allows to perform a summatory group for a column in the collection
Deletes a model instance. Returning true on success or false otherwise.
$robot = Robots::findFirst(); $robot->delete(); $robots = Robots::find(); foreach ($robots as $robot) { $robot->delete(); }
Sets the dirty state of the object using one of the DIRTY_STATE_* constants
Returns one of the DIRTY_STATE_* constants telling if the document exists in the collection or not
Sets up a behavior in a collection
Skips the current operation forcing a success state
Returns the instance as an array representation
print_r( $robot->toArray() );
Serializes the object ignoring connections or protected properties
Unserializes the object from a serialized string
© 2011–2017 Phalcon Framework Team
Licensed under the Creative Commons Attribution License 3.0.
https://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Collection.html