Table Of Contents

Previous topic

Class Phalcon\Mvc\Micro\Exception

Next topic

Class Phalcon\Mvc\Model\Criteria

This Page

Class Phalcon\Mvc\Model

implements Phalcon\Mvc\ModelInterface, Phalcon\Mvc\Model\ResultInterface, Phalcon\DI\InjectionAwareInterface, Phalcon\Events\EventsAwareInterface, Serializable

Phalcon\Mvc\Model connects business objects and database tables to create a persistable domain model where logic and data are presented in one wrapping. It‘s an implementation of the object-relational mapping (ORM). A model represents the information (data) of the application and the rules to manipulate that data. Models are primarily used for managing the rules of interaction with a corresponding database table. In most cases, each table in your database will correspond to one model in your application. The bulk of your application’s business logic will be concentrated in the models. Phalcon\Mvc\Model is the first ORM written in C-language for PHP, giving to developers high performance when interacting with databases while is also easy to use.

<?php

 $robot = new Robots();
 $robot->type = 'mechanical'
 $robot->name = 'Astro Boy';
 $robot->year = 1952;
 if ($robot->save() == false) {
  echo "Umh, We can store robots: ";
  foreach ($robot->getMessages() as $message) {
    echo $message;
  }
 } else {
  echo "Great, a new robot was saved successfully!";
 }

Constants

integer OP_NONE

integer OP_CREATE

integer OP_UPDATE

integer OP_DELETE

Methods

final public __construct (Phalcon\DiInterface $dependencyInjector, string $managerService, string $dbService)

Phalcon\Mvc\Model constructor

public setDI (Phalcon\DiInterface $dependencyInjector)

Sets the dependency injection container

public Phalcon\DiInterface getDI ()

Returns the dependency injection container

public setEventsManager (Phalcon\Events\ManagerInterface $eventsManager)

Sets the event manager

public Phalcon\Events\ManagerInterface getEventsManager ()

Returns the internal event manager

public Phalcon\Mvc\Model setTransaction (Phalcon\Mvc\Model\TransactionInterface $transaction)

Sets a transaction related to the Model instance

<?php

try {

  $transactionManager = new Phalcon\Mvc\Model\Transaction\Manager();

  $transaction = $transactionManager->get();

  $robot = new Robots();
  $robot->setTransaction($transaction);
  $robot->name = 'WALL·E';
  $robot->created_at = date('Y-m-d');
  if($robot->save()==false){
    $transaction->rollback("Can't save robot");
  }

  $robotPart = new RobotParts();
  $robotPart->setTransaction($transaction);
  $robotPart->type = 'head';
  if ($robotPart->save() == false) {
    $transaction->rollback("Can't save robot part");
  }

  $transaction->commit();

}
catch(Phalcon\Mvc\Model\Transaction\Failed $e){
  echo 'Failed, reason: ', $e->getMessage();
}

protected Phalcon\Mvc\Model setSource ()

Sets table name which model should be mapped

public string getSource ()

Returns table name mapped in the model

protected Phalcon\Mvc\Model setSchema ()

Sets schema name where table mapped is located

public string getSchema ()

Returns schema name where table mapped is located

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

Sets the DependencyInjection connection service

public string getConnectionService ()

Returns DependencyInjection connection service

public setForceExists (boolean $forceExists)

Forces that model doesn’t need to be checked if exists before store it

public Phalcon\Db\AdapterInterface getConnection ()

Gets the internal database connection

public static Phalcon\Mvc\Model $result dumpResultMap (Phalcon\Mvc\Model $base, array $data, array $columnMap, boolean $forceExists)

Assigns values to a model from an array returning a new model.

<?php

$robot = Phalcon\Mvc\Model::dumpResult(new Robots(), array(
  'type' => 'mechanical',
  'name' => 'Astro Boy',
  'year' => 1952
));

public static Phalcon\Mvc\Model $result dumpResult (Phalcon\Mvc\Model $base, array $data, boolean $forceExists)

Assigns values to a model from an array returning a new model.

<?php

