Posts

Git - How to revert, cancel last changes on remote server

Sometimes you do something wrong. For example, push to master branch dev commits or something else, what does not have to find itself here.  There are present a lot of official recommends how to push revert commits... but honestly, in my case, it does not work in proper way, as you want. So, I found ferroconcrete method how to solve this issue. But it seems to somebody not gracefully, but it works at least. First step: Revert to required commit.  git reset --hard <commit-hash> (This command will reset the branch to the specified commit. Be cautious when using --hard as it will discard changes in your working directory and staging area.) Second step: Create new branch from this point.  git checkout -b <new-branch-name> (This command creates a new branch and switches to it. Replace <new-branch-name> with the desired name for your new branch.) Third step: Remove your damaged branch on server. git push origin --delete <branch-name> (Replace <br...

Symfony - Doctrine - How to change old attributes in entity to new PHP 8 attributes

Old-Style Annotations: Written as comments, typically using PHPDoc syntax (/** ... */). Annotations are interpreted by external tools or libraries. Doctrine ORM interprets annotations to configure database mappings. Limited flexibility in terms of syntax and available metadata types. May lack type safety and validation. PHP 8 Attributes: Written using the #[...] syntax. Introduced as a language feature in PHP 8 for adding metadata directly to code. Provides a more structured and standardized way of associating metadata with classes, methods, or properties. Attributes are a built-in language feature, eliminating the need for external interpretation in many cases. Attributes are directly handled by the PHP runtime. Offers better type safety and validation compared to traditional annotations. Supports the use of PHP's native types and expressions within attribute values. Attributes support namespacing, allowing for better organization and...

JavaScript - How to create select options from array

Define an array with the values you want to populate in the <select> element. var fruits = ["Apple", "Banana", "Orange", "Grapes", "Mango"]; Create a <select> element in your HTML markup. <select id="mySelect"></select> Iterate through the array and create <option> elements for each value, then append them to the <select> element. Here's an example: document.addEventListener("DOMContentLoaded", function () { var fruits = ["Apple", "Banana", "Orange", "Grapes", "Mango"]; var selectElement = document.getElementById("mySelect"); for (var i = 0; i < fruits.length; i++) { var option = document.createElement("option"); option.value = fruits[i]; option.text = fruits[i]; selectElement.appendChild(option); } }); In this example, the JavaScript code runs ...

Symfony - Doctrine - How to add subquery into where clause

If you want to add subquery into where clause, you need to create subquery firstly: $sub = $this->_em->createQueryBuilder()             ->select('cf.id')             ->from(CustomField::class, 'cf')             ->where('cf.type = :type AND cf.fieldName = :fieldName'); After that you can use this subquery as DQL: $this->_em->createQueryBuilder()->expr()->in('cfv.fieldId', $sub->getDQL()) In that case we use it as IN condition. But it is just example. And finally it will look like that: $this->createQueryBuilder('cfv')             ->update(CustomFieldValue::class, 'cfv')             ->set('cfv.value', ':value')             ->where($this->_em->createQueryBuilder()->expr()->in('cfv.fieldId', $sub->getDQL()))           ...

WordPress - How to create simple plugin with shortcode

Creating a simple WordPress plugin involves a few steps. Below is an example of a minimalistic WordPress plugin that adds a custom shortcode to display a greeting message. Step 1: Set Up Plugin Structure Create a new folder for your plugin in the "wp-content/plugins" directory. Let's call it "simple-greeting-plugin". Step 2: Create the Main Plugin File Inside your plugin folder (simple-greeting-plugin), create a main PHP file, e.g., "simple-greeting-plugin.php". Step 3: Add Plugin Information In your main PHP file, add the plugin information in the comment header. <?php /* Plugin Name: Simple Greeting Plugin Description: A simple WordPress plugin that adds a greeting shortcode. Version: 1.0 Author: Your Name */ Step 4: Add Greeting Shortcode Functionality Now, let's add the functionality to display a greeting message using a shortcode. Update your "simple-greeting-plugin.php" file as follows: <?php /* Plugin Name:...

Symfony - How to fix Circular Reference

 A circular reference in Symfony typically occurs when there's a loop in the dependency injection graph. It means that one service depends on another service, which in turn depends on the first one, creating an infinite loop. Check Your Service Definitions: Review your service definitions in the services.yaml or other configuration files. Look for any circular dependencies. A circular reference often occurs when Service A depends on Service B, and Service B depends on Service A. Use Setter Injection: Instead of injecting dependencies through the constructor, use setter injection. In Symfony, you can use setter injection by creating setter methods in your services and injecting dependencies through these methods after the service is instantiated. Lazy Loading: Consider using lazy loading for dependencies. Symfony allows lazy loading of services, which means that the actual instantiation of a service is delayed until it's actually used. You can enable lazy loading for a service b...

PHP - How to show expected return array elements

When your function or method returns an associative array, it's beneficial to document the expected structure of that array using PHPDoc type annotations. This practice not only serves as documentation for developers using your code but also enables features like auto-completion in integrated development environments (IDEs). For example: /** * Creates a user. * * @return array{"name": string, "age": int, "married": bool} */ function createUser(): array { return ['name' => 'Nayf', 'age' => 38, 'married' => true]; } In this PHPDoc block, the @return annotation is used to specify that the function returns an associative array with three elements: "name" of type string, "age" of type int, and "married" of type bool. This information provides clarity on the expected structure of the returned array. The PHPDoc type system supports various basic PHP types such as string, bool, in...