Skip to content

Commit

Permalink
New method MetaModel::getAttributeByNames().
Browse files Browse the repository at this point in the history
  • Loading branch information
discordier committed Nov 7, 2014
1 parent 0a935f3 commit d6ac612
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/MetaModels/MetaModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,15 @@ protected function fetchTranslatedAttributeValues($ids)
*/
protected function fetchAdditionalAttributes($ids, $result, $attrOnly = array())
{
foreach (array_merge($this->getComplexAttributes(), $this->getTranslatedAttributes()) as $attribute) {
$attributes = array_intersect(
$this->getAttributeByNames($attrOnly),
array_merge($this->getComplexAttributes(), $this->getTranslatedAttributes())
);

foreach ($attributes as $attribute) {
/** @var IAttribute $attribute */
$attributeName = $attribute->getColName();

if (!in_array($attributeName, $attrOnly)) {
continue;
}

// If it is translated, fetch the translated data now.
if ($this->isTranslatedAttribute($attribute)) {
$attributeData = $this->fetchTranslatedAttributeValues($ids);
Expand Down Expand Up @@ -582,6 +583,27 @@ public function getAttributeById($intId)
return null;
}

/**
* Retrieve all attributes with the given names.
*
* @param string[] $attrNames The attribute names, if empty all attributes will be returned.
*
* @return IAttribute[]
*/
protected function getAttributeByNames($attrNames = array())
{
if (empty($attrNames)) {
return $this->arrAttributes;
}

$result = array();
foreach ($attrNames as $attributeName) {
$result[$attributeName] = $this->arrAttributes[$attributeName];
}

return $result;
}

/**
* {@inheritdoc}
*/
Expand Down

1 comment on commit d6ac612

@seaneble
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usage of array_intersect here leads to recoverable errors when viewing backend lists:

Recoverable error: Object of class MetaModels\Attribute\TranslatedText\TranslatedText could not be converted to string in composer/vendor/metamodels/core/src/MetaModels/MetaModel.php on line 381
#0 [internal function]: __error(4096, 'Object of class...', '/data/var/www/v...', 381, Array)
#1 composer/vendor/metamodels/core/src/MetaModels/MetaModel.php(381): array_intersect(Array, Array)
#2 composer/vendor/metamodels/core/src/MetaModels/MetaModel.php(446): MetaModels\MetaModel->fetchAdditionalAttributes(Array, Array, Array)
#3 composer/vendor/metamodels/core/src/MetaModels/MetaModel.php(672): MetaModels\MetaModel->getItemsWithId(Array, Array)
#4 composer/vendor/metamodels/core/src/MetaModels/DcGeneral/Data/Driver.php(633): MetaModels\MetaModel->findByFilter(Object(MetaModels\Filter\Filter), 'sorting', 0, 30, 'ASC', Array)
#5 composer/vendor/metamodels/core/src/MetaModels/DcGeneral/Data/Driver.php(654): MetaModels\DcGeneral\Data\Driver->getItemsFromFilter(Object(MetaModels\Filter\Filter), Object(ContaoCommunityAlliance\DcGeneral\Data\DefaultConfig))
#6 composer/vendor/contao-community-alliance/dc-general/src/ContaoCommunityAlliance/DcGeneral/Contao/View/Contao2BackendView/ListView.php(62): MetaModels\DcGeneral\Data\Driver->fetchAll(Object(ContaoCommunityAlliance\DcGeneral\Data\DefaultConfig))
#7 composer/vendor/contao-community-alliance/dc-general/src/ContaoCommunityAlliance/DcGeneral/Contao/View/Contao2BackendView/ListView.php(302): ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\ListView->loadCollection()
#8 [internal function]: ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\ListView->showAll()
#9 composer/vendor/contao-community-alliance/dc-general/src/ContaoCommunityAlliance/DcGeneral/Contao/View/Contao2BackendView/BaseView.php(155): call_user_func_array(Array, Array)
#10 [internal function]: ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\BaseView->handleAction(Object(ContaoCommunityAlliance\DcGeneral\Event\ActionEvent), 'dc-general.acti...', Object(Symfony\Component\EventDispatcher\EventDispatcher))
#11 composer/vendor/symfony/event-dispatcher/Symfony/Component/EventDispatcher/EventDispatcher.php(164): call_user_func(Array, Object(ContaoCommunityAlliance\DcGeneral\Event\ActionEvent), 'dc-general.acti...', Object(Symfony\Component\EventDispatcher\EventDispatcher))
#12 composer/vendor/symfony/event-dispatcher/Symfony/Component/EventDispatcher/EventDispatcher.php(53): Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(Array, 'dc-general.acti...', Object(ContaoCommunityAlliance\DcGeneral\Event\ActionEvent))
#13 composer/vendor/contao-community-alliance/dc-general/src/ContaoCommunityAlliance/DcGeneral/Event/EventPropagator.php(119): Symfony\Component\EventDispatcher\EventDispatcher->dispatch('dc-general.acti...', Object(ContaoCommunityAlliance\DcGeneral\Event\ActionEvent))
#14 composer/vendor/contao-community-alliance/dc-general/src/ContaoCommunityAlliance/DcGeneral/Controller/DefaultController.php(129): ContaoCommunityAlliance\DcGeneral\Event\EventPropagator->dispatch('dc-general.acti...', Object(ContaoCommunityAlliance\DcGeneral\Event\ActionEvent))
#15 composer/vendor/metamodels/core/src/MetaModels/BackendIntegration/Module.php(238): ContaoCommunityAlliance\DcGeneral\Controller\DefaultController->handle(Object(ContaoCommunityAlliance\DcGeneral\Action))
#16 composer/vendor/metamodels/core/src/MetaModels/BackendIntegration/Module.php(263): MetaModels\BackendIntegration\Module->runDC()
#17 composer/vendor/metamodels/core/src/MetaModels/BackendIntegration/Module.php(283): MetaModels\BackendIntegration\Module->performNormal()
#18 system/modules/core/classes/Backend.php(415): MetaModels\BackendIntegration\Module->generate()
#19 system/modules/core/controllers/BackendMain.php(135): Contao\Backend->getBackendModule('metamodel_mm_vt...')
#20 contao/main.php(22): Contao\BackendMain->run()
#21 {main}

The PHP function uses a comparison on string basis, so either each of the objects should provide a __toString() method or a more custom function has to be used instead of intersect.

Please sign in to comment.