Table Of Contents

Previous topic

< Class Phalcon\Mvc\Model\Exception

Next topic

Class Phalcon\Mvc\Model\Message >

Class Phalcon\Mvc\Model\Manager

implements Phalcon\Mvc\Model\ManagerInterface, Phalcon\DI\InjectionAwareInterface, Phalcon\Events\EventsAwareInterface

This components controls the initialization of models, keeping record of relations between the different models of the application. A ModelsManager is injected to a model via a Dependency Injector/Services Container such as Phalcon\DI.

<?php

 $di = new Phalcon\DI();

 $di->set('modelsManager', function() {
      return new Phalcon\Mvc\Model\Manager();
 });

 $robot = new Robots($di);

Methods

public setDI (Phalcon\DiInterface $dependencyInjector)

Sets the DependencyInjector container

public Phalcon\DiInterface getDI ()

Returns the DependencyInjector container

public setEventsManager (Phalcon\Events\ManagerInterface $eventsManager)

Sets a global events manager

public Phalcon\Events\ManagerInterface getEventsManager ()

Returns the internal event manager

public setCustomEventsManager (Phalcon\Mvc\ModelInterface $model, Phalcon\Events\ManagerInterface $eventsManager)

Sets a custom events manager for a specific model

public Phalcon\Events\ManagerInterface getCustomEventsManager (Phalcon\Mvc\ModelInterface $model)

Returns a custom events manager related to a model

public boolean initialize (Phalcon\Mvc\ModelInterface $model)

Initializes a model in the model manager

public bool isInitialized (string $modelName)

Check whether a model is already initialized

public Phalcon\Mvc\ModelInterface getLastInitialized ()

Get last initialized model

public Phalcon\Mvc\ModelInterface load (string $modelName, boolean $newInstance)

Loads a model throwing an exception if it doesn’t exist

public string setModelSource (Phalcon\Mvc\Model $model, string $source)

Sets the mapped source for a model

public string getModelSource (Phalcon\Mvc\Model $model)

Returns the mapped source for a model

public string setModelSchema (Phalcon\Mvc\Model $model, string $schema)

Sets the mapped schema for a model

public string getModelSchema (Phalcon\Mvc\Model $model)

Returns the mapped schema for a model

public setConnectionService (Phalcon\Mvc\ModelInterface $model, string $connectionService)

Sets both write and read connection service for a model

public setWriteConnectionService (Phalcon\Mvc\ModelInterface $model, string $connectionService)

Sets write connection service for a model

public setReadConnectionService (Phalcon\Mvc\ModelInterface $model, string $connectionService)

Sets read connection service for a model

public Phalcon\Db\AdapterInterface getWriteConnection (Phalcon\Mvc\ModelInterface $model)

Returns the connection to write data related to a model

public Phalcon\Db\AdapterInterface getReadConnection (Phalcon\Mvc\ModelInterface $model)

Returns the connection to read data related to a model

public getReadConnectionService (Phalcon\Mvc\ModelInterface $model)

Returns the connection service name used to read data related to a model

public getWriteConnectionService (Phalcon\Mvc\ModelInterface $model)

Returns the connection service name used to write data related to a model

public notifyEvent (string $eventName, Phalcon\Mvc\ModelInterface $model)

Receives events generated in the models and dispatches them to a events-manager if available Notify the behaviors that are listening in the model

public boolean missingMethod (Phalcon\Mvc\ModelInterface $model, string $eventName, array $data)

Dispatch a event to the listeners and behaviors This method expects that the endpoint listeners/behaviors returns true meaning that a least one is implemented

public addBehavior (Phalcon\Mvc\ModelInterface $model, Phalcon\Mvc\Model\BehaviorInterface $behavior)

Binds a behavior to a model

public keepSnapshots (Phalcon\Mvc\Model $model, boolean $keepSnapshots)

Sets if a model must keep snapshots

public boolean isKeepingSnapshots (unknown $model)

Checks if a model is keeping snapshots for the queried records

public useDynamicUpdate (Phalcon\Mvc\Model $model, boolean $dynamicUpdate)

Sets if a model must use dynamic update instead of the all-field update

public boolean isUsingDynamicUpdate (unknown $model)

Checks if a model is using dynamic update instead of all-field update

public Phalcon\Mvc\Model\Relation addHasOne (Phalcon\Mvc\Model $model, mixed $fields, string $referencedModel, mixed $referencedFields, [array $options])

