From 15334b3966e17c3d7598ba007d590ad0e9b5857c Mon Sep 17 00:00:00 2001 From: klingac Date: Sun, 29 Mar 2009 17:11:04 +0200 Subject: [PATCH] modified: application/models/Player.php parameter player_id is no more obligatory, Player doesn't load data from DB as default --- application/models/Player.php | 71 +++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 43 deletions(-) diff --git a/application/models/Player.php b/application/models/Player.php index b9f060e..c104388 100644 --- a/application/models/Player.php +++ b/application/models/Player.php @@ -4,40 +4,42 @@ class Player const PLAYER_ID = 'player_id'; protected $_table; - protected $_player_id; + protected $_player_id = null; protected $_info; // @todo bude sa slectovat cez id alebo cez login? - public function __construct($player_id) + public function __construct($player_id = null) { - //ziskat vsetky data o uzivatelovi - $this->_player_id = $player_id; - $table = $this->getTable(); - $select = $table->select()->where(self::PLAYER_ID.'=?', $player_id); - $this->_info = $table->fetchRow($select)->toArray(); + if (null !== $player_id) { + //ziskat vsetky data o uzivatelovi + $this->_player_id = $player_id; + $table = $this->getTable(); + $select = $table->select()->where(self::PLAYER_ID.'=?', $player_id); + $this->_info = $table->fetchRow($select)->toArray(); + } } public function getID() { - return $this->_player_id; + return $this->_player_id; } public function getInfo($info_offset = '') { if (empty($info_offset)) { - return $this->_info; - } - else { - return $this->_info[$info_offset]; - } + return $this->_info; + } + else { + return $this->_info[$info_offset]; + } } public function changeInfo($offset, $value) { - $this->_info[$offset] = $value; - $this->getTable()->update(array($offset,$value), $this->PLAYER_ID.' = '.$this->_player_id); - return $this; + $this->_info[$offset] = $value; + $this->getTable()->update(array($offset,$value), $this->PLAYER_ID.' = '.$this->_player_id); + return $this; } /** @@ -57,30 +59,6 @@ class Player } /** - * Fetch all entries - * - * @return Zend_Db_Table_Rowset_Abstract - */ - protected function fetchEntries() - { - return $this->getTable()->fetchAll('1')->toArray(); - } - - /** - * Fetch an individual entry - * - * @param int|string $id - * @return null|Zend_Db_Table_Row_Abstract - */ - protected function fetchEntry($id) - { - $table = $this->getTable(); - $select = $table->select()->where('id = ?', $id); - // see reasoning in fetchEntries() as to why we return only an array - return $table->fetchRow($select)->toArray(); - } - - /** * Save a new entry * * @param array $data @@ -89,14 +67,21 @@ class Player protected function save(array $data) { $table = $this->getTable(); - $fields = $table->info(Zend_Db_Table_Abstract::COLS); + $fields = $table->info(Zend_Db_Table_Abstract::COLS); + $player_id = $this->getID(); foreach ($data as $field => $value) { if (!in_array($field, $fields)) { unset($data[$field]); } - } - return $table->insert($data); + } + if (null !== $player_id) { + $table->update($data, $this->PLAYER_ID.' = '.$player_id); + return $player_id; + } + else { + return $table->insert($data); + } } } ?> -- 2.11.4.GIT