Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object of class DateTime could not be converted to string #737

Closed
PoisonousJohn opened this issue May 5, 2012 · 17 comments
Closed

Object of class DateTime could not be converted to string #737

PoisonousJohn opened this issue May 5, 2012 · 17 comments

Comments

@PoisonousJohn
Copy link

I have this error in listAction when adding field that has orm type date or datetime. Has anyone idea how to fix this?

Render fails on 20 string of base_list_field.htm.twig. Object passes here instead of string.

        {%- block field %}{{ value }}{% endblock -%}

I have no clue where to look for reason of error. Maybe some kind of a data transformer missing

@thomas2411
Copy link

I have the same after new update. I am using 2.0 branches and has this error with datetime list type:

Object of class DateTime could not be converted to string in ...app\cache\dev\twig\....") in "SonataAdminBundle:CRUD:base_list_field.html.twig".

I don't know if something changed in Doctrine and is connected with __toString()

@fran6co
Copy link
Contributor

fran6co commented May 5, 2012

The problem is in the SonataIntlBundle. The 2.0 branch doesn't have the twig files for rendering (referenced here https://github.com/sonata-project/SonataDoctrineORMAdminBundle/blob/2.0/DependencyInjection/SonataDoctrineORMAdminExtension.php#L114)

Some fixes:

  • Remove SonataIntlBundle
  • Use the master branch of SonataIntlBundle (not sure if it contains incompatiblities with symfony 2.0)

@PoisonousJohn
Copy link
Author

Remove SonataIntlBundle

This works for me. But I'm not sure how it will affect other components

Use the master branch of SonataIntlBundle (not sure if it contains incompatiblities with symfony 2.0)

I'm now at master branch, but it seems like AdminBundle just ignores these templates. I checked defaultsArray. It's correct.

@fran6co
Copy link
Contributor

fran6co commented May 10, 2012

It's an optional bundle, everything should work normally if you remove it. Some internationalization features are not going to be available.

@PoisonousJohn
Copy link
Author

Ok, thx for your help =)

@rande
Copy link
Member

rande commented May 11, 2012

@JohnPoison is the field declared as DateTime in your doctrine definition ? if not you need to set the field type as second argument.

@PoisonousJohn
Copy link
Author

@rande
It's declared as a "date" type in orm file and no type specified in admin class

    fields:
        startDate:
            type: date

...
->add('startDate')
...

@andrewtch
Copy link
Contributor

bumping, DateTime can not be rendered on 2.1

@bigcalm
Copy link

bigcalm commented Sep 28, 2012

Still not fixed for Symfony 2.1.3.
Removing the SonataIntlBundle and clearing the cache made no difference for me.

Continue to see this error in a list view:

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class DateTime could not be converted to string in MyProject/app/cache/dev/twig/c8/ba/c455cc2ad87a1b1e1f5964432bbb.php line 54") in "SonataAdminBundle:CRUD:list.html.twig".

Additionally, base_list_field.html.twig doesn't appear to pull in list_datetime.html.twig
Instead the 'datetime' fields are being treated as 'text'

@andrewtch
Copy link
Contributor

@bigcalm , please make sure that you're using correct entity field naming in $list->add. If you have, e.g. private $soneField, you should do $list->add('someField'), not $list->add('some_field') which will give you the same error as above.

In my case the issue has been fixed by forcibly removing doctrine proxy classes and re-generating custom repositories.

@bigcalm
Copy link

bigcalm commented Sep 28, 2012

Well, now I just feel like an idiot :)

Thank you @andrewtch that fixed it.

@mmenozzi
Copy link

mmenozzi commented Nov 3, 2012

Hi,
I want to state that the cause of this error could be that SonataIntlBundle is installed (via composer for example) but not enabled in AppKernel.

This is because at https://github.com/sonata-project/SonataDoctrineORMAdminBundle/blob/2.0/DependencyInjection/SonataDoctrineORMAdminExtension.php#L114 the strategy to decide if load intl templates or not is implemented with class_exists that in such situation returns true.

Now if you look at https://github.com/sonata-project/SonataAdminBundle/blob/2.0/Twig/Extension/SonataAdminExtension.php#L74 you can see that Twig will throws the exception (because SonataIntlBundle isn't enabled) and so will be used the base_list_field.html.twig template that will try to convert a DateTime in string.

@waiting-for-dev
Copy link
Contributor

Thanks @mmenozzi your solution works. This should be stated in the documentation, because it cause very frustrating errors in the very early stage of the development.

@mmenozzi
Copy link

You're welcome. ;)

@xmak
Copy link
Contributor

xmak commented Apr 23, 2014

This error still happens when you try to use a datetime field as associated_property for a related entity filed in list action.

The field is defined as datetime in the related entity:

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="endTime", type="datetime", nullable=true)
     */
    private $endTime;

and added to list fileds like this:

>add('solvingLog', 'entity', array('label' => 'End date', 'associated_property' => 'endTime'))

And i get the same error message.

I have installed the SonataIntlBundle and enabled it in AppKernel.php.

Any ideas what could be wrong?

@xmak
Copy link
Contributor

xmak commented Apr 24, 2014

I figured out the cause of my problem and submitted an issue to the SonataDoctrineORMAdminBundle:

sonata-project/SonataDoctrineORMAdminBundle#347

@dialmedu
Copy link

Although you can use the

 
// views/default/date.html.twig
date : {{ game.gameDate|date('Y-m-d') }} 

result
date : 2015-01-01

Solution view http://stackoverflow.com/questions/render-datetime

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants