Question

I have added a new column with declarative schema found here: https://devdocs.magento.com/guides/v2.4/extension-dev-guide/declarative-schema/db-schema.html

Is all added to the table cms_page_store and the backend form but when I save I can see the data in post but is not populating the field in cms_page_store table.

My setup:

db_schema.xml

<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="cms_page_store">
        <column xsi:type="varchar" name="ucri" nullable="true" length="50" comment="Unique Code Cmc Page Relation Identifier"/>
        <constraint xsi:type="unique" referenceId="CMS_PAGE_STORE_UCRI_STORE_ID">
            <column name="ucri"/>
            <column name="store_id"/>
        </constraint>
    </table>
</schema>

cms_page_form.xml

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="search_engine_optimisation">
        <field name="ucri" sortOrder="1">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Unique Code</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">page</item>
                    <item name="dataScope" xsi:type="string">ucri</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

db_shcema_whitelist.json

{
    "cms_page_store": {
        "column": {
            "ucri": true
        },
        "constraint": {
            "CMS_PAGE_STORE_UCRI_STORE_ID": true
        }
    }
}

Any idea ami missing something else here ?

No correct solution

OTHER TIPS

You need to implement the logic to bind two fields as unique, below class is used to store data in the cms_page_store table.

You can override it using preference or you can add your custom logic in plugin and store custom column data.

Magento\Cms\Model\ResourceModel\Page\Relation\Store\SaveHandler

In the execute function you can find code for delete and insert record as below

For delete

$delete = array_diff($oldStores, $newStores);
if ($delete) {
    $where = [
        $linkField . ' = ?' => (int)$entity->getData($linkField),
        'store_id IN (?)' => $delete,
    ];
    $connection->delete($table, $where);
}

For insert

$insert = array_diff($newStores, $oldStores);
if ($insert) {
    $data = [];
    foreach ($insert as $storeId) {
        $data[] = [
            $linkField => (int)$entity->getData($linkField),
            'store_id' => (int)$storeId
        ];
    }
    $connection->insertMultiple($table, $data);
}

In above code you can add your custom column value in $data array

$data[] = [
    $linkField => (int)$entity->getData($linkField),
    'store_id' => (int)$storeId,
    'ucri' => $entity->getData('ucri')
];

Also in order to show it in backend you need to override the collection responsible for it, otherwise value will be store in db but it will not be visible on backend form.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top