Exemple #1
0
	/**
	 * Executes the query to load the rows and returns them
	 *
	 * @param  SimpleXmlElement  $dataModel
	 * @param  int $limitstart
	 * @param  int $limit
	 * @return array of stdClass objects with ->_tbl set properly.
	 */
	public function & queryLoadObjectsList( $dataModel, $limitstart = 0, $limit = 0 ) {
		$sql								=	$this->_buildSQLquery();
		if ( $sql === null ) {
			return $sql;
		}
		$this->_db->setQuery( $sql, ( $limitstart ? (int) $limitstart : 0 ), ( $limit ? (int) $limit : 0 ) );

		$dataModelClass						=	$dataModel->attributes( 'class' );
		$dataModelKey						=	$dataModel->attributes( 'key' );
		$dataModelUseLoad					=	( $dataModel->attributes( 'useload' ) == 'true' );
		if ( ( ! $dataModelClass ) || ( $dataModelClass == 'stdClass' ) ) {
			$rows							=	$this->_db->loadObjectList();

			if ( $this->_db->getErrorNum() ) {
				trigger_error( 'SQLXML::queryObjectList: error: ' . $this->_db->getErrorMsg(), E_USER_NOTICE );
			} else {
				for ( $i = 0, $n = count( $rows ); $i < $n; $i++ ) {
					if ( ! ( $rows[$i] instanceof TableInterface ) ) {
						$rows[$i]->_tbl	=	$this->_table;
					}
				}
			}
		} else {
			$rowsArray						=	$this->_db->loadAssocList();

			if ( $this->_db->getErrorNum() ) {
				trigger_error( 'SQLXML::queryObjectList: error: ' . $this->_db->getErrorMsg(), E_USER_NOTICE );
			}

			if ( $rowsArray === null ) {
				$rows						=	null;
			} else {
				if ( strpos( $dataModelClass, '::' ) === false ) {
					$rows					=	array();
					foreach ( $rowsArray as $k => $rarr ) {
						$rows[$k]			=	new $dataModelClass( $this->_db );

						if ( $dataModelUseLoad && $dataModelKey && isset( $rarr[$dataModelKey] ) ) {
							if ( $rows[$k] instanceof TableInterface ) {
								/** @var TableInterface[] $rows */
								if ( $rows[$k]->getKeyName() == $dataModelKey ) {
									$rows[$k]->load( $rarr[$dataModelKey] );
								}
							}
						}

						foreach ( $rarr as $kk => $vv ) {
							$rows[$k]->$kk	=	$vv;
						}
					}
				} else {
					$dataModelSingleton		=	explode( '::', $dataModelClass );
					$rows					=	call_user_func_array( $dataModelSingleton, array( &$rowsArray ) );
				}
				unset( $rowsArray );
			}
		}
		return $rows;
	}
Exemple #2
0
 /**
  * Loads an array of typed objects of a given class (same class as current object by default)
  * which inherit from this class.
  *
  * @param  string  $class          [optional] class name
  * @param  string  $key            [optional] key name in db to use as key of array
  * @param  array   $additionalVars [optional] array of string additional key names to add as vars to object
  * @return static[]|array          Array of objects of the same class (empty array if no objects)
  */
 public function loadTrueObjects($class = null, $key = "", $additionalVars = array())
 {
     $objectsArray = array();
     $resultsArray = $this->_db->loadAssocList($key);
     if (is_array($resultsArray)) {
         if ($class == null) {
             $class = get_class($this);
         }
         foreach ($resultsArray as $k => $value) {
             $objectsArray[$k] = new $class($this->_db);
             // mosBindArrayToObject( $value, $objectsArray[$k], null, null, false );
             /** @var self[] $objectsArray */
             $objectsArray[$k]->bind($value, null, null, false);
             foreach ($additionalVars as $index) {
                 if (array_key_exists($index, $value)) {
                     $objectsArray[$k]->{$index} = $value[$index];
                 }
             }
         }
     }
     return $objectsArray;
 }