Skip to content

Commit c4bd747

Browse files
committed
feat: use EntityManager APIs for getting/saving page data.
1 parent 76ac52b commit c4bd747

File tree

10 files changed

+236
-476
lines changed

10 files changed

+236
-476
lines changed

Model/PageAi.php

Lines changed: 0 additions & 121 deletions
This file was deleted.

Model/PageAiRepository.php

Lines changed: 0 additions & 106 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright © Graycore, LLC. All rights reserved.
4+
* See LICENSE.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Graycore\CmsAiBuilder\Model\ResourceModel\Page\Relation\Ai;
9+
10+
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Framework\EntityManager\Operation\ExtensionInterface;
12+
13+
/**
14+
* Loads AI data from cms_page_ai table when a CMS page is loaded
15+
*/
16+
class ReadHandler implements ExtensionInterface
17+
{
18+
/**
19+
* @var ResourceConnection
20+
*/
21+
private ResourceConnection $resourceConnection;
22+
23+
/**
24+
* @param ResourceConnection $resourceConnection
25+
*/
26+
public function __construct(ResourceConnection $resourceConnection)
27+
{
28+
$this->resourceConnection = $resourceConnection;
29+
}
30+
31+
/**
32+
* Load AI data for a CMS page entity
33+
*
34+
* @param object $entity
35+
* @param array $arguments
36+
* @return object
37+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
38+
*/
39+
public function execute($entity, $arguments = [])
40+
{
41+
if (!$entity->getId()) {
42+
return $entity;
43+
}
44+
45+
$connection = $this->resourceConnection->getConnection();
46+
$tableName = $this->resourceConnection->getTableName('cms_page_ai');
47+
48+
$select = $connection->select()
49+
->from($tableName, ['ai_schema_json', 'ai_conversation_history'])
50+
->where('page_id = ?', (int)$entity->getId());
51+
52+
$result = $connection->fetchRow($select);
53+
54+
if ($result) {
55+
$entity->setData('ai_schema_json', $result['ai_schema_json']);
56+
$entity->setData('ai_conversation_history', $result['ai_conversation_history']);
57+
}
58+
59+
return $entity;
60+
}
61+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright © Graycore, LLC. All rights reserved.
4+
* See LICENSE.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Graycore\CmsAiBuilder\Model\ResourceModel\Page\Relation\Ai;
9+
10+
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Framework\EntityManager\Operation\ExtensionInterface;
12+
13+
/**
14+
* Saves AI data to cms_page_ai table when a CMS page is saved
15+
*/
16+
class SaveHandler implements ExtensionInterface
17+
{
18+
/**
19+
* @var ResourceConnection
20+
*/
21+
private ResourceConnection $resourceConnection;
22+
23+
/**
24+
* @param ResourceConnection $resourceConnection
25+
*/
26+
public function __construct(ResourceConnection $resourceConnection)
27+
{
28+
$this->resourceConnection = $resourceConnection;
29+
}
30+
31+
/**
32+
* Save AI data for a CMS page entity
33+
*
34+
* @param object $entity
35+
* @param array $arguments
36+
* @return object
37+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
38+
*/
39+
public function execute($entity, $arguments = [])
40+
{
41+
if (!$entity->getId()) {
42+
return $entity;
43+
}
44+
45+
$aiSchemaJson = $entity->getData('ai_schema_json');
46+
$aiConversationHistory = $entity->getData('ai_conversation_history');
47+
48+
// Only save if there's AI data to save
49+
if ($aiSchemaJson === null && $aiConversationHistory === null) {
50+
return $entity;
51+
}
52+
53+
$connection = $this->resourceConnection->getConnection();
54+
$tableName = $this->resourceConnection->getTableName('cms_page_ai');
55+
56+
$data = ['page_id' => (int)$entity->getId()];
57+
58+
if ($aiSchemaJson !== null) {
59+
$data['ai_schema_json'] = $aiSchemaJson;
60+
}
61+
62+
if ($aiConversationHistory !== null) {
63+
$data['ai_conversation_history'] = $aiConversationHistory;
64+
}
65+
66+
$connection->insertOnDuplicate($tableName, $data);
67+
68+
return $entity;
69+
}
70+
}

0 commit comments

Comments
 (0)