$robot = Phalcon\Mvc\Model::dumpResult(new Robots(), array(
  'type' => 'mechanical',
  'name' => 'Astro Boy',
  'year' => 1952
));

public static Phalcon\Mvc\Model\ResultsetInterface 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);

 //How many mechanical robots are there?
 $robots = Robots::find("type='mechanical'");
 echo "There are ", count($robots);

 //Get and print virtual robots ordered by name
 $robots = Robots::find(array("type='virtual'", "order" => "name"));
 foreach ($robots as $robot) {
   echo $robot->name, "\n";
 }

 //Get first 100 virtual robots ordered by name
 $robots = Robots::find(array("type='virtual'", "order" => "name", "limit" => 100));
 foreach ($robots as $robot) {
   echo $robot->name, "\n";
 }

public static Phalcon\Mvc\Model findFirst (array $parameters)

Allows to query the first record that match the specified conditions

<?php

 //What's the first robot in robots table?
 $robot = Robots::findFirst();
 echo "The robot name is ", $robot->name;

 //What's the first mechanical robot in robots table?
 $robot = Robots::findFirst("type='mechanical'");
 echo "The first mechanical robot name is ", $robot->name;

 //Get first virtual robot ordered by name
 $robot = Robots::findFirst(array("type='virtual'", "order" => "name"));
 echo "The first virtual robot name is ", $robot->name;

public static Phalcon\Mvc\Model\Criteria query (unknown $dependencyInjector)

Create a criteria for a especific model

protected boolean _exists ()

Checks if the current record already exists or not

protected static Phalcon\Mvc\Model\ResultsetInterface _groupResult ()

Generate a PHQL SELECT statement for an aggregate

public static int count (array $parameters)

Allows to count how many records match the specified conditions

<?php

 //How many robots are there?
 $number = Robots::count();
 echo "There are ", $number;

 //How many mechanical robots are there?
 $number = Robots::count("type='mechanical'");
 echo "There are ", $number, " mechanical robots";

public static double sum (array $parameters)

Allows to calculate a summatory on a column that match the specified conditions

<?php

 //How much are all robots?
 $sum = Robots::sum(array('column' => 'price'));
 echo "The total price of robots is ", $sum;

 //How much are mechanical robots?
 $sum = Robots::sum(array("type='mechanical'", 'column' => 'price'));
 echo "The total price of mechanical robots is  ", $sum;

public static mixed maximum (array $parameters)

Allows to get the maximum value of a column that match the specified conditions

<?php

 //What is the maximum robot id?
 $id = Robots::maximum(array('column' => 'id'));
 echo "The maximum robot id is: ", $id;

 //What is the maximum id of mechanical robots?
 $sum = Robots::maximum(array("type='mechanical'", 'column' => 'id'));
 echo "The maximum robot id of mechanical robots is ", $id;

public static mixed minimum (array $parameters)

Allows to get the minimum value of a column that match the specified conditions

<?php

 //What is the minimum robot id?
 $id = Robots::minimum(array('column' => 'id'));
 echo "The minimum robot id is: ", $id;

 //What is the minimum id of mechanical robots?
 $sum = Robots::minimum(array("type='mechanical'", 'column' => 'id'));
 echo "The minimum robot id of mechanical robots is ", $id;

public static double average (array $parameters)

Allows to calculate the average value on a column matching the specified conditions

<?php

 //What's the average price of robots?
 $average = Robots::average(array('column' => 'price'));
 echo "The average price is ", $average;

 //What's the average price of mechanical robots?
 $average = Robots::average(array("type='mechanical'", 'column' => 'price'));
 echo "The average price of mechanical robots is ", $average;

protected boolean _callEvent ()

Fires an internal event

protected boolean _callEventCancel ()

Fires an internal event that cancels the operation

protected boolean _cancelOperation ()

Cancel the current operation

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);
     }
   }
 }

protected validate ()

Executes validators on every validation call

<?php

use Phalcon\Mvc\Model\Validator\ExclusionIn as ExclusionIn;

class Subscriptors extends Phalcon\Mvc\Model
{

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\Model
{

public function validation()
  {
            $this->validate(new ExclusionIn(array(
            'field' => 'status',
            'domain' => array('A', 'I')
    )));
    if ($this->validationHasFailed() == true) {
            return false;
    }
}

}

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!";
}

