Skip to content

Commit bbd6f64

Browse files
committed
Merge PR #23 from @stefanheimes
Fixing a bug that caused all data to be copied twice.
2 parents 253aa40 + 23430ce commit bbd6f64

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

src/EventListener/BackendEventListener.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
use Contao\System;
2727
use ContaoCommunityAlliance\DcGeneral\Event\PostDuplicateModelEvent;
2828
use ContaoCommunityAlliance\DcGeneral\Event\PostPasteModelEvent;
29+
use Doctrine\DBAL\ArrayParameterType;
2930
use Doctrine\DBAL\Connection;
3031
use MetaModels\DcGeneral\Data\Model;
32+
use MetaModels\IFactory;
3133

3234
/**
3335
* Handles event operations on tl_metamodel_dcasetting
@@ -50,13 +52,20 @@ class BackendEventListener
5052
*/
5153
private Connection $connection;
5254

55+
/**
56+
* @var IFactory
57+
*/
58+
private IFactory $factory;
59+
5360
/**
5461
* The ArticleContent constructor.
5562
*
5663
* @param Connection $connection The database connection.
5764
*/
58-
public function __construct(Connection $connection = null)
59-
{
65+
public function __construct(
66+
IFactory $factory,
67+
Connection $connection = null,
68+
) {
6069
if (null === $connection) {
6170
// @codingStandardsIgnoreStart
6271
@trigger_error(
@@ -68,6 +77,7 @@ public function __construct(Connection $connection = null)
6877
assert($connection instanceof Connection);
6978
}
7079
$this->connection = $connection;
80+
$this->factory = $factory;
7181
}
7282

7383
/**
@@ -130,14 +140,33 @@ public function handlePostPasteModel(PostPasteModelEvent $event)
130140
*/
131141
private function duplicateContentEntries(string $strTable, int $intSourceId, int $intDestinationId): void
132142
{
143+
$metaModel = $this->factory->getMetaModel($strTable);
144+
if ($metaModel === null) {
145+
return;
146+
}
147+
148+
$colNamesToCopy = [];
149+
$attributes = $metaModel->getAttributes();
150+
foreach ($attributes as $attribute) {
151+
if ($attribute->get('type') === 'contentarticle') {
152+
$colNamesToCopy[] = $attribute->getColName();
153+
}
154+
}
155+
156+
if (empty($colNamesToCopy)) {
157+
return;
158+
}
159+
133160
$objContent = $this->connection
134161
->createQueryBuilder()
135162
->select('*')
136163
->from('tl_content', 't')
137164
->where('t.pid=:id')
138165
->andWhere('t.ptable=:ptable')
166+
->andWhere('t.mm_slot IN (:slots)')
139167
->setParameter('id', $intSourceId)
140168
->setParameter('ptable', $strTable)
169+
->setParameter('slots', $colNamesToCopy, ArrayParameterType::STRING)
141170
->executeQuery();
142171

143172
while ($row = $objContent->fetchAssociative()) {

src/Resources/config/listeners.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ services:
22
MetaModels\AttributeContentArticleBundle\EventListener\BackendEventListener:
33
public: false
44
arguments:
5+
- '@metamodels.factory'
56
- '@database_connection'
67
tags:
78
- name: kernel.event_listener

0 commit comments

Comments
 (0)