企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Abstract class Phalcon\\Mvc\\Collection # Abstract class **Phalcon\\Mvc\\Collection** *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 ### Constants *integer***OP\_NONE** *integer***OP\_CREATE** *integer***OP\_UPDATE** *integer***OP\_DELETE** ### Methods final public **\_\_construct** (\[*unknown* $dependencyInjector\], \[*unknown* $modelsManager\]) Phalcon\\Mvc\\Collection constructor public **setId** (*mixed* $id) Sets a value for the \_id property, creates a MongoId object if needed public *MongoId***getId** () Returns the value of the \_id property public **setDI** (*unknown* $dependencyInjector) Sets the dependency injection container public **getDI** () Returns the dependency injection container protected **setEventsManager** (*unknown* $eventsManager) Sets a custom events manager protected **getEventsManager** () Returns the custom events manager public **getCollectionManager** () Returns the models manager related to the entity instance public **getReservedAttributes** () Returns an array with reserved properties that cannot be part of the insert/update protected **useImplicitObjectIds** (*unknown* $useImplicitObjectIds) Sets if a model must use implicit objects ids protected **setSource** (*unknown* $source) Sets collection name which model should be mapped public **getSource** () Returns collection name mapped in the model public **setConnectionService** (*unknown* $connectionService) Sets the DependencyInjection connection service name public **getConnectionService** () Returns DependencyInjection connection service public *MongoDb***getConnection** () Retrieves a database connection public *mixed***readAttribute** (*string* $attribute) Reads an attribute value by its name ``` <pre class="calibre14">``` <?php echo $robot->readAttribute('name'); ``` ``` public **writeAttribute** (*string* $attribute, *mixed* $value) Writes an attribute value by its name ``` <pre class="calibre14">``` <?php $robot->writeAttribute('name', 'Rosey'); ``` ``` public static **cloneResult** (*unknown* $collection, *unknown* $document) Returns a cloned collection protected static *array*\*\*\_getResultset\*\* (*array* $params, <a class="calibre6 pcalibre1" href="">*Phalcon\\Mvc\\Collection*</a> $collection, *MongoDb* $connection, *boolean* $unique) Returns a collection resultset protected static *int*\*\*\_getGroupResultset\*\* (*array* $params, <a class="calibre6 pcalibre1" href="">*Phalcon\\Mvc\\Collection*</a> $collection, *MongoDb* $connection) Perform a count over a resultset final protected *boolean*\*\*\_preSave\*\* ([*Phalcon\\DiInterface*](#) $dependencyInjector, *boolean* $disableEvents, *boolean* $exists) Executes internal hooks before save a document final protected **\_postSave** (*unknown* $disableEvents, *unknown* $success, *unknown* $exists) Executes internal events after save a document protected **validate** (*unknown* $validator) Executes validators on every validation call ``` <pre class="calibre14">``` <?php 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; } } } ``` ``` public **validationHasFailed** () Check whether validation process has generated any messages ``` <pre class="calibre14">``` <?php 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; } } } ``` ``` public **fireEvent** (*unknown* $eventName) Fires an internal event public **fireEventCancel** (*unknown* $eventName) Fires an internal event that cancels the operation protected **\_cancelOperation** (*unknown* $disableEvents) Cancel the current operation protected *boolean*\*\*\_exists\*\* (*MongoCollection* $collection) Checks if the document exists in the collection public **getMessages** () Returns all the validation messages ``` <pre class="calibre14">``` <?php $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!"; } ``` ``` public **appendMessage** (*unknown* $message) Appends a customized message on the validation process ``` <pre class="calibre14">``` <?php 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); } } } ``` ``` public **save** () Creates/Updates a collection based on the values in the attributes public static <a class="calibre6 pcalibre1" href="">*Phalcon\\Mvc\\Collection*</a>**findById** (*string|MongoId* $id) Find a document by its id (\_id) public static **findFirst** (\[*unknown* $parameters\]) Allows to query the first record that match the specified conditions ``` <pre class="calibre14">``` <?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(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"; ``` ``` public static **find** (\[*unknown* $parameters\]) Allows to query a set of records that match the specified conditions ``` <pre class="calibre14">``` <?php //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"; } ``` ``` public static **count** (\[*unknown* $parameters\]) Perform a count over a collection ``` <pre class="calibre14">``` <?php echo 'There are ', Robots::count(), ' robots'; ``` ``` public static **aggregate** (\[*unknown* $parameters\]) Perform an aggregation using the Mongo aggregation framework public static **summatory** (*unknown* $field, \[*unknown* $conditions\], \[*unknown* $finalize\]) Allows to perform a summatory group for a column in the collection public **delete** () Deletes a model instance. Returning true on success or false otherwise. ``` <pre class="calibre14">``` <?php $robot = Robots::findFirst(); $robot->delete(); foreach (Robots::find() as $robot) { $robot->delete(); } ``` ``` protected **addBehavior** (*unknown* $behavior) Sets up a behavior in a collection public **skipOperation** (*unknown* $skip) Skips the current operation forcing a success state public **toArray** () Returns the instance as an array representation ``` <pre class="calibre14">``` <?php print_r($robot->toArray()); ``` ``` public **serialize** () Serializes the object ignoring connections or protected properties public **unserialize** (*unknown* $data) Unserializes the object from a serialized string | - [索引](# "总目录") - [下一页](# "Abstract class Phalcon\Mvc\Collection\Behavior") | - [上一页](# "Class Phalcon\Mvc\Application\Exception") | - [API Indice](#) »