Adding an Auto-increment Column in db_schema.xml: A Professional Guide
/0 Comments/in Magento2 /by puneetk00
Introduction:
In Magento 2, the db_schema.xml file plays a crucial role in defining the database structure for your module. One common requirement when designing database tables is the inclusion of an auto-increment column. This column type automatically generates a unique value for each new record inserted into the table. In this article, we will explore how to add an auto-increment column in db_schema.xml with a step-by-step guide.
Understanding Auto-increment Columns:
An auto-increment column is a special type of column in a database table that assigns a unique numeric value to each new record. This value increments automatically as new records are inserted, ensuring uniqueness and simplifying data retrieval and management.
Adding an Auto-increment Column in db_schema.xml:
Follow these steps to add an auto-increment column using the db_schema.xml file in Magento 2:
Step 1: Locate or create the db_schema.xml file:
In your Magento module, navigate to the etc/db_schema.xml
file. If it doesn’t exist, create one in the appropriate directory.
Step 2: Define the table structure:
Inside the <schema>
tag, define the structure of your table using the <table>
tag. Specify the table name and any additional attributes required.
Step 3: Add the column declaration:
Within the <table>
tag, add a <column>
tag for the auto-increment column. Define the column name, type, and any other attributes.
xml<column xsi:type="int" name="entity_id" nullable="false" unsigned="true" identity="true" comment="Entity ID"/>
Here, the identity="true"
attribute signifies that the column should be an auto-increment column.
Step 4: Complete the db_schema.xml file:
Save the db_schema.xml file with the complete table and column definitions.
Step 5: Run the database upgrade:
To apply the changes, run the following command from the Magento root directory:
bin/magento setup:upgrade
This command triggers the installation and upgrade process, which executes the changes defined in the db_schema.xml file.
Conclusion:
Adding an auto-increment column to a database table is a common requirement when working with Magento 2. By following the steps outlined in this article, you can easily include an auto-increment column in the db_schema.xml file of your module. Utilizing the db_schema.xml file and its declarative approach ensures consistent and efficient management of your module’s database structure. Incorporating auto-increment columns enhances data integrity and simplifies the retrieval and manipulation of records within your Magento 2 application.
Adding TIMESTAMP_INIT as a Default Value in Declarative Schema
/0 Comments/in Uncategorized /by puneetk00Introduction:
When working with databases, it’s often essential to track the timestamp of various operations such as record creation or modification. Magento 2, a popular e-commerce platform, introduced the Declarative Schema approach to simplify database setup and maintenance. In this article, we will explore how to add the TIMESTAMP_INIT
value as a default value using the Declarative Schema in Magento 2.
Understanding Declarative Schema:
Declarative Schema is a powerful feature in Magento 2 that allows developers to define and manage database structures using XML files. It provides a structured way of creating and updating database tables, columns, indexes, and other elements. The schema is defined in a declarative manner, which means developers specify what the database should look like, and Magento handles the necessary database operations automatically.
Adding TIMESTAMP_INIT
as a Default Value:
To add TIMESTAMP_INIT
as a default value in the Declarative Schema, follow these steps:
Step 1: Create a new file or modify an existing one:
In your Magento module, navigate to the etc/db_schema.xml
file or create one if it doesn’t exist.
Step 2: Define the table structure:
Inside the <schema>
tag, define your table structure using the <table>
tag. For example, let’s say we have a table called example_table
with a column named created_at
that we want to set with the TIMESTAMP_INIT
value.
Step 3: Add the column declaration:
Within the <table>
tag, add a <column>
tag for the created_at
column. Specify the column name, type, and length as per your requirements.
xml<column xsi:type="timestamp" name="created_at" on_update="false" nullable="false" comment="Created At"/>
Here, on_update="false"
ensures that the created_at
column won’t update automatically when the record is modified.
Step 4: Set the default value:
To set TIMESTAMP_INIT
as the default value, include the default="TIMESTAMP_INIT"
attribute in the <column>
tag.
xml<column xsi:type="timestamp" name="created_at" on_update="false" nullable="false" default="TIMESTAMP_INIT" comment="Created At"/>
Step 5: Complete the db_schema.xml
file:
Save the db_schema.xml
file with the complete table and column definitions.
Step 6: Run the database upgrade:
To apply the changes, run the following command from the Magento root directory:
arduinobin/magento setup:upgrade
This command triggers the installation and upgrade process, which executes the Declarative Schema changes.
Conclusion:
By following the steps outlined in this article, you can easily add the TIMESTAMP_INIT
value as a default value in the Declarative Schema of your Magento 2 module. Leveraging Declarative Schema simplifies the management of database structures and ensures consistent database operations across different environments.
To call a static block into a GraphQL query in the Alpine.js
/0 Comments/in Uncategorized /by puneetk00To call a static block into a GraphQL query in the Alpine.js framework within Magento 2, you would need to follow these steps:
-
Create a GraphQL query:
-
Define your query in the
app/code/{Vendor}/{Module}/etc/graphql/schema.graphqls
file. For example:graphqltype Query { staticBlock(identifier: String!): String @resolver(class: "{Vendor}\{Module}\Model\Resolver\StaticBlock") @doc(description: "Retrieve the content of a static block by identifier") }
-
-
Create a Resolver class:
-
Create the resolver class
app/code/{Vendor}/{Module}/Model/Resolver/StaticBlock.php
. In this class, you will define the logic to retrieve the static block content. Here’s an example implementation:php<?php namespace {Vendor}{Module}ModelResolver; use MagentoCmsModelBlockRepository; use MagentoFrameworkExceptionNoSuchEntityException; use MagentoFrameworkGraphQlQueryResolverInterface; use MagentoFrameworkGraphQlQueryResolverContextInterface; class StaticBlock implements ResolverInterface { private $blockRepository; public function __construct(BlockRepository $blockRepository) { $this->blockRepository = $blockRepository; } public function resolve( ContextInterface $context, array $args, array $directives = [], GraphQLTypeDefinitionResolveInfo $info = null ) { $identifier = $args['identifier']; try { $block = $this->blockRepository->getByIdentifier($identifier); return $block->getContent(); } catch (NoSuchEntityException $e) { return null; } } }
-
-
Utilize the GraphQL query in Alpine.js:
-
In your Alpine.js component, make an HTTP request to the GraphQL endpoint to fetch the static block content. Here’s an example using Axios:
javascript<template> <div x-data="{ blockContent: '' }"> <div x-html="blockContent"></div> </div> </template> <script> import axios from 'axios'; export default { mounted() { axios.post('/graphql', { query: ` query { staticBlock(identifier: "your_block_identifier") } `, }) .then(response => { this.blockContent = response.data.data.staticBlock; }) .catch(error => { console.error(error); }); }, } </script>
-
Ensure to replace {Vendor}
and {Module}
with your actual vendor and module names, respectively. Also, replace "your_block_identifier"
with the identifier of the static block you want to retrieve.
Remember to adjust the code according to your specific requirements and ensure that you have appropriate permissions and configurations set up for GraphQL and the Alpine.js framework in your Magento 2 installation.
Credit Card Flow | Magento Braintree | Paypal Payflow Pro | Pk Magento 2
/0 Comments/in Uncategorized /by puneetk00Credit Card Flow | Magento Braintree | Paypal Payflow Pro
-
Users add products in cart then move on checkout.
-
User full fill all required fields then select shipping method and payment method
-
If payment is selected as Braintree credit card. Then user enters card details
-
Then Magento to validate all other field data like billing address, shipping address and shipping method etc.
-
Then User clicks on “place order” button.
-
Magento sends validation request to Braintree with Braintree (Merchant) credentials and Card Details.
-
After successful validation, Braintree sends back response token to Magento on real-time.
-
Magento now sends “Token, customer details & order details” to Braintree.
*details like customer billing & shipping address, item total, discount., order ID etc.
-
Braintree validates the card details through token and charge the amount to customer and sends back response to Magento in real-time with below responses
-
Success – Response contains Success array
-
False– Response contains the Error details
-
Null – Blank Array with no details
“Null” response is not handled currently which needs to be solutionized
Request example in json.
‘request’ =>
array (
‘customer’ =>
array (
‘firstName‘ => ‘xxrri-xxynn’,
‘lastName‘ => ‘McKillop’,
‘company’ => ”,
‘phone’ => ‘xxxxxxx’,
’email’ => ‘xxxx@outlook.com’,
),
‘amount’ => ‘189.99’,
‘paymentMethodNonce‘ => ‘tokencc_bd_wyn4wk_657vx3_gfkg9s_t85xkq_vr3’,
‘orderId‘ => ‘xx171241xxx38921’,
‘channel’ => ‘MagxxxxxeneBT’,
‘options’ =>
array (
‘skipAdvancedFraudChecking‘ => false,
‘storeInVaultOnSuccess‘ => true,
‘submitForSettlement‘ => true,
),
‘customFields‘ =>
array (
),
‘billing’ =>
array (
‘firstName‘ => ‘Terri-Lynn’,
‘lastName‘ => ‘McKillop’,
‘company’ => ”,
‘streetAddress‘ => ‘724 xxxtherine Street’,
‘extendedAddress‘ => ”,
‘locality’ => ‘thunder bay’,
‘region’ => ‘ON’,
‘postalCode‘ => ‘p7xxx’,
‘countryCodeAlpha2’ => ‘CA’,
),
‘shipping’ =>
array (
‘firstName‘ => ‘Terri-Lynn’,
‘lastName‘ => ‘McKillop’,
‘company’ => NULL,
‘streetAddress‘ => ‘724 Catherine Street’,
‘extendedAddress‘ => ”,
‘locality’ => ‘thunder bay’,
‘region’ => ‘ON’,
‘postalCode‘ => ‘p7e1l5’,
‘countryCodeAlpha2’ => ‘CA’,
‘countryCodeAlpha3’ => ‘CAN’,
),
‘purchaseOrderNumber‘ => ‘xxx’,
‘taxAmount‘ => ‘21.86’,
‘discountAmount‘ => ‘137.57’,
Response sample
Null
Failed
Response sample
‘client’ => ‘PayPal\Braintree\Gateway\Http\Client\TransactionSale‘,
‘response’ =>
array (
‘success’ => true,
” . “