Пример #1
0
 /**
  * Loads all the elements using its parent id and the parent Id value
  *
  * @param   string  $elementsTableName    Table Name
  * @param   string  $parentColumnName     Parent column name
  * @param   string  $parentId             Parent Id
  * @param   boolean $transformProperties  If the properties should be transform to CamelCase
  * @param   array   $extraWhereStatements Extra where statements
  *
  * @return array
  */
 public static function getElementsByParentId($elementsTableName, $parentColumnName, $parentId, $transformProperties = false, $extraWhereStatements = array())
 {
     $db = JFactory::getDbo();
     $query = $db->getQuery(true);
     $query->select('*')->from($elementsTableName)->where($parentColumnName . ' = ' . intval($parentId));
     if (!empty($extraWhereStatements)) {
         foreach ($extraWhereStatements as $extraWhereStatement) {
             $query->where($extraWhereStatement);
         }
     }
     $db->setQuery($query);
     $elements = $db->loadObjectList();
     if ($transformProperties) {
         for ($i = 0; $i < count($elements); $i++) {
             $data = new stdClass();
             $elementArray = get_object_vars($elements[$i]);
             foreach ($elementArray as $property => $value) {
                 $data->{NenoHelper::convertDatabaseColumnNameToPropertyName($property)} = $value;
             }
             $elements[$i] = $data;
         }
     }
     return $elements;
 }
Пример #2
0
 /**
  * Get Worker related to a task
  *
  * @return NenoTaskWorker
  */
 protected function getWorker()
 {
     // Generate Worker class name
     $className = 'NenoTaskWorker' . ucfirst(NenoHelper::convertDatabaseColumnNameToPropertyName($this->task));
     // Check if the class exists, if it doesn't, let's try to load it.
     if (class_exists($className)) {
         $worker = new $className();
         return $worker;
     } else {
         NenoLog::log('Worker not found for this task', 1);
         throw new UnexpectedValueException('Worker not found for this task');
     }
 }
Пример #3
0
 /**
  * Load element from the database
  *
  * @param   mixed   $pk            Could be the ID of the element or an array of clauses
  * @param   boolean $loadExtraData Load extra data once the object has been created
  * @param   boolean $loadParent    If the parent should be loaded
  *
  * @return stdClass|array
  */
 public static function load($pk, $loadExtraData = true, $loadParent = false)
 {
     if (!is_array($pk)) {
         $pk = array('id' => $pk);
     }
     $db = JFactory::getDbo();
     $query = $db->getQuery(true);
     $query->select(empty($pk['_select']) ? '*' : $pk['_select'])->from(self::getDbTable());
     foreach ($pk as $field => $value) {
         if (!in_array($field, array('_order', '_limit', '_offset'))) {
             if (is_array($value)) {
                 // If this field has an special condition, let's apply it
                 if (!empty($value['_field']) && !empty($value['_condition']) && !empty($value['_value'])) {
                     $query->where($db->quoteName(NenoHelper::convertPropertyNameToDatabaseColumnName($value['_field'])) . ' ' . $value['_condition'] . ' ' . $db->quote($value['_value']));
                 }
             } else {
                 $query->where($db->quoteName(NenoHelper::convertPropertyNameToDatabaseColumnName($field)) . ' = ' . $db->quote($value));
             }
         }
     }
     // If order clauses have been set, let's process them
     if (!empty($pk['_order'])) {
         foreach ($pk['_order'] as $orderField => $orderDirection) {
             $query->order($orderField . ' ' . $orderDirection);
         }
     }
     $offset = empty($pk['_offset']) ? 0 : (int) $pk['_offset'];
     $limit = empty($pk['_limit']) ? 0 : (int) $pk['_limit'];
     $db->setQuery($query, $offset, $limit);
     $objects = $db->loadAssocList();
     $objectsData = array();
     if (empty($pk['_select'])) {
         $className = get_called_class();
         if (!empty($objects)) {
             foreach ($objects as $object) {
                 $objectData = new stdClass();
                 foreach ($object as $key => $value) {
                     $objectData->{NenoHelper::convertDatabaseColumnNameToPropertyName($key)} = $value;
                 }
                 $objectsData[] = empty($pk['_select']) ? new $className($objectData, $loadExtraData, $loadParent) : $objectsData;
             }
         }
     } else {
         $objectsData = $objects;
     }
     if (count($objectsData) == 1) {
         $objectsData = array_shift($objectsData);
     }
     return $objectsData;
 }