Skip to content

Commit 76a66f7

Browse files
authored
[Activities] Restore activity meta-data storage (#170)
* restore activity meta-data storage * restore activity meta-data storage
1 parent 5af5518 commit 76a66f7

File tree

2 files changed

+88
-29
lines changed

2 files changed

+88
-29
lines changed

src/ActivityStore/MariaDb.php

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929

3030
class MariaDb extends SqlActivityStore implements ActivityStoreInterface
3131
{
32-
const ACTIVITIES_TABLE = 'plugin_cmf_activities';
33-
const ACTIVITIES_METADATA_TABLE = 'plugin_cmf_activities_metadata';
34-
const DELETIONS_TABLE = 'plugin_cmf_deletions';
3532

3633
use LoggerAware;
3734

@@ -145,19 +142,49 @@ protected function saveActivityStoreEntry(ActivityStoreEntryInterface $entry, Ac
145142
$data['md5'] = $db->quote(md5(serialize($md5Data)));
146143
$data['modificationDate'] = $time;
147144

148-
if ($entry->getId()) {
149-
\CustomerManagementFrameworkBundle\Service\MariaDb::getInstance()->update(
150-
self::ACTIVITIES_TABLE,
151-
$data,
152-
'id = '.$entry->getId()
153-
);
154-
} else {
155-
$data['creationDate'] = $time;
156-
$id = \CustomerManagementFrameworkBundle\Service\MariaDb::getInstance()->insert(
157-
self::ACTIVITIES_TABLE,
158-
$data
159-
);
160-
$entry->setId($id);
145+
$db->beginTransaction();
146+
147+
try {
148+
149+
if ($entry->getId()) {
150+
\CustomerManagementFrameworkBundle\Service\MariaDb::getInstance()->update(
151+
self::ACTIVITIES_TABLE,
152+
$data,
153+
'id = '.$entry->getId()
154+
);
155+
} else {
156+
$data['creationDate'] = $time;
157+
$id = \CustomerManagementFrameworkBundle\Service\MariaDb::getInstance()->insert(
158+
self::ACTIVITIES_TABLE,
159+
$data
160+
);
161+
$entry->setId($id);
162+
}
163+
164+
try {
165+
$db->query('delete from ' . self::ACTIVITIES_METADATA_TABLE . ' where activityId = ' . intval($entry->getId()));
166+
167+
foreach($entry->getMetadata() as $key => $data) {
168+
169+
$db->insert(
170+
self::ACTIVITIES_METADATA_TABLE,
171+
[
172+
'activityId' => $entry->getId(),
173+
'key' => $key,
174+
'data' => $data
175+
]
176+
);
177+
}
178+
} catch(TableNotFoundException $ex) {
179+
$this->getLogger()->error(sprintf('table %s not found - please press the update button of the CMF bundle in the extension manager', self::ACTIVITIES_METADATA_TABLE));
180+
$db->rollBack();
181+
}
182+
183+
$db->commit();
184+
185+
} catch(\Exception $e) {
186+
$this->getLogger()->error(sprintf('save activity (%s) failed: %s', $entry->getId(), $e->getMessage()));
187+
$db->rollBack();
161188
}
162189
}
163190

src/ActivityStore/SqlActivityStore.php

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
use CustomerManagementFrameworkBundle\Model\ActivityStoreEntry\ActivityStoreEntryInterface;
99
use CustomerManagementFrameworkBundle\Model\CustomerInterface;
1010
use Doctrine\DBAL\Connection;
11+
use Doctrine\DBAL\Exception\TableNotFoundException;
1112
use Pimcore\Db;
1213
use Pimcore\Model\DataObject\Concrete;
1314
use Zend\Paginator\Paginator;
1415

1516
abstract class SqlActivityStore
1617
{
1718
const ACTIVITIES_TABLE = 'plugin_cmf_activities';
19+
const ACTIVITIES_METADATA_TABLE = 'plugin_cmf_activities_metadata';
1820
const DELETIONS_TABLE = 'plugin_cmf_deletions';
1921

2022
public function insertActivityIntoStore(ActivityInterface $activity)
@@ -78,19 +80,49 @@ protected function saveActivityStoreEntry(ActivityStoreEntryInterface $entry, Ac
7880
$data['md5'] = md5(serialize($md5Data));
7981
$data['modificationDate'] = $time;
8082

81-
if ($entry->getId()) {
82-
$this->getActivityStoreConnection()->update(
83-
self::ACTIVITIES_TABLE,
84-
$data,
85-
'id = '.$entry->getId()
86-
);
87-
} else {
88-
$data['creationDate'] = $time;
89-
$db->insert(
90-
self::ACTIVITIES_TABLE,
91-
$data
92-
);
93-
$entry->setId($db->lastInsertId());
83+
$db->beginTransaction();
84+
85+
try {
86+
87+
if ($entry->getId()) {
88+
$this->getActivityStoreConnection()->update(
89+
self::ACTIVITIES_TABLE,
90+
$data,
91+
'id = '.$entry->getId()
92+
);
93+
} else {
94+
$data['creationDate'] = $time;
95+
$db->insert(
96+
self::ACTIVITIES_TABLE,
97+
$data
98+
);
99+
$entry->setId($db->lastInsertId());
100+
}
101+
102+
try {
103+
$db->query('delete from ' . self::ACTIVITIES_METADATA_TABLE . ' where activityId = ' . intval($entry->getId()));
104+
105+
foreach($entry->getMetadata() as $key => $data) {
106+
107+
$db->insert(
108+
self::ACTIVITIES_METADATA_TABLE,
109+
[
110+
'activityId' => $entry->getId(),
111+
'key' => $key,
112+
'data' => $data
113+
]
114+
);
115+
}
116+
} catch(TableNotFoundException $ex) {
117+
$this->getLogger()->error(sprintf('table %s not found - please press the update button of the CMF bundle in the extension manager', self::ACTIVITIES_METADATA_TABLE));
118+
$db->rollBack();
119+
}
120+
121+
$db->commit();
122+
123+
} catch(\Exception $e) {
124+
$this->getLogger()->error(sprintf('save activity (%s) failed: %s', $entry->getId(), $e->getMessage()));
125+
$db->rollBack();
94126
}
95127
}
96128

0 commit comments

Comments
 (0)