This is my ERD: https://minus.com/i/0CzNcg881AfW
Entities:
<?php
class Game extends \Phalcon\Mvc\Model
{
/** @var int Unique ID */
public $ID;
/** @var string Title */
public $name;
/** @var string Vendor */
public $vendor;
/** @var DateTime Release date */
public $released;
/** @var string Thumbnail image path */
public $image;
/** @var string About text */
public $about;
/**
* Initialize foreign keys
*/
public function initialize()
{
$this->hasMany('ID','Translation','ID'); //required if ORM dumps database schema before queries?
}
/**
* Get ISO 8601 formatted date
*/
public function toISO8601(){}
/**
* Create DateTime object after fetch
*/
protected function afterFetch()
{
$this->released = new DateTime($this->released);
}
/**
* Get table name
* @return string
*/
function getSource()
{
return 'games';
}
}
class Translation extends \Phalcon\Mvc\Model
{
/** @var int Unique ID */
public $ID;
/** @var Game Related game */
protected $game;
/** @var User User who translates it */
public $translator;
/** @var string Language code (PL, EN, DE, SK) */
public $language;
/** @var int Completion indicator (0-100) */
public $complete;
/** @var Translation base translation */
public $base;
/**
* Set virtual foreign keys
*/
public function initialize()
{
$this->belongsTo('game','Game','ID');
}
/**
* Get table name
* @return string
*/
function getSource()
{
return 'translations';
}
}
Entity names are singular, table names are plural. We have a translation. Now we want to get related game. If Translation::game is public, {{translation.game}} returns just game's ID. I changed its visibility to protected. Now ORM gets related game. But there is a problem. If you access the same related property twice, you get a fatal error:
{{translation.game.name}} first time it is OK
{{translation.game.name}} second time - exception
Catchable fatal error: Object of class Game could not be converted to string
Phalcon bug?
How to name foreign keys columns in tables to follow standards? Should its name be the same as entity or table's name or different?
- game
- gameid
IMO good ORM should map all foreign keys to related objects without any workarounds.
How to write models? Like the following?
public $id; // ID small or capital letters?
public $gameid; // only ID numeric
protected $game; // useless in Phalcon but I need an information for NetBeans IDE