Table Of Contents

Previous topic

< Class Phalcon\Mvc\Application\Exception

Next topic

Class Phalcon\Mvc\Collection\Document >

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 ([Phalcon\DiInterface $dependencyInjector])

Phalcon\Mvc\Model 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 (Phalcon\DiInterface $dependencyInjector)

Sets the dependency injection container

public Phalcon\DiInterface getDI ()

Returns the dependency injection container

protected setEventsManager (Phalcon\Events\ManagerInterface $eventsManager)

Sets a custom events manager

protected Phalcon\Events\ManagerInterface getEventsManager ()

Returns the custom events manager

public Phalcon\Mvc\Model\ManagerInterface getModelsManager ()

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 ()

Sets if a model must use implicit objects ids

protected Phalcon\Mvc\Collection setSource ()

Sets collection name which model should be mapped

public string getSource ()

Returns collection name mapped in the model

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

Sets the DependencyInjection connection service name

public string getConnectionService ()

Returns DependencyInjection connection service

public MongoDb getConnection ()

Retrieves a database connection

public mixed readAttribute (string $attribute)

Reads an attribute value by its name

<?php

echo $robot->readAttribute('name');

public writeAttribute (string $attribute, mixed $value)

Writes an attribute value by its name

<?php

$robot->writeAttribute('name', 'Rosey');

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

Returns a cloned collection

protected static array _getResultset ()

Returns a collection resultset

protected static int _getGroupResultset ()

Perform a count over a resultset

protected boolean _preSave ()

Executes internal hooks before save a document

protected boolean _postSave ()

Executes internal events after save a document

protected validate ()

Executes validators on every validation call

<?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 boolean validationHasFailed ()

Check whether validation process has generated any messages

<?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 boolean fireEvent (string $eventName)

Fires an internal event

public boolean fireEventCancel (string $eventName)

Fires an internal event that cancels the operation

protected boolean _cancelOperation ()

Cancel the current operation

protected _exists ()

Checks if the document exists in the collection

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

Returns all the validation messages

<?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 (Phalcon\Mvc\Model\MessageInterface $message)

Appends a customized message on the validation process

<?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 boolean save ()

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

public static Phalcon\Mvc\Collection findById (string|MongoId $id)

Find a document by its id (_id)

public static array findFirst ([array $parameters])

Allows to query the first record that match the specified conditions

<?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 array 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(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 array count ([array $parameters])

Perform a count over a collection

<?php

 echo 'There are ', Robots::count(), ' robots';

public static array aggregate (array $parameters)

Perform an aggregation using the Mongo aggregation framework

public static array summatory (string $field, [array $conditions], [string $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.

<?php

$robot = Robots::findFirst();
$robot->delete();

foreach (Robots::find() as $robot) {
    $robot->delete();
}

public array toArray ()

Returns the instance as an array representation

<?php

 print_r($robot->toArray());

public string serialize ()

Serializes the object ignoring connections or protected properties

public unserialize ([unknown $serialized])

Unserializes the object from a serialized string

public static array execute (mixed $code, [array $args])

Runs JavaScript code on the database server.

<?php

 $ret = Robots::execute("function() { return 'Hello, world!';}");
 echo $ret['retval'], "\n";