Abstract class Phalcon\Mvc\Collection

implements 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\Model constructor

public setId (unknown $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 Phalcon\DiInterface getDI ()

Returns the dependency injection container

protected setEventsManager (unknown $eventsManager)

Sets a custom events manager

protected Phalcon\Events\ManagerInterface getEventsManager ()

Returns the custom events manager

public Phalcon\Mvc\Model\ManagerInterface getCollectionManager ()

Returns the models manager related to the entity instance

public array 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 Phalcon\Mvc\Collection setSource (unknown $source)

Sets collection name which model should be mapped

public string getSource ()

Returns collection name mapped in the model

public Phalcon\Mvc\Model setConnectionService (unknown $connectionService)

Sets the DependencyInjection connection service name

public string getConnectionService ()

Returns DependencyInjection connection service

public MongoDb getConnection ()

Retrieves a database connection

public mixed readAttribute (unknown $attribute)

Reads an attribute value by its name

  1. <?php
  2. echo robot->readAttribute('name');

public writeAttribute (unknown $attribute, unknown $value)

Writes an attribute value by its name

  1. <?php
  2. robot->writeAttribute('name', 'Rosey');

public static Phalcon\Mvc\Collection cloneResult (unknown $collection, unknown $document)

Returns a cloned collection

protected static array _getResultset (unknown $params, unknown $collection, unknown $connection, unknown $unique)

Returns a collection resultset

protected static int _getGroupResultset (unknown $params, unknown $collection, unknown $connection)

Perform a count over a resultset

final protected boolean _preSave (unknown $dependencyInjector, unknown $disableEvents, unknown $exists)

Executes internal hooks before save a document

final protected boolean _postSave (unknown $disableEvents, unknown $success, unknown $exists)

Executes internal events after save a document

protected validate (unknown $validator)

Executes validators on every validation call

  1. <?php
  2. use Phalcon\Mvc\Model\Validator\ExclusionIn as ExclusionIn;
  3. class Subscriptors extends \Phalcon\Mvc\Collection
  4. {
  5. public function validation()
  6. {
  7. this->validate(new ExclusionIn(array(
  8. 'field' => 'status',
  9. 'domain' => array('A', 'I')
  10. )));
  11. if (this->validationHasFailed() == true) {
  12. return false;
  13. }
  14. }
  15. }

public boolean validationHasFailed ()

Check whether validation process has generated any messages

  1. <?php
  2. use Phalcon\Mvc\Model\Validator\ExclusionIn as ExclusionIn;
  3. class Subscriptors extends \Phalcon\Mvc\Collection
  4. {
  5. public function validation()
  6. {
  7. this->validate(new ExclusionIn(array(
  8. 'field' => 'status',
  9. 'domain' => array('A', 'I')
  10. )));
  11. if (this->validationHasFailed() == true) {
  12. return false;
  13. }
  14. }
  15. }

public boolean fireEvent (unknown $eventName)

Fires an internal event

public boolean fireEventCancel (unknown $eventName)

Fires an internal event that cancels the operation

protected boolean _cancelOperation (unknown $disableEvents)

Cancel the current operation

protected boolean _exists (unknown $collection)

Checks if the document exists in the collection

public Phalcon\Mvc\Model\MessageInterface [] getMessages ()

Returns all the validation messages

  1. <?php
  2. robot = new Robots();
  3. robot->type = 'mechanical';
  4. robot->name = 'Astro Boy';
  5. robot->year = 1952;
  6. if (robot->save() == false) {
  7. echo "Umh, We can't store robots right now ";
  8. foreach (robot->getMessages() as message) {
  9. echo message;
  10. }
  11. } else {
  12. echo "Great, a new robot was saved successfully!";
  13. }

public appendMessage (unknown $message)

Appends a customized message on the validation process

  1. <?php
  2. use \Phalcon\Mvc\Model\Message as Message;
  3. class Robots extends \Phalcon\Mvc\Model
  4. {
  5. public function beforeSave()
  6. {
  7. if (this->name == 'Peter') {
  8. message = new Message("Sorry, but a robot cannot be named Peter");
  9. this->appendMessage(message);
  10. }
  11. }
  12. }

public boolean save ()

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

public static Phalcon\Mvc\Collection findById (unknown $id)

Find a document by its id (_id)

public static array findFirst ([unknown $parameters])

Allows to query the first record that match the specified conditions

  1. <?php
  2. //What's the first robot in the robots table?
  3. robot = Robots::findFirst();
  4. echo "The robot name is ", robot->name, "\n";
  5. //What's the first mechanical robot in robots table?
  6. robot = Robots::findFirst(array(
  7. array("type" => "mechanical")
  8. ));
  9. echo "The first mechanical robot name is ", robot->name, "\n";
  10. //Get first virtual robot ordered by name
  11. robot = Robots::findFirst(array(
  12. array("type" => "mechanical"),
  13. "order" => array("name" => 1)
  14. ));
  15. echo "The first virtual robot name is ", robot->name, "\n";

public static array find ([unknown $parameters])

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

  1. <?php
  2. //How many robots are there?
  3. robots = Robots::find();
  4. echo "There are ", count(robots), "\n";
  5. //How many mechanical robots are there?
  6. robots = Robots::find(array(
  7. array("type" => "mechanical")
  8. ));
  9. echo "There are ", count(robots), "\n";
  10. //Get and print virtual robots ordered by name
  11. robots = Robots::findFirst(array(
  12. array("type" => "virtual"),
  13. "order" => array("name" => 1)
  14. ));
  15. foreach (robots as robot) {
  16. echo robot->name, "\n";
  17. }
  18. //Get first 100 virtual robots ordered by name
  19. robots = Robots::find(array(
  20. array("type" => "virtual"),
  21. "order" => array("name" => 1),
  22. "limit" => 100
  23. ));
  24. foreach (robots as robot) {
  25. echo robot->name, "\n";
  26. }

public static array count ([unknown $parameters])

Perform a count over a collection

  1. <?php
  2. echo 'There are ', Robots::count(), ' robots';

public static array aggregate (unknown $parameters)

Perform an aggregation using the Mongo aggregation framework

public static array summatory (unknown $field, [unknown $conditions], [unknown $finalize])

Allows to perform a summatory group for a column in the collection

public boolean delete ()

Deletes a model instance. Returning true on success or false otherwise.

  1. <?php
  2. robot = Robots::findFirst();
  3. robot->delete();
  4. foreach (Robots::find() as robot) {
  5. robot->delete();
  6. }

public array toArray ()

Returns the instance as an array representation

  1. <?php
  2. print_r(robot->to[]);

public string serialize ()

Serializes the object ignoring connections or protected properties

public unserialize (unknown $data)

Unserializes the object from a serialized string