DDC-556: Doctrine_Expression equivalent in Doctrine2? #691

Closed
opened 2026-01-22 12:46:55 +01:00 by admin · 7 comments
Owner

Originally created by @doctrinebot on GitHub (Apr 29, 2010).

Jira issue originally created by user mjh_ca:

Is this possible? Examples:

$myEntity->createdAt = new \Doctrine\ORM\Expression('CURRENT_TIMESTAMP');
$em->persist($myEntity);
/*** @Entity @HasLifeCycleCallbacks **/
class MyEntity {
    /****
     * @Column(type="string", columnDefinition="BINARY(16) NOT NULL")
     */
   public $md5;

   /****
    * @PrePersist
    */
   public function doSomethingOnPrePersist()
   {
       if (isset($this->md5)) $this->md5 = new \Doctrine\ORM\Expression('UNHEX(\'' . $this->md5 . '\'));
   }
}

$myEntity->md5 = md5('some string');
$em->persist($myEntity);
Originally created by @doctrinebot on GitHub (Apr 29, 2010). Jira issue originally created by user mjh_ca: Is this possible? Examples: ``` $myEntity->createdAt = new \Doctrine\ORM\Expression('CURRENT_TIMESTAMP'); $em->persist($myEntity); ``` ``` /*** @Entity @HasLifeCycleCallbacks **/ class MyEntity { /**** * @Column(type="string", columnDefinition="BINARY(16) NOT NULL") */ public $md5; /**** * @PrePersist */ public function doSomethingOnPrePersist() { if (isset($this->md5)) $this->md5 = new \Doctrine\ORM\Expression('UNHEX(\'' . $this->md5 . '\')); } } $myEntity->md5 = md5('some string'); $em->persist($myEntity); ```
admin added the Improvement label 2026-01-22 12:46:55 +01:00
admin closed this issue 2026-01-22 12:46:56 +01:00
Author
Owner

@doctrinebot commented on GitHub (Apr 29, 2010):

Comment created by @beberlei:

you can do this by using Doctrine\DBAL\Types\AbstractType instances

@doctrinebot commented on GitHub (Apr 29, 2010): Comment created by @beberlei: you can do this by using Doctrine\DBAL\Types\AbstractType instances
Author
Owner

@doctrinebot commented on GitHub (Apr 29, 2010):

Comment created by @beberlei:

http://www.doctrine-project.org/documentation/manual/2_0/en/basic-mapping#custom-mapping-types

but this does not work with sql expressions, only with php conversions

@doctrinebot commented on GitHub (Apr 29, 2010): Comment created by @beberlei: http://www.doctrine-project.org/documentation/manual/2_0/en/basic-mapping#custom-mapping-types but this does not work with sql expressions, only with php conversions
Author
Owner

@doctrinebot commented on GitHub (Apr 29, 2010):

Comment created by mjh_ca:

Thanks. It would be great if this could be further improved upon to allow unescaped SQL expressions to be passed.

@doctrinebot commented on GitHub (Apr 29, 2010): Comment created by mjh_ca: Thanks. It would be great if this could be further improved upon to allow unescaped SQL expressions to be passed.
Author
Owner

@doctrinebot commented on GitHub (Apr 30, 2010):

Comment created by romanb:

The main problem with something like this is that its really intrusive. Such expressions and SQL snippets should really not slip into the domain model. I have the feeling this should be handled at a different level/layer. Besides the obvious ORM dependency that is introduced with sth like "new Doctrine\ORM\Expression" inside the entity, what meaning has an "Expression" in the domain model? Really feels like an infrastructure concern that should be done elsewhere.

Another problem with sth like this:

$myEntity->createdAt = new \Doctrine\ORM\Expression('CURRENT_TIMESTAMP');
$em->persist($myEntity);

is that your object won't be updated with the actual value. When it comes to default values, PHP default values should be preferred instead of database default values.

@doctrinebot commented on GitHub (Apr 30, 2010): Comment created by romanb: The main problem with something like this is that its really intrusive. Such expressions and SQL snippets should really not slip into the domain model. I have the feeling this should be handled at a different level/layer. Besides the obvious ORM dependency that is introduced with sth like "new Doctrine\ORM\Expression" inside the entity, what meaning has an "Expression" in the domain model? Really feels like an infrastructure concern that should be done elsewhere. Another problem with sth like this: ``` $myEntity->createdAt = new \Doctrine\ORM\Expression('CURRENT_TIMESTAMP'); $em->persist($myEntity); ``` is that your object won't be updated with the actual value. When it comes to default values, PHP default values should be preferred instead of database default values.
Author
Owner

@doctrinebot commented on GitHub (Apr 30, 2010):

Comment created by mjh_ca:

@Roman That makes sense now that you point it out.

Efficient storage of an MD5 as in the second example can be implemented cleanly, as Benjamin suggested, with a simple custom mapping type. Could also do this for other "custom" efficient binary storage such as IP addresses. I'll contribute the code for a cookbook entry if you like. (it is MySQL specific and therefore non-portable, obviously).

As for the timestamp issue I think it is possible to live with that limitation of keeping it on the PHP side. Thanks for the analysis.

@doctrinebot commented on GitHub (Apr 30, 2010): Comment created by mjh_ca: @Roman That makes sense now that you point it out. Efficient storage of an MD5 as in the second example can be implemented cleanly, as Benjamin suggested, with a simple custom mapping type. Could also do this for other "custom" efficient binary storage such as IP addresses. I'll contribute the code for a cookbook entry if you like. (it is MySQL specific and therefore non-portable, obviously). As for the timestamp issue I think it is possible to live with that limitation of keeping it on the PHP side. Thanks for the analysis.
Author
Owner

@doctrinebot commented on GitHub (Apr 30, 2010):

Comment created by mjh_ca:

Withdrawing the request :)

@doctrinebot commented on GitHub (Apr 30, 2010): Comment created by mjh_ca: Withdrawing the request :)
Author
Owner

@doctrinebot commented on GitHub (Apr 30, 2010):

Issue was closed with resolution "Won't Fix"

@doctrinebot commented on GitHub (Apr 30, 2010): Issue was closed with resolution "Won't Fix"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#691