/system/class_modul_votings_answer.php

As one of the two model-beans, the answer of a single voting is represented by the class class_modul_votings_answer.


modul_votings_answer extends class_model implements interface_model {
private $strText = "";
private $intHits = 0;
public function __construct($strSystemid = "") {
$arrModul = array();
$arrModul["name"] = "modul_votings";
$arrModul["author"] = "sidler @ mulchprod.de";
$arrModul["moduleId"] = _votings_modul_id_;
$arrModul["modul"] = "votings";
$arrModul["table"] = _dbprefix_."votings_answer";
parent::__construct($arrModul, $strSystemid);

if($strSystemid != "")
$this->initObject();
}


As already known from the installer, the constructor is being used to define meta-informations.
The most of them are optional, but should be maintained for documentation purposes.
In addition, the initialization of the object is triggered as soon as a systemid was passed.


protected function getObjectTables() {
return array(_dbprefix_."votings_answer" => "votings_answer_id");
}


The method getObjectTables() returns the table(s) including the primary key(s) used to persist the object. If there's no matching record in the database during saving the record, the framework create san empty record automatically. Afterwards, the update-function of the bean is being called. The advantage of this procedure is, that the developer of the bean only has to maintain the update-beahviour. The “insert” known by databases is made by the framework transparently to the developer.
See in addition

protected function getObjectDescription() {
return "voting answer ".$this->getStrText();
}


The objects' description is used for documentation purposes, only, e.g. in the logfiles of the system.


public function initObject() {
$strQuery = "SELECT * FROM ".$this->arrModule["table"]."
WHERE votings_answer_id = '".dbsafeString($this->getSystemid())."'";
$arrRow = $this->objDB->getRow($strQuery);
$this->setStrText($arrRow["votings_answer_text"]);
$this->setIntHits($arrRow["votings_answer_hits"]);
}


It's obvious, that initObject() is responsible for initializing the current object. The state is loaded from the database and the values are assigned to the properties.


protected function updateStateToDb() {
class_logger::getInstance()->addLogRow("updated votings answer ".$this->getSystemid(),class_logger::$levelInfo);

$strQuery = "UPDATE ".$this->arrModule["table"]."
SET votings_answer_text = '".dbsafeString($this->getStrText())."',
votings_answer_hits = ".dbsafeString((int)$this->getIntHits())."
WHERE votings_answer_id = '".dbsafeString($this->getSystemid())."'";
return $this->objDB->_query($strQuery);
}


As already mentioned before, Kajona-beans only know how to update themselves. If an update is triggered from external, the framework calls the method updateStateToDb() on the bean. This method synchronizes the objects' state back to the database.
See in addition

public function deleteAnswer() {
class_logger::getInstance()->addLogRow("deleted answer ".$this->getObjectDescription(), class_logger::$levelInfo);
$strQuery = "DELETE FROM ".$this->arrModule["table"]." WHERE votings_answer_id = '".dbsafeString($this->getSystemid())."'";

if($this->objDB->_query($strQuery)) {
if($this->deleteSystemRecord($this->getSystemid()))
return true;
}
return false;
}
[ ... Getters and Setter ... ]
}


If the bean should be deleted, in this case an answer, the call of deleteAnswer() does the job. The bean is responsible of deleting its own data, all other records related with the bean are removed by calling deleteSystemRecord() of the super-class automatically.