Constants in Doctrine 2 Objects

Suppose I have the following Doctrine 2 object:

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 */
class User {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     *
     * @var int
     */
    protected $id;

    /**
     * @ORM\Column(length=100)
     *
     * @var string
     */
    protected $name;

    /**
     * @ORM\Column(type="integer")
     *
     * @var int
     */
    protected $status;
}

A user can have several statuses, for example: Waiting, Active, Hanging. These statuses are required throughout the code (services, repositories, etc.), as well as at the user interface level (the user editing form will display them in a drop-down list).

To avoid defining them in several places, what I have done so far has been to use a class to store them (all application constants) and looks something like this:

class App_Constants extends Zrzr_Constants
{

    protected static $_constants = array( 
        'users' => array( 
            'status' => array( 
                0 => 'Pending', 
                1 => 'Active',
                2 => 'Suspended' ) ) );

}

The base class (Zrzr_Constants) will offer some methods to extract them, and it looks like this:

class Zrzr_Constants
{
    protected static $_constants = array();

    public static function getConstantValues( $key, $subkey )
    {
        // ...
    }

    public static function getConstantByName( $name )
    {
        // ...
    }
}

General use:

// example of retrieval by constant name ... it would return an integer
$pendingStatus = App_Constants::getConstantByName( 'USERS.STATUS.PENDING' );

// example of retrieval for UI display purposes ... would return an array
$statuses = App_Constants::getConstantValues('users', 'status');

Of course, this means that there are some limitations in that constant labels cannot contain periods, but I can live with it.

Doctrine 2 DDD-, , , "" " " ( Doctrine 2 ), , , , ( const).

, , UI? ( ) ().

+5
2

, :

class User {
  const STATUS_PENDING = 'Pending';
  const STATUS_ACTIVE = 'Active';
  const STATUS_SUSPENDED = 'Suspended';

  public static function getStatusList() {
    return array(
                 self::STATUS_PENDING, 
                 self::STATUS_ACTIVE, 
                 self::STATUS_SUSPENDED
                );
  }

  public function getStatus() {...}

  public function setStatus($value) {...}

  public function isStatusPending() {...} //If you need it
}

( , , user_status_0). Symfony2 trans Twig user.

- , , , :: STATUS_XXX . , , .

, , .

+2

,

class ContactResource
{
   const TYPE_PHONE = 1;
   const TYPE_EMAIL = 2;
   const TYPE_BIRTDAY = 3;
   const TYPE_ADDRESS = 4;
   const TYPE_OTHER = 5;
   const TYPE_SKYPE = 6;
   const TYPE_LINKEDIN = 7;
   const TYPE_MEETUP = 8;
   const TYPE_TELEGRAM = 9;
   const TYPE_INSTAGRAM = 10;
   const TYPE_TWITTER = 11;

   public static $resourceType = array(
       ContactResource::TYPE_PHONE => "Phone",
       ContactResource::TYPE_EMAIL => "Email",
       ContactResource::TYPE_BIRTDAY => "Birtday",
       ContactResource::TYPE_ADDRESS => "Address",
       ContactResource::TYPE_OTHER => "Other",
       ContactResource::TYPE_SKYPE => "Skype",
       ContactResource::TYPE_LINKEDIN => "LinkedIn",
       ContactResource::TYPE_MEETUP => "Meetup",
       ContactResource::TYPE_TELEGRAM => "Telegram",
       ContactResource::TYPE_INSTAGRAM => "Instagram",
       ContactResource::TYPE_TWITTER => "Twitter",
   );

   /**
   * @var integer
   *
   * @ORM\Column(type="integer", length=2)
   *
   */
   private $type;


   public function __toString()
   {
      return (string)$this->getType();
   }

   public function getType()
   {
      if (!is_null($this->type)) {
          return self::$resourceType[$this->type];
      } else {
          return null;
      }
   }

   public static function getTypeList() {
      return self::$resourceType;
   }

}

Twig

{{ entity.type }}

ContactResource::getTypeList()

!

+2

All Articles