Top Related Projects
Doctrine Object Relational Mapper (ORM)
Integration bundle for DoctrineExtensions by l3pp4rd in Symfony
Easily serialize, and deserialize data of any complexity (supports XML, JSON, YAML)
This Bundle provides various tools to rapidly develop RESTful API's with Symfony
Quick Overview
The DoctrineExtensions project is a set of Doctrine 2 behavioral extensions that add new functionality to the Doctrine ORM. These extensions provide a wide range of features, such as tree structures, sluggable, timestampable, and more, to enhance the functionality of your Doctrine-based applications.
Pros
- Extensive Functionality: The project offers a diverse set of extensions, covering a wide range of common use cases, such as tree structures, sluggable, and timestampable.
- Actively Maintained: The project is actively maintained, with regular updates and bug fixes, ensuring compatibility with the latest versions of Doctrine.
- Flexible and Customizable: The extensions are designed to be flexible and customizable, allowing developers to tailor them to their specific needs.
- Community Support: The project has a strong community of contributors and users, providing support and feedback to help improve the extensions.
Cons
- Complexity: The project can be complex, especially for developers new to Doctrine, as it requires a good understanding of the Doctrine ORM and its concepts.
- Potential Performance Impact: Depending on the extensions used, there may be a performance impact on the application, as the extensions add additional functionality and processing.
- Dependency on Doctrine: The project is tightly coupled with the Doctrine ORM, which means that it may not be suitable for applications that do not use Doctrine.
- Limited Documentation: While the project has good documentation, it may not be as comprehensive as some developers would like, especially for more advanced use cases.
Code Examples
Tree Structure Extension
use Gedmo\Tree\Entity\Repository\NestedTreeRepository;
use Gedmo\Tree\TreeListener;
// Set up the TreeListener
$treeListener = new TreeListener();
$evm = $entityManager->getEventManager();
$evm->addEventSubscriber($treeListener);
// Create a new node
$node = new Category();
$node->setTitle('Electronics');
$entityManager->persist($node);
$entityManager->flush();
// Move the node to a new parent
$parent = $entityManager->getRepository(Category::class)
->findOneByTitle('Computers');
$node->setParent($parent);
$entityManager->flush();
This code demonstrates the usage of the Tree Structure extension, which allows you to manage hierarchical data structures within your Doctrine-based application.
Sluggable Extension
use Gedmo\Sluggable\Sluggable;
use Gedmo\Sluggable\SluggableListener;
// Set up the SluggableListener
$sluggableListener = new SluggableListener();
$evm = $entityManager->getEventManager();
$evm->addEventSubscriber($sluggableListener);
// Create a new entity with a slug
$post = new BlogPost();
$post->setTitle('My Awesome Blog Post');
$entityManager->persist($post);
$entityManager->flush();
// The slug will be automatically generated: 'my-awesome-blog-post'
echo $post->getSlug();
This code demonstrates the usage of the Sluggable extension, which automatically generates URL-friendly slugs based on the title or other fields of your entities.
Timestampable Extension
use Gedmo\Timestampable\Timestampable;
use Gedmo\Timestampable\TimestampableListener;
// Set up the TimestampableListener
$timestampableListener = new TimestampableListener();
$evm = $entityManager->getEventManager();
$evm->addEventSubscriber($timestampableListener);
// Create a new entity with timestamps
$post = new BlogPost();
$post->setTitle('My Awesome Blog Post');
$entityManager->persist($post);
$entityManager->flush();
// The created_at and updated_at fields will be automatically set
echo $post->getCreatedAt()->format('Y-m-d H:i:s');
echo $post->getUpdatedAt()->format('Y-m-d H:i:s');
This code demonstrates the usage of the Timestampable extension, which automatically sets the created_at
and updated_at
fields of your entities based on the current timestamp
Competitor Comparisons
Doctrine Object Relational Mapper (ORM)
Pros of Doctrine ORM
- Core ORM functionality with extensive features and documentation
- Active development and maintenance by the Doctrine Project
- Seamless integration with Symfony and other PHP frameworks
Cons of Doctrine ORM
- Steeper learning curve for beginners
- Limited built-in support for advanced database functions
Code Comparison
DoctrineExtensions:
$qb->select('RAND() as rand')
->from('Entity\Name', 'e')
->orderBy('rand');
Doctrine ORM:
$qb->select('e')
->from('Entity\Name', 'e')
->orderBy('e.id', 'ASC');
Key Differences
- DoctrineExtensions provides additional database functions not available in Doctrine ORM
- Doctrine ORM offers a more comprehensive ORM solution
- DoctrineExtensions can be used alongside Doctrine ORM to extend its functionality
Use Cases
- Use Doctrine ORM for full-featured ORM capabilities in PHP projects
- Employ DoctrineExtensions when needing specific database functions not provided by Doctrine ORM
- Combine both for a powerful and flexible database interaction solution
Integration bundle for DoctrineExtensions by l3pp4rd in Symfony
Pros of StofDoctrineExtensionsBundle
- Seamless integration with Symfony framework
- Provides configuration options through Symfony's bundle system
- Easier to set up and use in Symfony projects
Cons of StofDoctrineExtensionsBundle
- Limited to Symfony projects, less flexible for other PHP applications
- May have a steeper learning curve for developers not familiar with Symfony
Code Comparison
StofDoctrineExtensionsBundle (in Symfony's config.yml):
stof_doctrine_extensions:
orm:
default:
timestampable: true
sluggable: true
DoctrineExtensions (in PHP):
$config = new Doctrine\ORM\Configuration();
$config->addCustomStringFunction('GROUP_CONCAT', 'DoctrineExtensions\Query\Mysql\GroupConcat');
$config->addCustomNumericFunction('RAND', 'DoctrineExtensions\Query\Mysql\Rand');
StofDoctrineExtensionsBundle is tailored for Symfony projects, offering easy integration and configuration through Symfony's bundle system. It's ideal for developers working within the Symfony ecosystem but may be less suitable for standalone PHP applications.
DoctrineExtensions, on the other hand, provides a more flexible approach that can be used in any PHP project using Doctrine ORM. It offers a wide range of custom functions and extensions but requires more manual setup and configuration.
The choice between these two largely depends on the project's framework and specific requirements.
Easily serialize, and deserialize data of any complexity (supports XML, JSON, YAML)
Pros of JMSSerializerBundle
- More comprehensive serialization/deserialization capabilities
- Better support for complex object graphs and circular references
- Extensive configuration options for fine-grained control
Cons of JMSSerializerBundle
- Steeper learning curve due to its complexity
- Potentially slower performance for simple serialization tasks
- Requires more setup and configuration
Code Comparison
JMSSerializerBundle:
use JMS\Serializer\Annotation as Serializer;
class User
{
/** @Serializer\Expose */
private $username;
}
DoctrineExtensions:
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class User
{
/** @ORM\Column(type="string") */
private $username;
}
JMSSerializerBundle offers more control over serialization through annotations, while DoctrineExtensions focuses on extending Doctrine ORM functionality. JMSSerializerBundle is better suited for complex serialization needs, whereas DoctrineExtensions is more lightweight and integrates seamlessly with Doctrine ORM for database operations and simple extensions.
This Bundle provides various tools to rapidly develop RESTful API's with Symfony
Pros of FOSRestBundle
- Provides a comprehensive set of tools for building RESTful APIs in Symfony
- Offers flexible content negotiation and view layer integration
- Includes built-in support for API versioning and documentation
Cons of FOSRestBundle
- Steeper learning curve due to its extensive feature set
- May introduce unnecessary complexity for simpler API projects
- Requires additional configuration and setup compared to DoctrineExtensions
Code Comparison
FOSRestBundle:
use FOS\RestBundle\Controller\AbstractFOSRestController;
class ApiController extends AbstractFOSRestController
{
public function getUsers()
{
$users = $this->getDoctrine()->getRepository(User::class)->findAll();
return $this->handleView($this->view($users));
}
}
DoctrineExtensions:
use Doctrine\ORM\Query\Expr;
$qb = $em->createQueryBuilder();
$qb->select('u')
->from('User', 'u')
->where($qb->expr()->in('u.id', ':ids'))
->setParameter('ids', [1, 2, 3]);
FOSRestBundle is more focused on building RESTful APIs with Symfony, providing a complete toolkit for API development. DoctrineExtensions, on the other hand, extends Doctrine ORM's functionality with additional query functions and is more database-centric. While FOSRestBundle offers a higher-level abstraction for API development, DoctrineExtensions provides lower-level database query enhancements.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
DoctrineExtensions
A set of extensions to Doctrine 2 that add support for functions available in MySQL, Oracle, PostgreSQL and SQLite.
DB | Functions |
---|---|
MySQL | ACOS, ADDTIME, AES_DECRYPT, AES_ENCRYPT, ANY_VALUE, ASCII, ASIN, ATAN, ATAN2, BINARY, BIT_COUNT, BIT_XOR, CAST, CEIL, CHAR_LENGTH, COLLATE, CONCAT_WS, CONVERT_TZ, COS, COT, COUNTIF, CRC32, DATE, DATE_FORMAT, DATEADD, DATEDIFF, DATESUB, DAY, DAYNAME, DAYOFWEEK, DAYOFYEAR, DEGREES, DIV, EXP, EXTRACT, FIELD, FIND_IN_SET, FLOOR, FORMAT, FROM_BASE64, FROM_UNIXTIME, GREATEST, GROUP_CONCAT, HEX, HOUR, IFELSE, IFNULL, INET_ATON, INET_NTOA, INET6_ATON, INET6_NTOA, INSTR, IS_IPV4, IS_IPV4_COMPAT, IS_IPV4_MAPPED, IS_IPV6, JSON_CONTAINS, JSON_DEPTH, JSON_LENGTH, LAG, LAST_DAY, LEAD, LEAST, LOG, LOG10, LOG2, LPAD, MAKEDATE, MATCH, MD5, MINUTE, MONTH, MONTHNAME, NOW, NULLIF, OVER, PERIOD_DIFF, PI, POWER, QUARTER, RADIANS, RAND, REGEXP, REPLACE, ROUND, RPAD, SECOND, SECTOTIME, SHA1, SHA2, SIN, SOUNDEX, STD, STDDEV, STRTODATE, STR_TO_DATE, SUBSTRING_INDEX, TAN, TIME, TIMEDIFF, TIMESTAMPADD, TIMESTAMPDIFF, TIMETOSEC, TRUNCATE, UNHEX, UNIX_TIMESTAMP, UTC_TIMESTAMP, UUID_SHORT, VARIANCE, WEEK, WEEKDAY, WEEKOFYEAR, YEAR, YEARMONTH, YEARWEEK |
Oracle | CEIL, DAY, FLOOR, HOUR, LISTAGG, MINUTE, MONTH, NVL, SECOND, TO_CHAR, TO_DATE, TRUNC, YEAR |
SQLite | CASE WHEN THEN ELSE END, DATE, DATE_FORMAT*, DAY, HOUR, IFNULL, JULIANDAY, MINUTE, MONTH, REPLACE, ROUND, SECOND, STRFTIME, WEEK, WEEKDAY, YEAR |
PostgreSQL | AT_TIME_ZONE, COUNT_FILTER, DATE, DATE_PART, DATE_TRUNC, DAY, EXTRACT, GREATEST, HOUR, LEAST, MINUTE, MONTH, REGEXP_REPLACE, SECOND, STRING_AGG, TO_CHAR, TO_DATE, YEAR |
Note: SQLite date functions are implemented as
strftime(format, value)
. SQLite only supports the most common formats, sodate_format
will convert the mysql substitutions to the closest available SQLite substitutions. This meansdate_format(field, '%b %D %Y') -> Jan 1st 2015
becomesstrftime('%m %d %Y', field) -> 01 01 2015
.
Installation
To install this library, run the command below and you will get the latest version:
composer require beberlei/doctrineextensions
If you want to run phpunit:
composer run test
If you want to run php-cs-fixer:
composer run lint
Usage
Symfony
If you are using DoctrineExtensions with Symfony read How to Register custom DQL Functions.
You can find example Symfony configuration for using DoctrineExtensions custom DQL functions in config.
Standalone
If you are using DoctrineExtensions standalone, you need to register the custom function in the ORM configuration. For more information check out the documentation of Doctrine DQL User Defined Functions.
Notes
- MySQL
DATE_ADD
is available in DQL asDATEADD(CURRENT_DATE(), 1, 'DAY')
- MySQL
DATE_SUB
is available in DQL asDATESUB(CURRENT_DATE(), 1, 'DAY')
- MySQL
IF
is available in DQL asIFELSE(field > 0, 'true', 'false')
Troubleshooting
Issues are disabled on this repository, if a custom DQL function that you want isn't provided, or does not support the arguments you want to pass, pull requests are open and we would love to have your contributions.
Top Related Projects
Doctrine Object Relational Mapper (ORM)
Integration bundle for DoctrineExtensions by l3pp4rd in Symfony
Easily serialize, and deserialize data of any complexity (supports XML, JSON, YAML)
This Bundle provides various tools to rapidly develop RESTful API's with Symfony
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot