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 ?
1 Answer 1
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.
- 
 Thanks, I guess there is a bit more work I have to not overwrite core function but to extend saveHandler using ExtensionPool? Like in this post magento.stackexchange.com/questions/146994/…Juliano Vargas– Juliano Vargas2021年03月16日 16:09:27 +00:00Commented Mar 16, 2021 at 16:09
- 
 Yes you can do it via saveHandler.Rahul Barot– Rahul Barot2021年03月17日 09:26:22 +00:00Commented Mar 17, 2021 at 9:26
Explore related questions
See similar questions with these tags.
cms_page_storeI have added unique key between them, But how can I do that adding tocms_pagetable? When I runsetup:db-declaration:generate-whitelistgives errorTable cms_page do not have column with name store_id.