implements Phalcon\Mvc\CollectionInterface, Phalcon\DI\InjectionAwareInterface, Serializable
This component implements a high level abstraction for NoSQL databases which works with documents
Phalcon\Mvc\Model 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() { $this->validate(new ExclusionIn(array( 'field' => 'status', 'domain' => array('A', 'I') ))); if ($this->validationHasFailed() == true) { return false; } } }
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(array( 'field' => 'status', 'domain' => array('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 "; foreach ($robot->getMessages() 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); } } }
Creates/Updates a collection based on the values in the attributes
Find a document by its id (_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(array( array("type" => "mechanical") )); echo "The first mechanical robot name is ", $robot->name, "\n"; //Get first virtual robot ordered by name $robot = Robots::findFirst(array( array("type" => "mechanical"), "order" => array("name" => 1) )); echo "The first virtual robot name is ", $robot->name, "\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(array( array("type" => "mechanical") )); echo "There are ", count($robots), "\n"; //Get and print virtual robots ordered by name $robots = Robots::findFirst(array( array("type" => "virtual"), "order" => array("name" => 1) )); foreach ($robots as $robot) { echo $robot->name, "\n"; } //Get first 100 virtual robots ordered by name $robots = Robots::find(array( array("type" => "virtual"), "order" => array("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(); foreach (Robots::find() as $robot) { $robot->delete(); }
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
Runs JavaScript code on the database server.
$ret = Robots::execute("function() { return 'Hello, world!';}"); echo $ret['retval'], "\n";
© 2011–2016 Phalcon Framework Team
Licensed under the Creative Commons Attribution License 3.0.
https://docs.phalconphp.com/en/2.0.0/api/Phalcon_Mvc_Collection.html