We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon + Pattern State

Hi, i need implement the pattern state with phalcon and mysql. The implementation of pattern is not a problem. I not understand how save data in mysql. Use this example https://sourcemaking.com/design_patterns/state/php

Help :) Thanks



8.7k

Correct link: state pattern php example

Why don't your show us what you've got so far.

Model Door

class **Door** extends \Phalcon\Mvc\Model
{

/**
 *
 * @var integer
 */
protected $door_id;

/**
 *
 * @var integer
 */
protected $door_stateId;

/**
 *
 * @var string
 */
protected $door_description;

/**
 * Method to set the value of field door_id
 *
 * @param integer $door_id
 * @return $this
 */
public function setDoorId($door_id)
{
    $this->door_id = $door_id;

    return $this;
}

/**
 * Method to set the value of field door_stateId
 *
 * @param integer $door_stateId
 * @return $this
 */
public function setDoorStateid($door_stateId)
{
    $this->door_stateId = $door_stateId;

    return $this;
}

/**
 * Method to set the value of field door_description
 *
 * @param string $door_description
 * @return $this
 */
public function setDoorDescription($door_description)
{
    $this->door_description = $door_description;

    return $this;
}

/**
 * Returns the value of field door_id
 *
 * @return integer
 */
public function getDoorId()
{
    return $this->door_id;
}

/**
 * Returns the value of field door_stateId
 *
 * @return integer
 */
public function getDoorStateid()
{
    return $this->door_stateId;
}

/**
 * Returns the value of field door_description
 *
 * @return string
 */
public function getDoorDescription()
{
    return $this->door_description;
}

/**
 * Returns table name mapped in the model.
 *
 * @return string
 */
public function getSource()
{
    return 'door';
}

/**
 * Allows to query a set of records that match the specified conditions
 *
 * @param mixed $parameters
 * @return Door[]
 */
public static function find($parameters = null)
{
    return parent::find($parameters);
}

/**
 * Allows to query the first record that match the specified conditions
 *
 * @param mixed $parameters
 * @return Door
 */
public static function findFirst($parameters = null)
{
    return parent::findFirst($parameters);
}

/**
 * @var DoorState
 */
private $state;
public function initialize(DoorState $state)
{
    $this->setState($state);
}
/**
 * @throws IllegalStateTransitionException
 */
public function open()
{
    $this->setState($this->state->open());
}
/**
 * @throws IllegalStateTransitionException
 */
public function close()
{
    $this->setState($this->state->close());
}
/**
 * @throws IllegalStateTransitionException
 */
public function lock()
{
    $this->setState($this->state->lock());
}
/**
 * @throws IllegalStateTransitionException
 */
public function unlock()
{
    $this->setState($this->state->unlock());
}
/**
 * @return bool
 */
public function isOpen()
{
    echo "es INSTANCIA DE OpenDoorState";
    return $this->state instanceof Dooropen;
}
/**
 * @return bool
 */
public function isClosed()
{
    echo "es INSTANCIA DE ClosedDoorState";
    return $this->state instanceof Doorclose;
}
/**
 * @return bool
 */
public function isLocked()
{
    echo "es INSTANCIA DE LockedDoorState";
    return $this->state instanceof Doorlocked;
}
private function setState(DoorState $state)
{
    $this->state = $state;
}

Model Interface DoorState

interface **DoorState** {
    public function open();
    public function close();
    public function lock();
    public function unlock();
}

Model DoorOpen implements DoorState

class **Dooropen** extends \Phalcon\Mvc\Model implements DoorState
{

  /**
   *
   * @var integer
   */
  protected $doorOpen_id;

  /**
   *
   * @var string
   */
  protected $doorOpen_description;

  /**
   * Method to set the value of field doorOpen_id
   *
   * @param integer $doorOpen_id
   * @return $this
   */
  public function setDooropenId($doorOpen_id)
  {
      $this->doorOpen_id = $doorOpen_id;

      return $this;
  }

  /**
   * Method to set the value of field doorOpen_description
   *
   * @param string $doorOpen_description
   * @return $this
   */
  public function setDooropenDescription($doorOpen_description)
  {
      $this->doorOpen_description = $doorOpen_description;

      return $this;
  }

  /**
   * Returns the value of field doorOpen_id
   *
   * @return integer
   */
  public function getDooropenId()
  {
      return $this->doorOpen_id;
  }

  /**
   * Returns the value of field doorOpen_description
   *
   * @return string
   */
  public function getDooropenDescription()
  {
      return $this->doorOpen_description;
  }

  /**
   * Returns table name mapped in the model.
   *
   * @return string
   */
  public function getSource()
  {
      return 'dooropen';
  }

  /**
   * Allows to query a set of records that match the specified conditions
   *
   * @param mixed $parameters
   * @return Dooropen[]
   */
  public static function find($parameters = null)
  {
      return parent::find($parameters);
  }

  /**
   * Allows to query the first record that match the specified conditions
   *
   * @param mixed $parameters
   * @return Dooropen
   */
  public static function findFirst($parameters = null)
  {
      return parent::findFirst($parameters);
  }
  public function initialize()
  {
      $this->setDooropenDescription("PUERTA ABIERTA");
      $this->setDooropenId(1);
  }

  public function close()
  {
      return new Doorclose();
  }

  public function open()
  {
      throw new IllegalStateTransitionException;
  }

  public function lock()
  {
      throw new IllegalStateTransitionException;
  }

  public function unlock()
  {
      throw new IllegalStateTransitionException;
  }
}

I do not understand how they are reflected in the database. The Door class has a $ state (DoorState interface) attribute, this field as it is stored in the database? The State classes are new tables in the database that relate to the state attribute of the Door class? I hope you understand

In your code, only two classes will be reflected in the database (those which extend Phalcon\Mvc\Model):

Door and Dooropen.

The properties of these classes will be mapped to DB columns, which you can fine-tune with:

class Door extends \Phalcon\Mvc\Model {
    public function columnMap() {
        return [
            '<db_column_name>' => '<model_property_name>'
        ];
    }
}

In case I want to add a new state, for example DoorClosed, it should be reflected in the database. I am right? How should I change the relationship in colummap?

    class Door extends \Phalcon\Mvc\Model {
        public function columnMap() {
            return [
                '<db_column_name>' => '<model_property_name>',
                '<db_column_name2>' => '<model_property_name2>'
            ];
        }
    }