/** * (non-PHPdoc). * * @see Alpha\Model\ActiveRecordProviderInterface::loadByAttribute() */ public function loadByAttribute($attribute, $value, $ignoreClassType = false, $loadAttributes = array()) { self::$logger->debug('>>loadByAttribute(attribute=[' . $attribute . '], value=[' . $value . '], ignoreClassType=[' . $ignoreClassType . '], loadAttributes=[' . var_export($loadAttributes, true) . '])'); if (count($loadAttributes) == 0) { $attributes = $this->BO->getPersistentAttributes(); } else { $attributes = $loadAttributes; } $fields = ''; foreach ($attributes as $att) { $fields .= $att . ','; } $fields = mb_substr($fields, 0, -1); if (!$ignoreClassType && $this->BO->isTableOverloaded()) { $sqlQuery = 'SELECT ' . $fields . ' FROM ' . $this->BO->getTableName() . ' WHERE ' . $attribute . ' = ? AND classname = ? LIMIT 1;'; } else { $sqlQuery = 'SELECT ' . $fields . ' FROM ' . $this->BO->getTableName() . ' WHERE ' . $attribute . ' = ? LIMIT 1;'; } self::$logger->debug('Query=[' . $sqlQuery . ']'); $this->BO->setLastQuery($sqlQuery); $stmt = self::getConnection()->stmt_init(); $row = array(); if ($stmt->prepare($sqlQuery)) { if ($this->BO->getPropObject($attribute) instanceof Integer) { if (!$ignoreClassType && $this->BO->isTableOverloaded()) { $stmt->bind_param('is', $value, get_class($this->BO)); } else { $stmt->bind_param('i', $value); } } else { if (!$ignoreClassType && $this->BO->isTableOverloaded()) { $stmt->bind_param('ss', $value, get_class($this->BO)); } else { $stmt->bind_param('s', $value); } } $stmt->execute(); $result = $this->bindResult($stmt); if (isset($result[0])) { $row = $result[0]; } $stmt->close(); } else { self::$logger->warn('The following query caused an unexpected result [' . $sqlQuery . ']'); if (!$this->BO->checkTableExists()) { $this->BO->makeTable(); throw new RecordNotFoundException('Failed to load object by attribute [' . $attribute . '] and value [' . $value . '], table did not exist so had to create!'); } return; } if (!isset($row['OID']) || $row['OID'] < 1) { throw new RecordNotFoundException('Failed to load object by attribute [' . $attribute . '] and value [' . $value . '], not found in database.'); self::$logger->debug('<<loadByAttribute'); return; } $this->OID = $row['OID']; // get the class attributes $reflection = new ReflectionClass(get_class($this->BO)); $properties = $reflection->getProperties(); try { foreach ($properties as $propObj) { $propName = $propObj->name; if (isset($row[$propName])) { // filter transient attributes if (!in_array($propName, $this->BO->getTransientAttributes())) { $this->BO->set($propName, $row[$propName]); } elseif (!$propObj->isPrivate() && $this->BO->get($propName) != '' && $this->BO->getPropObject($propName) instanceof Relation) { $prop = $this->BO->getPropObject($propName); // handle the setting of ONE-TO-MANY relation values if ($prop->getRelationType() == 'ONE-TO-MANY') { $this->BO->set($propObj->name, $this->BO->getOID()); } } } } } catch (IllegalArguementException $e) { self::$logger->warn('Bad data stored in the table [' . $this->BO->getTableName() . '], field [' . $propObj->name . '] bad value[' . $row[$propObj->name] . '], exception [' . $e->getMessage() . ']'); } catch (PHPException $e) { // it is possible that the load failed due to the table not being up-to-date if ($this->BO->checkTableNeedsUpdate()) { $missingFields = $this->BO->findMissingFields(); $count = count($missingFields); for ($i = 0; $i < $count; ++$i) { $this->BO->addProperty($missingFields[$i]); } throw new RecordNotFoundException('Failed to load object by attribute [' . $attribute . '] and value [' . $value . '], table [' . $this->BO->getTableName() . '] was out of sync with the database so had to be updated!'); self::$logger->debug('<<loadByAttribute'); return; } } self::$logger->debug('<<loadByAttribute'); }