Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Faker bug with 'dateTime' #221

Closed
superhaggis opened this Issue Apr 5, 2013 · 6 comments

Comments

Projects
None yet
4 participants

Hello,

If you try to use Faker from within PropelBundle to create a random date and time, NULL is being returned (and echoed).

This is because objects of type \DateTime are being returned by Faker, not strings.

Example:

Foo\BarBundle\Model\Foobar:
  object1:
    name: 'Foobar'
    activated_at: <?php $faker('dateTime') ?>

Error:

[Propel] Exception
Malformed inline YAML string ('null) at line XXX (near "activated_at: 'null").

Temporary Solution:
In Propel\PropelBundle\DataFixtures\Loader\YamlDataLoader, scroll to line 55.

Replace...

echo Yaml::dump(call_user_func_array(array($generator, $type), $args)) . "\n";

... with...

$value = call_user_func_array(array($generator, $type), $args);

if ($value instanceof \DateTime) {
    echo Yaml::dump($value->format('Y-m-d H:i:s')) . "\n";
} else {
    echo Yaml::dump($value) . "\n";
}     

Now, hard-coding the DateTime format is not really ideal but I couldn't really settle on a correct implementation. Ideally, the actual vendor files for Faker should be modified in order to return data in a consistent manner. I'd be interested to know your collective thoughts on the matter. 😄

Member

jaugustin commented Apr 5, 2013

@superhaggis what about doing this :

Foo\BarBundle\Model\Foobar:
  object1:
    name: 'Foobar'
    activated_at: <?php $faker('dateTime')->format('Y-m-d H:i:s') ?>

@jaugustin Afraid that won't work, because the returned value is echoed, not returned. You end up with a non-object fatal error.

Member

jaugustin commented Apr 5, 2013

ok, you could use $faker('date', 'Y-m-d') and $faker('time', 'H:i:s') (I didn't test it, but I think it work)

@jaugustin I could use a workaround like that and the randomise my own hours, minutes and seconds but that kind of defeats the purpose of using the Faker library in my fixtures. 😄

Also, using $faker('date', 'Y-m-d') side-by-side with $faker('time', 'H:i:s') won't work because two prepared YAML strings are returned, i.e. activated_at: '2013-03-03' '12:00:00'. I would have to create my own helper function to parse these two strings and return a valid 'Y-m-d H:i:s' string which I shouldn't really have to do.

Gavinvin commented Apr 8, 2013

@jaugustin you could use the date() function to work around the dateTime() function not working, however this bug affects any of the functions in the Faker library that return anything other than a string. Function such as dateTimeBetween(), dateTimeThisCentury(), dateTimeThisDecade(), dateTimeThisYear() ,dateTimeThisMonth() and dateTimeAD() will also return null in the fixtures as these functions return DateTime objects, not a string.

@havvg havvg added this to the 1.5 - Symfony 2.8 LTS support milestone Jan 22, 2016

havvg added a commit to havvg/PropelBundle that referenced this issue Jan 24, 2016

convert Faker datetime into string
This fixes #221.

Faker returns a \DateTime object, but the YAML format requires a string.

@havvg havvg closed this Jan 24, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment