public function valueForKey($key) { if (array_key_exists($key, $this->attributeNames)) { if (!array_key_exists($key, $this->attributes)) { $query = "SELECT `{$key}` FROM `{$this->dbName}`.`{$this->tableName}` WHERE `{$this->primaryKeyName}` = '{$this->primaryKey}' LIMIT 1"; $result = $this->db->processQuery($query); $this->attributes[$key] = stripslashes($result[0]); } return $this->attributes[$key]; } else { return parent::valueForKey($key); } }
public function valueForKey($key) { if (array_key_exists($key, $this->attributeNames)) { if (!array_key_exists($key, $this->attributes)) { $query = "SELECT `{$key}` FROM `{$this->tableName}` WHERE `{$this->primaryKeyName}` = '{$this->primaryKey}' LIMIT 1"; $result = $this->db->processQuery($query); if (isset($result[0])) { $this->attributes[$key] = stripslashes($result[0]); } } return $this->attributes[$key]; } else { if (array_key_exists($key, $this->parentNames)) { if (!array_key_exists($key, $this->parents)) { $parentClassName = $this->parentNames[$key]; $exampleParent = new $parentClassName(); $parentPrimaryKey = $this->valueForKey($exampleParent->primaryKeyName); $this->parents[$key] = new $parentClassName(new NamedArguments(array('primaryKey' => $parentPrimaryKey))); } return $this->parents[$key]; } else { if (array_key_exists($key, $this->childNames)) { if (!array_key_exists($key, $this->children)) { $this->children[$key] = array(); $childClassName = $this->childNames[$key]; $exampleChild = new $childClassName(); $whereClause = "`{$this->primaryKeyName}`='{$this->primaryKey}'"; $query = "SELECT `{$exampleChild->primaryKeyName}` FROM `{$exampleChild->tableName}` WHERE {$whereClause}"; $results = $this->db->processQuery($query); foreach ($results as $result) { $id = $result[0]; $child = new $childClassName(new NamedArguments(array('primaryKey' => $id))); array_push($this->children[$key], $child); } } return $this->children[$key]; } else { if (array_key_exists($key, $this->peerNames)) { if (!array_key_exists($key, $this->peers)) { $this->peers[$key] = array(); $peerClassName = $this->peerNames[$key]; $examplePeer = new $peerClassName(); $whereClause = "`{$this->primaryKeyName}`='{$this->primaryKey}'"; $tableNames = array($this->tableName, $examplePeer->tableName); $sortedTableNames = sort($tableNames); $joinTableName = $sortedTableNames[0] . 'To' . $sortedTableNames[1]; $query = "SELECT `{$examplePeer->primaryKeyName}` FROM `{$joinTableName}` WHERE {$whereClause}"; $results = $this->db->processQuery($query); foreach ($results as $result) { $id = $result[0]; $peer = new $peerClassName(new NamedArguments(array('primaryKey' => $id))); array_push($this->peers[$key], $peer); } } return $this->peers[$key]; } else { return parent::valueForKey($key); } } } } }