我可以在PhalconPHP上为两个表构建一个模型吗?

时间:2014-07-17 14:23:48

标签: mysql phalcon

我有一个games表和两个表来存储他们的分数,game_scoresthirdparty_game_scores具有完全相同的结构,类似于:

-------------------------GAMES-------------------------
slug | title | technology | (other non-relevant fields)
-------------------------------------------------------

----------------------GAME SCORES----------------------
 id (PK) | game_slug | user | team | score | timestamp
-------------------------------------------------------

----------------THIRDPARTY GAME SCORES-----------------
 id (PK) | game_slug | user | team | score | timestamp
-------------------------------------------------------

他们的模特如:

class GameScores extends BaseGamesModel {

  public function initialize() {
    parent::initialize();
    $this->belongsTo( 'game_slug', 'Games', 'slug' );
    $this->skipAttributes(array('creation_time', 'last_modified'));
  }
}

我建立了每周排名,游戏分数仅存储在game_scores

$ranking = GameScores::sum(
  array(
    "column" => "score",
    "conditions" => $conditions,
    "group" => "team",
    "order" => "sumatory DESC"
  )
);

有没有办法创建一个模型来连接game_scoresthirdparty_game_scores的所有数据,以创建相同的排名而不需要额外的努力?

非常感谢你!

1 个答案:

答案 0 :(得分:0)

您可以在模型中动态设置源表。见Can I build a Model for two Tables on PhalconPHP?

您应该使用setSource()方法并将条件放在那里,例如:

   $source = 'default_table';
   if (class_name($this) === 'A') {
      $source = 'A_table'; 
   }
   return $source;