Combu Server  3.1.1
PHP API Documentation
GameMatch_Account.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Combu;
4 
11 
12  const TABLE_NAME = "Match_Account";
13 
14  public $Id = 0;
15  public $IdMatch = 0;
16  public $IdAccount = 0;
17  public $CustomData = "";
18 
22  public function __construct($src = null, $stripSlashes = false) {
23  global $Database;
24  if ($src == null)
25  return;
26  if (is_array($src)) {
27  // Load by array
28  $this->_loadByRow($src, $stripSlashes);
29  } else if (is_numeric($src) && intval($src) > 0) {
30  // Load by Id
31  $this->_loadFilter(self::GetTableName(__CLASS__), "Id = " . intval($src));
32  }
33  }
34 
42  public static function Load($idMatch = 0, $idAccount = 0, $returnArray = FALSE) {
43  $where = "";
44  if ($idMatch > 0)
45  $where .= ($where ? " AND " : "") . sprintf("(IdMatch = %d)", $idMatch);
46  if ($idAccount > 0)
47  $where .= ($where ? " AND " : "") . sprintf("(IdAccount = %d)", $idAccount);
48  return self::_load(self::GetTableName(__CLASS__), ($returnArray ? "" : __CLASS__), $where, "Id");
49  }
50 
56  public function Save() {
57  global $Database;
58  if ($this->IdMatch < 1 || $this->IdAccount < 1) {
59  return FALSE;
60  }
61  if ($this->Id > 0) {
62  $query = sprintf("UPDATE %s SET CustomData = '%s' WHERE Id = %d",
63  self::GetTableName(__CLASS__),
64  $Database->Escape($this->CustomData),
65  $this->Id);
66  } else {
67  $this->DateCreation = date("Y-m-d H:i:s");
68  $query = sprintf("INSERT INTO %s (IdMatch, IdAccount, CustomData) VALUES (%d, %d, '%s')",
69  self::GetTableName(__CLASS__),
70  $this->IdMatch,
71  $this->IdAccount,
72  $Database->Escape($this->CustomData));
73  }
74  if ($Database->Query($query)) {
75  if ($this->Id < 1) {
76  $this->Id = $Database->InsertedId();
77  }
78  return TRUE;
79  }
80  return FALSE;
81  }
82 
87  public function Delete() {
88  if ($this->Id > 0) {
89  if ($this->_Delete(self::GetTableName(__CLASS__), sprintf("Id = %d", $this->Id))) {
90  $this->_Delete(self::GetTableName(GameMatch_Round::class), sprintf("IdMatchAccount = %d", $this->Id));
91  return TRUE;
92  }
93  }
94  return FALSE;
95  }
96 
97  public function ToArray() {
98  $user = new Account($this->IdAccount);
99  $array = Utils::ObjectToArray($this);
100  $array["User"] = $user->ToArray();
101  $array["Rounds"] = array();
102  $rounds = GameMatch_Round::Load($this->Id);
103  foreach ($rounds as $r) {
104  $array["Rounds"][] = $r->ToArray();
105  }
106  return $array;
107  }
108 
109  public function ToJson() {
110  $array = $this->ToArray();
111  return json_encode($array);
112  }
113 }
__construct($src=null, $stripSlashes=false)
Definition: Account.php:3
static Load($idMatch=0, $idAccount=0, $returnArray=FALSE)