cancel
Showing results for 
Search instead for 
Did you mean: 

How to add custom db_field in exists magento module table

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

How to add custom db_field in exists magento module table

Hi, all.
I have very stupid question:
How to add and use new column in exist db table (for example customer_address_entity).

I added some column in db table, but can't set data using $obj->setData('custom_field',$custom_value), i need to custom field in DB, not EAV, custom addribute, extension attribute and another bull**bleep** in Service Contract.

6 REPLIES 6

Re: How to add custom db_field in exists magento module table

Re: How to add custom db_field in exists magento module table

I don't need stupid extension attribute or custom attribute or something else attribute. I need to add this field directly on db. I found solution but is very very bad.
I'm developer and i need direct access to DB field, but is magento Service Contract is too many not compatible interfaces.
For example if i use Collection to get data form model, that return all field in DB, but if i use Repository, that return random field.
Second if i need to set data to db directly for example i make many to many relationship whit extra field, i need to create custom query to add that data, but this is very big problem when i use loop to add multiple records.

I need for KISS solution for that. May be like ($object->setData('db_field',$value) to add value.
Please help.

Re: How to add custom db_field in exists magento module table

I dont have an idea without extension attribute.

For example, in my case i want to add custom field i  order table. If I want to pass through the data and needs to be stored after placing order it must needs be saved in quote. so I followed extension attribute it works fine. In the observer file I get the quote value by 

public function beforeSaveAddressInformation(
\Magento\Checkout\Model\ShippingInformationManagement $subject,
$cartId,
\Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
) {
if (!$extAttributes = $addressInformation->getExtensionAttributes()) {
return;
}

$quote = $this->quoteRepository->getActive($cartId);

$quote->setCustomFieldName($extAttributes->getCustomFieldName());

}

Reference : https://oyenetwork.com/articles/magento2-devliery-date-module-creation-from-scratch/ 

Re: How to add custom db_field in exists magento module table

Thanks for reply, but i need something easy.

I added three fields in customer_address_entity and need to work whit him.
If i use extension attribute, i can't search whit SearchCriteria. If i added in table i can't set value.

"I dont have an idea without extension attribute." - because i can't search whit search criteria.

Re: How to add custom db_field in exists magento module table

can you try it with eav attribute,which saves the values as you asked for.

Re: How to add custom db_field in exists magento module table

O say again, I need direct access on this db field. If I use something else like(EAV or custom attribute) I made to overhead to getter that value or setter that value. I have 500 000 customers, every customer have minimum 15-20 addresses. In every 10 minutes I need to sync Magento whit ERP system. If I use EAV Magento consume too many resources for simple update.

Please again, tell me how to add this db field in Customer Address directly on Service Contract.