* making the fracking paginator work
[vsc.git] / _res / _libs / models / foo / fooentitya.class.php
blob6e229a4daba0b3103835ef49041584a490792621
1 <?php
2 /**
3 * The abstract object entity - it represents an entry in the database.
4 * It can be composed from more Entity Objects using reflection
6 * @package ts_models
7 * @author Marius Orcsik <marius@habarnam.ro>
8 * @date 09.02.26
9 */
10 usingPackage ('models/foo/fields');
11 usingPackage ('models/foo/indexes');
13 abstract class fooEntityA {
14 protected $name;
15 private $alias;
16 private $pk;
17 private $fields = array ();
18 private $indexes = array ();
20 public function __call ($sMethodName, $aParameters) {
21 // d ($sMethodName, $aParameters);
22 $i = preg_match ('/(set|get)(.*)/i', $sMethodName, $found );
23 if ($i) {
24 $sMethod = $found[1];
25 $sProperty = $found[2];
27 $sProperty[0] = strtolower ($sProperty[0]); // lowering the first letter
30 if ( $sMethod == 'set' ) {
31 // check for fields with $found[1] name
32 $this->$sProperty->setValue($aParameters[0]);
33 return true;
34 } else if ( $sMethod == 'get' ) {
35 return $this->$sProperty->getValue();
38 throw new tsExceptionUnimplemented ('Method [' . get_class ($this) . '::' . $sMethodName . ']');
41 public function __get ($sPropertyName) {
42 return $this->fields[$sPropertyName];
45 public function __set ($sPropertyName, $mValue) {
46 if (fooFieldA::isValid ($mValue)) {
47 $this->fields[$sPropertyName] = $mValue;
48 } else {
49 $this->fields[$sPropertyName]->setValue($mValue);
53 /**
54 * @param string $sName
55 * @return void
57 protected function setName ($sName) {
58 $this->name = $sName;
61 public function getName () {
62 return $this->name;
65 /**
66 * @param fooFieldA $oIndex
67 * @return void
69 public function setPrimaryKey () {
70 $this->pk = new fooKeyPrimary (func_get_args());
73 public function getPrimaryKey () {
74 return $this->pk;
77 /**
78 * @param fooFieldA[] $aFields
79 * @param string $sAlias
80 * @return void
82 private function addFields ($aFields, $sAlias) {
83 foreach ($aFields as $sFieldName => $oField) {
84 $this->addField (array ($sAlias . '.' . $sFieldName => $oField));
88 /**
90 * @param []$aIncField
91 * @return void
93 private function addField ($aIncField) {
94 $this->fields [$aIncField[0]] = $aIncField[1];
97 /**
98 * @return fooFieldA[]
100 public function getFields () {
101 return $this->fields;
105 * gets all the column names as an array
106 * @return string[]
108 public function getFieldNames () {
109 $aRet = array_keys($this->fields);
110 return $aRet;
113 public function getIndexes ($bWithPrimaryKey = false) {
114 $aIndexes = array ();
115 if ($bWithPrimaryKey)
116 $aIndexes[] = $this->getPrimaryKey();
118 $aIndexes = array_merge ($aIndexes, $this->indexes);
120 return $aIndexes;
124 * returns an array of key=>value for all properties of the current object
125 * @return mixed[]
127 public function toArray () {
128 $aRet = array();
130 foreach ($this->getFields() as $sFieldName => $oField) {
131 $aRet[$sFieldName] = $oField->getValue();
134 return $aRet;
138 * Receives an array of keys=> values and constructs an entity based on
139 * the existing ones.
140 * Returns:
141 * 1 if all array keys existed as properties of the object
142 * 0 if one of the keys didn't exist as a property of the object
143 * 2 if there were properties which didn't have a corresponding key=>value pair
144 * @param mixed[string] $aIncArray
145 * @return int
147 public function fromArray ($aIncArray) {
148 foreach ($aIncArray as $sFieldName => $mValue) {
149 try {
150 $this->fields[$sFieldName]->setValue ($mValue);
151 } catch (Exception $e) {
152 // dunno what might be thrown here
153 d ($e);
154 return 0;
157 return 1;
162 * @param fooEntityA $oChild
163 * @return bool
165 public function loadChild (fooEntityA $oChild) {}
169 * @param fooEntityA $oChild
170 * @return bool
172 public function join (fooEntityA $oObject) {
173 $this->addFields ($oObject->getFields (), $oObject->getName());