Skip to main content

Posts

Showing posts from February, 2023

Oop features in PHP 7

 OOP (Object-Oriented Programming) is a programming paradigm that utilizes objects to represent and manipulate data. PHP supports OOP features, which include: Classes: In PHP, a class is a blueprint for creating objects. It defines properties and methods that an object can have. Objects: An object is an instance of a class. It contains properties and methods that define its behavior. Encapsulation: This is the ability to encapsulate data and functionality within a class. This means that data is hidden from the outside world, and only methods that belong to the class can access it. Inheritance: Inheritance allows a class to inherit properties and methods from another class. This promotes code reuse and simplifies code maintenance. Polymorphism: This is the abilit...

What is EntityManager in LaravelDoctrine?

What EntityManager is and how it works in LaravelDoctrine. At its core, EntityManager is responsible for managing entities and their persistence in a database. It provides a layer of abstraction between your application code and the database, allowing you to work with entities as objects rather than directly interacting with the database using SQL statements. In order to work with entities using EntityManager , you first need to define your entities as classes that extend Doctrine\ORM\Mapping\Entity .  Here's an example of a simple User entity: <?php namespace App\Entities; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="users") */ class User { /** * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="string", length=255) */ private $name; /** * @ORM\Column(type="string", length=255) */ priva...

Difference between EntityManager::persist() and EntityManager::flush() methods in LaravelDoctrine

 The EntityManager::persist() and EntityManager::flush() methods are both part of the LaravelDoctrine library and are used for managing entities in a database. However, they serve different purposes. EntityManager::persist() is used to add a new entity instance to the EntityManager's list of managed entities, which will subsequently be saved to the database when the EntityManager's flush() method is called. Essentially, persist() schedules the entity for insertion into the database, but does not actually execute the SQL INSERT statement yet. Here's an example: <?php $user = new User(); $user->setName('John Doe'); $entityManager->persist($user); ?> In this example, we create a new User entity and set its name to "John Doe". We then pass this entity to the persist() method of the EntityManager object, which adds it to the list of managed entities. EntityManager::flush() , on the other hand, is used to synchronize all chang...