Can't create fixtures with dates

2.4k views Asked by At

I have the following User entity and fixture file, but when I run app/console doctrine:fixtures:load I get the following error and not sure why. If I remove the date fields the fixtures generate fine. What am I missing here? Thanks.

The error: Could not determine how to assign created_at to a AppBundle\Entity\User object

<?php

namespace AppBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="AppBundle\Entity\UserRepository")
 */
class User
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="role", type="string", length=255)
     */
    private $role;

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=255)
     */
    private $email;

    /**
     * @var string
     *
     * @ORM\Column(name="phone", type="string", length=16)
     */
    private $phone;

    /**
     * @var \DateTime
     *
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(name="created_at", type="datetime", nullable=true)
     */
    private $createdAt;

    /**
     * @var \DateTime
     *
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(name="updated_at", type="datetime", nullable=true)
     */
    private $updatedAt;

And my fixture yml:

AppBundle\Entity\User:
user{1..10}:
    name: <firstName()> <lastName()>
    role: employee
    email: <email()>
    phone: <numberBetween(1555000000, 1555999999)>
    created_at: <datetimeBetween('-5 days', 'now')>
    updated_at: <datetimeBetween('-5 days', 'now')>
2

There are 2 answers

2
Dan Mironis On BEST ANSWER

Not sure, but I believe that the naming is not correct, just what the error is saying. I think it should be:

createdAt: <datetimeBetween('-5 days', 'now')>
updatedAt: <datetimeBetween('-5 days', 'now')>
4
Maks On

I am afraid you can't set timestampable fields manually. In documentation and entity example there are no setters at all for such fields. So in fixtures you can simply remove created_at and updated_at (they will be set automatically):

ppBundle\Entity\User:
user{1..10}:
    name: <firstName()> <lastName()>
    role: employee
    email: <email()>
    phone: <numberBetween(1555000000, 1555999999)>