1: <?php
2: /**
3: * A helper class used by the QCubed Code Generator to describe a database Table
4: * @package Codegen
5: *
6: * @property int $OwnerDbIndex
7: * @property string $Name
8: * @property string $ClassNamePlural
9: * @property string $ClassName
10: * @property QSqlColumn[] $ColumnArray
11: * @property QSqlColumn[] $PrimaryKeyColumnArray
12: * @property QReverseReference[] $ReverseReferenceArray
13: * @property QManyToManyReference[] $ManyToManyReferenceArray
14: * @property QIndex[] $IndexArray
15: * @property-read int $ReferenceCount
16: */
17: class QSqlTable extends QBaseClass {
18:
19: /////////////////////////////
20: // Protected Member Variables
21: /////////////////////////////
22:
23: /**
24: * @var int DB Index to which it belongs in the configuration.inc.php and codegen_settings.xml files.
25: */
26: protected $intOwnerDbIndex;
27:
28: /**
29: * Name of the table (as defined in the database)
30: * @var string Name
31: */
32: protected $strName;
33:
34: /**
35: * Name as a PHP Class
36: * @var string ClassName
37: */
38: protected $strClassName;
39:
40: /**
41: * Pluralized Name as a collection of objects of this PHP Class
42: * @var string ClassNamePlural;
43: */
44: protected $strClassNamePlural;
45:
46: /**
47: * Array of Column objects (as indexed by Column name)
48: * @var QSqlColumn[] ColumnArray
49: */
50: protected $objColumnArray;
51:
52: /**
53: * Array of ReverseReverence objects (indexed numerically)
54: * @var QReverseReference[] ReverseReferenceArray
55: */
56: protected $objReverseReferenceArray;
57:
58: /**
59: * Array of ManyToManyReference objects (indexed numerically)
60: * @var QManyToManyReference[] ManyToManyReferenceArray
61: */
62: protected $objManyToManyReferenceArray;
63:
64: /**
65: * Array of Index objects (indexed numerically)
66: * @var QIndex[] IndexArray
67: */
68: protected $objIndexArray;
69:
70: /**
71: * @var array developer specified options.
72: */
73: protected $options;
74:
75:
76:
77: /////////////////////
78: // Public Constructor
79: /////////////////////
80:
81: /**
82: * Default Constructor. Simply sets up the TableName and ensures that ReverseReferenceArray is a blank array.
83: *
84: * @param string $strName Name of the Table
85: */
86: public function __construct($strName) {
87: $this->strName = $strName;
88: $this->objReverseReferenceArray = array();
89: $this->objManyToManyReferenceArray = array();
90: $this->objColumnArray = array();
91: $this->objIndexArray = array();
92: }
93:
94:
95: /**
96: * return the QSqlColumn object related to that column name
97: * @param string $strColumnName Name of the column
98: * @return QSqlColumn
99: */
100: public function GetColumnByName($strColumnName) {
101: if ($this->objColumnArray) {
102: foreach ($this->objColumnArray as $objColumn){
103: if ($objColumn->Name == $strColumnName)
104: return $objColumn;
105: }
106: }
107: return null;
108: }
109:
110: /**
111: * Search within the table's columns for the given column
112: * @param string $strColumnName Name of the column
113: * @return boolean
114: */
115: public function HasColumn($strColumnName){
116: return ($this->GetColumnByName($strColumnName) !== null);
117: }
118:
119: /**
120: * Return the property name for a given column name (false if it doesn't exists)
121: * @param string $strColumnName name of the column
122: * @return string
123: */
124: public function LookupColumnPropertyName($strColumnName){
125: $objColumn = $this->GetColumnByName($strColumnName);
126: if ($objColumn)
127: return $objColumn->PropertyName;
128: else
129: return null;
130: }
131:
132: public function HasImmediateArrayExpansions() {
133: $intCount = count($this->objManyToManyReferenceArray);
134: foreach ($this->objReverseReferenceArray as $objReverseReference) {
135: if (!$objReverseReference->Unique) {
136: $intCount++;
137: }
138: }
139: return $intCount > 0;
140: }
141:
142: public function HasExtendedArrayExpansions(QDatabaseCodeGen $objCodeGen, $objCheckedTableArray = array()) {
143: $objCheckedTableArray[] = $this;
144: foreach ($this->ColumnArray as $objColumn) {
145: if (($objReference = $objColumn->Reference) && !$objReference->IsType) {
146: if ($objTable2 = $objCodeGen->GetTable($objReference->Table)) {
147: if ($objTable2->HasImmediateArrayExpansions()) {
148: return true;
149: }
150: if (!in_array($objTable2, $objCheckedTableArray) && // watch out for circular references
151: $objTable2->HasExtendedArrayExpansions($objCodeGen, $objCheckedTableArray)) {
152: return true;
153: }
154: }
155: }
156: }
157: return false;
158: }
159:
160:
161:
162:
163: ////////////////////
164: // Public Overriders
165: ////////////////////
166:
167: /**
168: * Override method to perform a property "Get"
169: * This will get the value of $strName
170: *
171: * @param string $strName Name of the property to get
172: * @throws Exception
173: * @throws QCallerException
174: * @return mixed
175: */
176: public function __get($strName) {
177: switch ($strName) {
178: case 'OwnerDbIndex':
179: return $this->intOwnerDbIndex;
180: case 'Name':
181: return $this->strName;
182: case 'ClassNamePlural':
183: return $this->strClassNamePlural;
184: case 'ClassName':
185: return $this->strClassName;
186: case 'ColumnArray':
187: return (array) $this->objColumnArray;
188: case 'PrimaryKeyColumnArray':
189: if ($this->objColumnArray) {
190: $objToReturn = array();
191: foreach ($this->objColumnArray as $objColumn)
192: if ($objColumn->PrimaryKey)
193: array_push($objToReturn, $objColumn);
194: return $objToReturn;
195: } else
196: return null;
197: case 'ReverseReferenceArray':
198: return (array) $this->objReverseReferenceArray;
199: case 'ManyToManyReferenceArray':
200: return (array) $this->objManyToManyReferenceArray;
201: case 'IndexArray':
202: return (array) $this->objIndexArray;
203: case 'ReferenceCount':
204: $intCount = count($this->objManyToManyReferenceArray);
205: foreach ($this->objColumnArray as $objColumn)
206: if ($objColumn->Reference)
207: $intCount++;
208: return $intCount;
209:
210: case 'Options':
211: return $this->options;
212:
213: default:
214: try {
215: return parent::__get($strName);
216: } catch (QCallerException $objExc) {
217: $objExc->IncrementOffset();
218: throw $objExc;
219: }
220: }
221: }
222:
223: /**
224: * Override method to perform a property "Set"
225: * This will set the property $strName to be $mixValue
226: *
227: * @param string $strName Name of the property to set
228: * @param string $mixValue New value of the property
229: * @throws Exception
230: * @throws QCallerException
231: * @return mixed
232: */
233: public function __set($strName, $mixValue) {
234: try {
235: switch ($strName) {
236: case 'OwnerDbIndex':
237: return $this->intOwnerDbIndex = QType::Cast($mixValue, QType::Integer);
238: case 'Name':
239: return $this->strName = QType::Cast($mixValue, QType::String);
240: case 'ClassName':
241: return $this->strClassName = QType::Cast($mixValue, QType::String);
242: case 'ClassNamePlural':
243: return $this->strClassNamePlural = QType::Cast($mixValue, QType::String);
244: case 'ColumnArray':
245: return $this->objColumnArray = QType::Cast($mixValue, QType::ArrayType);
246: case 'ReverseReferenceArray':
247: return $this->objReverseReferenceArray = QType::Cast($mixValue, QType::ArrayType);
248: case 'ManyToManyReferenceArray':
249: return $this->objManyToManyReferenceArray = QType::Cast($mixValue, QType::ArrayType);
250: case 'IndexArray':
251: return $this->objIndexArray = QType::Cast($mixValue, QType::ArrayType);
252: case 'Options':
253: return $this->options = QType::Cast($mixValue, QType::ArrayType);
254: default:
255: return parent::__set($strName, $mixValue);
256: }
257: } catch (QCallerException $objExc) {
258: $objExc->IncrementOffset();
259: throw $objExc;
260: }
261: }
262: }
263: