27

I have a field named "birthday" in doctrine entity.

I would like to create an object to add to database using doctrine.

Inside the controller :

$name = "John Alex";
$birthday = "11-11-90";
$student = new Student();
$student->setName($name);
$student->setBirthday(strtotime($birthday);
...

but when I try to persist I get this error

Fatal error: Call to a member function format() on a non-object in /Library/WebServer/Documents/Symfony/vendor/doctrine-dbal/lib/Doctrine/DBAL/Types/DateType.php on line 44

Edit:

My entity:

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

/**
 * @var date $birthday
 *
 * @ORM\Column(name="birthday", type="date", nullable=true)
 */
private $birthday;

/**
 * Set birthday
 *
 * @param date $birthday
 */
public function setBirthday($birthday)
{
    $this->birthday = $birthday;
}

/**
 * Get birthday
 *
 * @return date 
 */
public function getBirthday()
{
    return $this->birthday;
}
Mick
  • 30,759
  • 16
  • 111
  • 130
AKRAM EL HAMDAOUI
  • 1,818
  • 4
  • 22
  • 27
  • Could you show us your entity related to Student ? And did you check [this answer](http://stackoverflow.com/questions/7463137/saving-a-zend-date-in-the-database-with-doctrine-2-1) ? – j0k May 31 '12 at 15:03
  • http://pastebin.com/8D7tdSef (i can't edit the thread) – AKRAM EL HAMDAOUI May 31 '12 at 15:25
  • Did you check [this answer](http://stackoverflow.com/questions/7463137/saving-a-zend-date-in-the-database-with-doctrine-2-1) ? - about DateTime – j0k May 31 '12 at 15:46

2 Answers2

41
$name = "John Alex";
$birthday = "11-11-1990"; // I changed this
$student = new Student();
$student->setName($name);
$student->setBirthday(new \DateTime($birthday)); // setting a new date instance
// ...
Ocramius
  • 25,171
  • 7
  • 103
  • 107
AKRAM EL HAMDAOUI
  • 1,818
  • 4
  • 22
  • 27
30

Fields of your entities mapped as "datetime" or "date" should contain instances of DateTime.

Therefore, your setter should be type-hinted as following:

/**
 * Set birthday
 *
 * @param \DateTime|null $birthday
 */
public function setBirthday(\DateTime $birthday = null)
{
    $this->birthday = $birthday ? clone $birthday : null;
}

/**
 * Get birthday
 *
 * @return \DateTime|null 
 */
public function getBirthday()
{
    return $this->birthday ? clone $this->birthday : null;
}

This allows setting either null or an instance of DateTime for the birthday.

As you notice, I also clone the values for the birthday date to avoid breaking encapsulation (see Doctrine2 ORM does not save changes to a DateTime field ).

To set the birthday, you then simply do following:

$student->setBirthday(new \DateTime('11-11-90'));
Community
  • 1
  • 1
Ocramius
  • 25,171
  • 7
  • 103
  • 107
  • `php app/console doctrine:generate:entities` generated only the following method header `* @param \DateTime $birthdate, * @return Person, public function setBirthdate($birthdate)`. When I adopt the method header to `public function setBirthdate(\DateTime $birthdate = NULL)` I get `Argument 1 passed to <...>::setBirthdate() must be an instance of DateTime, string given`. But the string is empty (because form field is not filled). – rabudde Mar 20 '13 at 09:19
  • @rabudde you shouldn't generate your entities. Entities are part of your code base, and are a core component of your domain. Generated code is usually a bad idea since you avoid testing it after generating it. Also, it looks like your form isn't converting an empty value to a `null` value. – Ocramius Mar 20 '13 at 09:26