Completed
Push — master ( b9922f...d9a893 )
by Vitaly
04:21
created

Material::copy()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 42
Code Lines 22

Duplication

Lines 16
Ratio 38.1 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 16
loc 42
rs 5.3846
cc 8
eloc 22
nc 8
nop 2
1
<?php
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>
4
 * on 07.08.14 at 17:11
5
 */
6
namespace samsoncms\api;
7
8
use \samsonframework\orm\Condition;
9
use \samsonframework\orm\QueryInterface;
10
11
/**
12
 * SamsonCMS Material database record object.
13
 * This class extends default ActiveRecord material table record functionality.
14
 * @package samson\cms
15
 * @author Vitaly Egorov <[email protected]>
16
 */
17
class Material extends \samson\activerecord\material
18
{
19
    /** Override table attributes for late static binding */
20
    public static $_attributes = array();
21
    public static $_sql_select = array();
22
    public static $_sql_from = array();
23
    public static $_own_group = array();
24
    public static $_map = array();
25
26
    /**
27
     * Get current entity instances collection by navigation identifier.
28
     *
29
     * @param QueryInterface $query Database query
30
     * @param string $navigationID Navigation identifier
31
     * @param self[]|array|null $return Variable where request result would be returned
32
     * @return bool|self[] True if material entities has been found and $return is passed
33
     *                      or self[] if only two parameters is passed.
34
     */
35
    public static function byNavigationID(QueryInterface $query, $navigationID, &$return = array())
36
    {
37
        /** @var \samson\activerecord\structurematerial[] $materials Navigation to material relation */
38
        $materials = array();
39
        if ($query->entity('\samson\activerecord\structurematerial')
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface samsonframework\orm\QueryInterface as the method cond() does only exist in the following implementations of said interface: samson\activerecord\dbQuery.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
40
            ->cond('StructureID', $navigationID)
41
            ->fields('MaterialID', $materials)
42
        ) {
43
            $return = $query->entity(get_called_class())
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface samsonframework\orm\QueryInterface as the method cond() does only exist in the following implementations of said interface: samson\activerecord\dbQuery.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
44
                ->cond('MaterialID', $materials)
45
                ->cond('Active', 1)
46
                ->cond('Published', 1)
47
                ->exec();
48
        }
49
50
        // If only one argument is passed - return null, otherwise bool
51
        return func_num_args() > 2 ? $return == null : $return;
52
    }
53
54
    /**
55
     * Get material entities collection by URL(s).
56
     * @param QueryInterface $query Object for performing database queries
57
     * @param array|string $url Material URL or collection of material URLs
58
     * @param self[]|array|null $return Variable where request result would be returned
59
     * @return bool|self[] True if material entities has been found
60
     */
61
    public static function byUrl(QueryInterface $query, $url, & $return = array())
62
    {
63
        // Get field record by identifier column
64
        $return = static::collectionByColumn($query, 'Url', $url);
0 ignored issues
show
Bug introduced by
It seems like $url defined by parameter $url on line 61 can also be of type array; however, samsonframework\orm\Record::collectionByColumn() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
65
66
        // If only one argument is passed - return null, otherwise bool
67
        return func_num_args() > 1 ? $return == null : $return;
68
    }
69
70
    /**
71
     * Set additional material field value by field identifier
72
     * @param string $fieldID Field identifier
73
     * @param string $value Value to be stored
74
     * @param string $locale Locale identifier
75
     */
76
    public function setFieldByID($fieldID, $value, $locale = DEFAULT_LOCALE)
77
    {
78
        // TODO: This should be removed
79
        /** @var QueryInterface $query This should be removed to use $this->database*/
80
        $query = dbQuery();
0 ignored issues
show
Bug introduced by
The call to dbQuery() misses a required argument $class_name.

This check looks for function calls that miss required arguments.

Loading history...
81
82
        /** @var Field $fieldRecord Try to find this additional field */
83
        $fieldRecord = null;
84
        if (Field::byID($query, $fieldID, $fieldRecord)) {
0 ignored issues
show
Documentation introduced by
$fieldRecord is of type object<samsoncms\api\Field>, but the function expects a null|object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85
            /** @var MaterialField $materialFieldRecord Try to find additional field value */
86
            $materialFieldRecord = null;
87
            if (!MaterialField::byFieldIDAndMaterialID($query, $this->id, $fieldRecord->id, $materialFieldRecord)) {
88
                // Create new additional field value record if it does not exists
89
                $materialFieldRecord = new MaterialField();
90
                $materialFieldRecord->FieldID = $fieldRecord->id;
91
                $materialFieldRecord->MaterialID = $this->id;
92
                $materialFieldRecord->Active = 1;
93
                $materialFieldRecord->locale = $locale;
94
            }
95
96
            // Define which field should be filled
97
            switch ($fieldRecord->Type) {
98
                case 1:
99
                    $valueFieldName = 'numeric_value';
100
                    break;
101
                case 2:
102
                    $valueFieldName = 'key_value';
103
                    break;
104
                default:
105
                    $valueFieldName = 'Value';
106
            }
107
108
            // At this point we already have database record instance
109
            $fieldRecord->$valueFieldName = $value;
110
            $fieldRecord->save();
111
        }
112
    }
113
114
    /**
115
     * Get select additional field text value
116
     * TODO: Find where do we use it
117
     * @return string Select field text
118
     */
119
    public function selectText($fieldID)
120
    {
121
        /** @var \samson\activerecord\field $field */
122
        $field = null;
123
        if (dbQuery('field')->id($fieldID)->first($field)) {
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::id() has been deprecated with message: Use direct query with where('PRIMARY_FIELD',...)

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
124
            // If this entity has this field set
125
            if (isset($this[$field->Name]{0})) {
126
                $types = array();
127
                foreach (explode(',', $field->Value) as $typeValue) {
128
                    $typeValue = explode(':', $typeValue);
129
                    $types[$typeValue[0]] = $typeValue[1];
130
                }
131
                return $types[$this[$field->Name]];
132
            }
133
        }
134
135
        // Value not set
136
        return '';
137
    }
138
139
    /**
140
     * Get collection of images for material by gallery additional field selector. If none is passed
141
     * all images from gallery table would be returned for this material entity.
142
     *
143
     * @param string|null $fieldSelector Additional field selector value
144
     * @param string $selector Additional field field name to search for
145
     * @return \samson\activerecord\gallery[] Collection of images in this gallery additional field for material
146
     */
147
    public function &gallery($fieldSelector = null, $selector = 'FieldID')
148
    {
149
        /** @var \samson\activerecord\gallery[] $images Get material images for this gallery */
150
        $images = array();
151
152
        /* @var \samson\activerecord\field Get field object if we need to search it by other fields */
153
        $field = null;
0 ignored issues
show
Unused Code introduced by
$field is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
154
        if ($selector != 'FieldID') {
155
            $field = dbQuery('field')->cond($selector, $fieldSelector)->first();
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::cond() has been deprecated with message: @see self::where()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
156
            $fieldSelector = $field->id;
157
        }
158
159
        // Create query
160
        $query = dbQuery('materialfield');
161
162
        // Add field filter if present
163
        if (isset($fieldSelector)) {
164
            $query->cond("FieldID", $fieldSelector);
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::cond() has been deprecated with message: @see self::where()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
165
        }
166
167
        /** @var \samson\activerecord\materialfield $dbMaterialField Find material field gallery record */
168
        $dbMaterialField = null;
169
        if ($query->cond('MaterialID', $this->id)->first($dbMaterialField)) {
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::cond() has been deprecated with message: @see self::where()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
170
            // Get material images for this materialfield
171
            if (dbQuery('gallery')->cond('materialFieldId', $dbMaterialField->id)->exec($images)) {
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::cond() has been deprecated with message: @see self::where()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
172
173
            }
174
        }
175
176
        return $images;
177
    }
178
179
    /**
180
     * Create copy of current object
181
     * @param mixed $clone Material for cloning
182
     * @param array $excludedFields excluded from materialfield fields identifiers
183
     * @returns void
184
     */
185
    public function &copy(& $clone = null, $excludedFields = array())
186
    {
187
        // Create new instance by copying
188
        $clone = parent::copy($clone);
189
190
        /** @var \samson\activerecord\structurematerial[] $objects Create structure material relations */
191
        $objects = array();
192 View Code Duplication
        if (dbQuery('structurematerial')->cond('MaterialID', $this->MaterialID)->exec($objects)) {
0 ignored issues
show
Bug introduced by
The property MaterialID does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Deprecated Code introduced by
The method samson\activerecord\dbQuery::cond() has been deprecated with message: @see self::where()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
193
            foreach ($objects as $cmsNavigation) {
194
                /** @var \samson\activerecord\Record $copy */
195
                $copy = $cmsNavigation->copy();
196
                $copy->MaterialID = $clone->id;
197
                $copy->save();
198
            }
199
        }
200
        /** @var \samson\activerecord\materialfield[] $objects Create material field relations */
201
        $objects = array();
202
        if (dbQuery('materialfield')->cond('MaterialID', $this->MaterialID)->exec($objects)) {
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::cond() has been deprecated with message: @see self::where()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
203
            foreach ($objects as $pMaterialField) {
204
                // Check if field is NOT excluded from copying
205
                if (!in_array($pMaterialField->FieldID, $excludedFields)) {
206
                    /** @var \samson\activerecord\dbRecord $copy Copy instance */
207
                    $copy = $pMaterialField->copy();
208
                    $copy->MaterialID = $clone->id;
0 ignored issues
show
Bug introduced by
The property MaterialID does not seem to exist in samson\activerecord\dbRecord.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
209
                    $copy->save();
210
                }
211
            }
212
        }
213
214
        /** @var \samson\activerecord\gallery[] $objects Create gallery field relations */
215
        $objects = array();
216 View Code Duplication
        if (dbQuery('gallery')->cond('MaterialID', $this->MaterialID)->exec($objects)) {
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::cond() has been deprecated with message: @see self::where()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
217
            foreach ($objects as $cmsGallery) {
218
                /** @var \samson\activerecord\Record $copy */
219
                $copy = $cmsGallery->copy();
220
                $copy->MaterialID = $clone->id;
221
                $copy->save();
222
            }
223
        }
224
225
        return $clone;
226
    }
227
228
    /**
229
     * Function to retrieve this material table by specified field
230
     * @param string $tableSelector Selector to identify table structure
231
     * @param string $selector Database field by which search is performed
232
     * @param array $tableColumns Columns names list
233
     * @param string $externalHandler External handler to perform some extra code
234
     * @param array $params External handler params
235
     * @return array Collection of collections of table cells, represented as materialfield objects
236
     */
237
    public function getTable($tableSelector, $selector = 'StructureID', &$tableColumns = null, $externalHandler = null, $params = array())
238
    {
239
        /** @var array $resultTable Collection of collections of field cells */
240
        $resultTable = array();
241
        /** @var array $dbTableFieldsIds Array of table structure column identifiers */
242
        $dbTableFieldsIds = array();
243
244
        // Get structure object if we need to search it by other fields
245
        if ($selector != 'StructureID') {
246
            $structure = dbQuery('structure')->cond($selector, $tableSelector)->first();
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::cond() has been deprecated with message: @see self::where()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
247
            $tableSelector = $structure->id;
248
        }
249
250
        /** If this table has columns */
251
        if (dbQuery('structurefield')
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::cond() has been deprecated with message: @see self::where()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
252
            ->cond("StructureID", $tableSelector)
253
            ->fields('FieldID', $dbTableFieldsIds)
0 ignored issues
show
Documentation introduced by
$dbTableFieldsIds is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
254
        ) {
255
            // Get localized and not localized fields
256
            $localizedFields = array();
257
            $unlocalizedFields = array();
258
            /** @var \samson\cms\CMSField $dbTableField Table column */
259
            foreach (dbQuery('field')->order_by('priority')->cond('FieldID', $dbTableFieldsIds)->exec() as $field) {
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::cond() has been deprecated with message: @see self::where()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
260
                /** Add table columns names */
261
                $tableColumns[] = $field->Name;
262
                if ($field->local == 1) {
263
                    $localizedFields[] = $field->id;
264
                } else {
265
                    $unlocalizedFields[] = $field->id;
266
                }
267
            }
268
269
            // Query to get table rows(table materials)
270
            $tableQuery = dbQuery('material')
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::cond() has been deprecated with message: @see self::where()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
271
                ->cond('parent_id', $this->MaterialID)
272
                ->cond('Active', '1')
273
                ->join('structurematerial')
274
                ->cond('structurematerial_StructureID', $tableSelector)
275
                ->order_by('priority');
276
277
            // Call user function if exists
278
            if (is_callable($externalHandler)) {
279
                // Give it query as parameter
280
                call_user_func_array($externalHandler, array_merge(array(&$tableQuery), $params));
281
            }
282
283
            // Get table row materials
284
            $tableMaterialIds = array();
285
            if ($tableQuery->fields('MaterialID', $tableMaterialIds)) {
286
                // Create field condition
287
                $localizationFieldCond = new Condition('or');
288
289
                // Create localized condition
290
                if (sizeof($localizedFields)) {
291
                    $localizedFieldCond = new Condition('and');
292
                    $localizedFieldCond->add('materialfield_FieldID', $localizedFields)
293
                        ->add('materialfield_locale', locale());
294
                    // Add this condition to condition group
295
                    $localizationFieldCond->add($localizedFieldCond);
0 ignored issues
show
Bug introduced by
The call to add() misses a required argument $value.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
$localizedFieldCond is of type object<samsonframework\orm\Condition>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
296
                }
297
298
                // Create not localized condition
299
                if (sizeof($unlocalizedFields)) {
300
                    $localizationFieldCond->add('materialfield_FieldID', $unlocalizedFields);
301
                }
302
303
                // Create db query
304
                $materialFieldQuery = dbQuery('materialfield')
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::cond() has been deprecated with message: @see self::where()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
305
                    ->cond('MaterialID', $tableMaterialIds)
0 ignored issues
show
Documentation introduced by
$tableMaterialIds is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
306
                    ->cond($localizationFieldCond);
0 ignored issues
show
Documentation introduced by
$localizationFieldCond is of type object<samsonframework\orm\Condition>, but the function expects a string|object<samson\act...cord\ArgumentInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
307
308
                // Flip field identifiers as keys
309
                $tableColumnIds = array_flip($dbTableFieldsIds);
310
                $resultTable = array_flip($tableMaterialIds);
311
312
                /** @var \samson\activerecord\material $dbTableRow Material object (table row) */
313
                foreach ($materialFieldQuery->exec() as $mf) {
314
                    if (!is_array($resultTable[$mf['MaterialID']])) {
315
                        $resultTable[$mf['MaterialID']] = array();
316
                    }
317
318
                    $resultTable[$mf['MaterialID']][$tableColumnIds[$mf->FieldID]] =
319
                        !empty($mf->Value) ? $mf->Value : (!empty($mf->numeric_value) ? $mf->numeric_value : $mf->key_value);
320
                }
321
            }
322
        }
323
324
        return array_values($resultTable);
325
    }
326
}
327