protected boolean _checkForeignKeys ()

Reads “belongs to” relations and check the virtual foreign keys when inserting or updating records

protected boolean _checkForeignKeysReverse ()

Reads both “hasMany” and “hasOne” relations and check the virtual foreign keys when deleting records

protected boolean _preSave ()

Executes internal hooks before save a record

protected boolean _postSave ()

Executes internal events after save a record

protected boolean _doLowInsert ()

Sends a pre-build INSERT SQL statement to the relational database system

protected boolean _doLowUpdate ()

Sends a pre-build UPDATE SQL statement to the relational database system

public boolean save (array $data)

Inserts or updates a model instance. Returning true on success or false otherwise.

<?php

//Creating a new robot
$robot = new Robots();
$robot->type = 'mechanical'
$robot->name = 'Astro Boy';
$robot->year = 1952;
$robot->save();

//Updating a robot name
$robot = Robots::findFirst("id=100");
$robot->name = "Biomass";
$robot->save();

public boolean create (array $data)

Inserts a model instance. If the instance already exists in the persistance it will throw an exception Returning true on success or false otherwise.

<?php

//Creating a new robot
$robot = new Robots();
$robot->type = 'mechanical'
$robot->name = 'Astro Boy';
$robot->year = 1952;
$robot->create();

  //Passing an array to create
  $robot = new Robots();
  $robot->create(array(
      'type' => 'mechanical',
      'name' => 'Astroy Boy',
      'year' => 1952
  ));

public boolean update (array $data)

Updates a model instance. If the instance doesn’t exist in the persistance it will throw an exception Returning true on success or false otherwise.

<?php

//Updating a robot name
$robot = Robots::findFirst("id=100");
$robot->name = "Biomass";
$robot->update();

public boolean delete ()

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

<?php

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

foreach(Robots::find("type = 'mechanical'") as $robot){
   $robot->delete();
}

public int getOperationMade ()

Returns the type of the latest operation performed by the ORM Returns one of the OP_* class constants

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');

protected skipAttributes ()

Sets a list of attributes that must be skipped from the generated INSERT/UPDATE statement

<?php

class Robots extends \Phalcon\Mvc\Model
{

   public function initialize()
   {
       $this->skipAttributes(array('price'));
   }

}

protected skipAttributesOnCreate ()

Sets a list of attributes that must be skipped from the generated INSERT statement

<?php

class Robots extends \Phalcon\Mvc\Model
{

   public function initialize()
   {
       $this->skipAttributesOnUpdate(array('created_at'));
   }

}

protected skipAttributesOnUpdate ()

Sets a list of attributes that must be skipped from the generated UPDATE statement

<?php

class Robots extends \Phalcon\Mvc\Model
{

   public function initialize()
   {
       $this->skipAttributesOnUpdate(array('modified_in'));
   }

}

protected hasOne ()

Setup a 1-1 relation between two models

<?php

class Robots extends \Phalcon\Mvc\Model
{

   public function initialize()
   {
       $this->hasOne('id', 'RobotsDescription', 'robots_id');
   }

}

protected belongsTo ()

Setup a relation reverse 1-1 between two models

<?php

class RobotsParts extends \Phalcon\Mvc\Model
{

   public function initialize()
   {
       $this->belongsTo('robots_id', 'Robots', 'id');
   }

}

protected hasMany ()

Setup a relation 1-n between two models

<?php

class Robots extends \Phalcon\Mvc\Model
{

   public function initialize()
   {
       $this->hasMany('id', 'RobotsParts', 'robots_id');
   }

}

public Phalcon\Mvc\Model\ResultsetInterface getRelated (string $modelName, array $arguments)

Returns related records based on defined relations

protected mixed __getRelatedRecords ()

Returns related records defined relations depending on the method name

public mixed __call (string $method, array $arguments)

Handles methods when a method does not exist

public string serialize ()

Serializes the object ignoring connections or static properties

public unserialize (string $data)

Unserializes the object from a serialized string

public dump ()

...