Setup a 1-1 relation between two models

public Phalcon\Mvc\Model\Relation addBelongsTo (Phalcon\Mvc\Model $model, mixed $fields, string $referencedModel, mixed $referencedFields, [array $options])

Setup a relation reverse many to one between two models

public addHasMany (Phalcon\Mvc\ModelInterface $model, mixed $fields, string $referencedModel, mixed $referencedFields, [array $options])

Setup a relation 1-n between two models

public Phalcon\Mvc\Model\Relation addHasManyToMany (unknown $model, string $fields, string $intermediateModel, string $intermediateFields, string $intermediateReferencedFields, string $referencedModel, string $referencedFields, [array $options])

Setups a relation n-m between two models

public boolean existsBelongsTo (string $modelName, string $modelRelation)

Checks whether a model has a belongsTo relation with another model

public boolean existsHasMany (string $modelName, string $modelRelation)

Checks whether a model has a hasMany relation with another model

public boolean existsHasOne (string $modelName, string $modelRelation)

Checks whether a model has a hasOne relation with another model

public boolean existsHasManyToMany (string $modelName, string $modelRelation)

Checks whether a model has a hasManyToMany relation with another model

public Phalcon\Mvc\Model\Relation getRelationByAlias (string $modelName, string $alias)

Returns a relation by its alias

public Phalcon\Mvc\Model\Resultset\Simple getRelationRecords (Phalcon\Mvc\Model\Relation $relation, string $method, Phalcon\Mvc\ModelInterface $record, [array $parameters])

Helper method to query records based on a relation definition

public object getReusableRecords (string $modelName, string $key)

Returns a reusable object from the internal list

public setReusableRecords (string $modelName, string $key, mixed $records)

Stores a reusable record in the internal list

public clearReusableObjects ()

Clears the internal reusable list

public Phalcon\Mvc\Model\ResultsetInterface getBelongsToRecords (string $method, string $modelName, string $modelRelation, Phalcon\Mvc\Model $record, [array $parameters])

Gets belongsTo related records from a model

public Phalcon\Mvc\Model\ResultsetInterface getHasManyRecords (string $method, string $modelName, string $modelRelation, Phalcon\Mvc\Model $record, [array $parameters])

Gets hasMany related records from a model

public Phalcon\Mvc\Model\ResultsetInterface getHasOneRecords (string $method, string $modelName, string $modelRelation, Phalcon\Mvc\Model $record, [array $parameters])

Gets belongsTo related records from a model

public Phalcon\Mvc\Model\RelationInterface [] getBelongsTo (Phalcon\Mvc\ModelInterface $model)

Gets all the belongsTo relations defined in a model

<?php

$relations = $modelsManager->getBelongsTo(new Robots());

public Phalcon\Mvc\Model\RelationInterface [] getHasMany (Phalcon\Mvc\ModelInterface $model)

Gets hasMany relations defined on a model

public array getHasOne (Phalcon\Mvc\ModelInterface $model)

Gets hasOne relations defined on a model

public Phalcon\Mvc\Model\RelationInterface [] getHasManyToMany (Phalcon\Mvc\ModelInterface $model)

Gets hasManyToMany relations defined on a model

public array getHasOneAndHasMany (Phalcon\Mvc\ModelInterface $model)

Gets hasOne relations defined on a model

public Phalcon\Mvc\Model\RelationInterface [] getRelations (string $modelName)

Query all the relationships defined on a model

public Phalcon\Mvc\Model\RelationInterface getRelationsBetween (string $first, string $second)

Query the first relationship defined between two models

public Phalcon\Mvc\Model\QueryInterface createQuery (string $phql)

Creates a Phalcon\Mvc\Model\Query without execute it

public Phalcon\Mvc\Model\QueryInterface executeQuery (string $phql, [array $placeholders])

Creates a Phalcon\Mvc\Model\Query and execute it

public Phalcon\Mvc\Model\Query\BuilderInterface createBuilder ([string $params])

Creates a Phalcon\Mvc\Model\Query\Builder

public Phalcon\Mvc\Model\QueryInterface getLastQuery ()

Returns the lastest query created or executed in the models manager

public registerNamespaceAlias (string $alias, string $namespace)

Registers shorter aliases for namespaces in PHQL statements

public string getNamespaceAlias (string $alias)

Returns a real namespace from its alias

public array getNamespaceAliases ()

Returns all the registered namespace aliases

public __destruct ()

Destroys the PHQL cache