DDC-1647: DateTime field returns to string if used in association #2069

Closed
opened 2026-01-22 13:39:29 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (Feb 10, 2012).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user alexeyshockov:

I have two objects with one-to-many accociation, based on date ("date" type column in all two objects):

@ORM\JoinColumn(name="date", referencedColumnName="date"),

When I establish accociation, I get strange behaviour — without association date was DateTime, after — string.

Originally created by @doctrinebot on GitHub (Feb 10, 2012). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user alexeyshockov: I have two objects with one-to-many accociation, based on date ("date" type column in all two objects): ``` @ORM\JoinColumn(name="date", referencedColumnName="date"), ``` When I establish accociation, I get strange behaviour — without association date was DateTime, after — string.
admin added the Bug label 2026-01-22 13:39:29 +01:00
admin closed this issue 2026-01-22 13:39:30 +01:00
Author
Owner

@doctrinebot commented on GitHub (Feb 10, 2012):

Comment created by @beberlei:

Since DateTime cannot be part of a PK it cannot be part of a join column. how does the full mapping look like?

@doctrinebot commented on GitHub (Feb 10, 2012): Comment created by @beberlei: Since DateTime cannot be part of a PK it cannot be part of a join column. how does the full mapping look like?
Author
Owner

@doctrinebot commented on GitHub (Feb 13, 2012):

Comment created by megazoll:

/****
 * @ORM\Entity
 */
class Blank
{
    /****
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     * @var int
     */
    private $id;

    /****
     * @ORM\Column(type="date")
     *
     * @var \DateTime
     */
    private $date;
}
/****
 * @ORM\Entity
 */
class Sale
{
    /****
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     * @var int
     */
    private $id;

    /****
     * @ORM\Column(type="date")
     *
     * @var \DateTime
     */
    private $date;

    /****
     * @ORM\ManyToOne(targetEntity="Blank", fetch="EAGER")
     * @ORM\JoinColumn(name="date", referencedColumnName="date")
     */
    private $blank;
}

$blank loading properly, but field "date" doesn't mapping to \DateTime

// without annotation on $blank
var_dump($sale->getDate()); // output "object(DateTime)#266..."
// with annotation on $blank
var_dump($sale->getDate()); // output "string(10) "2012-02-13""
@doctrinebot commented on GitHub (Feb 13, 2012): Comment created by megazoll: ``` /**** * @ORM\Entity */ class Blank { /**** * @ORM\Column(type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") * * @var int */ private $id; /**** * @ORM\Column(type="date") * * @var \DateTime */ private $date; } ``` ``` /**** * @ORM\Entity */ class Sale { /**** * @ORM\Column(type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") * * @var int */ private $id; /**** * @ORM\Column(type="date") * * @var \DateTime */ private $date; /**** * @ORM\ManyToOne(targetEntity="Blank", fetch="EAGER") * @ORM\JoinColumn(name="date", referencedColumnName="date") */ private $blank; } ``` $blank loading properly, but field "date" doesn't mapping to \DateTime ``` // without annotation on $blank var_dump($sale->getDate()); // output "object(DateTime)#266..." // with annotation on $blank var_dump($sale->getDate()); // output "string(10) "2012-02-13"" ```
Author
Owner

@doctrinebot commented on GitHub (Feb 20, 2012):

Comment created by @beberlei:

This kind of association is not supported, you can only map towards the Primary key of another entity. "DateTime" type cannot be part of the primary key, you have to map it as string to get it working. Sorry, these are two restrictions that greatly simplify and speed up the internals of Doctrine and that we have to keep.

@doctrinebot commented on GitHub (Feb 20, 2012): Comment created by @beberlei: This kind of association is not supported, you can only map towards the Primary key of another entity. "DateTime" type cannot be part of the primary key, you have to map it as string to get it working. Sorry, these are two restrictions that greatly simplify and speed up the internals of Doctrine and that we have to keep.
Author
Owner

@doctrinebot commented on GitHub (Feb 20, 2012):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Feb 20, 2012): Issue was closed with resolution "Invalid"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#2069