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

Automatic Entity cast for Parser #2317

Closed
SteeveDroz opened this issue Oct 10, 2019 · 4 comments · Fixed by #3538
Closed

Automatic Entity cast for Parser #2317

SteeveDroz opened this issue Oct 10, 2019 · 4 comments · Fixed by #3538
Labels
new feature PRs for new features

Comments

@SteeveDroz
Copy link
Contributor

Describe the bug
Passing an Entity to the Parser displays an ErrorExceptions: Object of class (…) could not be converted to string

CodeIgniter 4 version
v4.0.0-rc.2.1

Affected module(s)
Entity or Parser, couldn't tell

Expected behavior, and steps to reproduce if appropriate
Currently, in order to be able to pass an entity to the parser, one must do the following:

// Entity:
namespace App\Entities;

class Article extends \CodeIgniter\Entity
{
    protected $casts = [
        'options' => 'array'
    ];

    // …
}

// Controller:
$article = …; // from the model or whatever
$parser = \Config\Services::parser();
$parser->setData(['article' => $article->options])->render('article.html');

It would be so convenient if the Entity class didn't need such a cast and if, when passed to a Parser, it was automatically cast into usable data.

Currently, the code below returns an ErrorException because you can't convert an Entity into a string. That simple way of doing would be very useful, though:

// Entity: nothing in particular!

// Controller:
$article = …; // from the model or whatever
$parser = \Config\Services::parser();
$parser->setData(['article' => $article])->render('article.html'); // or with compact('article')

Context

  • OS: Linux Fedora 30
  • Web server Apache 2.4.41
  • PHP version 7.3.10
@lonnieezell lonnieezell added the new feature PRs for new features label Oct 17, 2019
@jim-parry
Copy link
Contributor

Please raise future feature requests in the forum, thanks :)

@SteeveDroz
Copy link
Contributor Author

Will do, sorry for the quiproquo, I honestly thought this was a bug!

@MGatner
Copy link
Member

MGatner commented Feb 18, 2020

@SteeveDroz Sorry if I am misunderstanding... but why don't you just use the Entity method toArray()? Wouldn't that get at what you want precisely, without mucking around in parser casts?

$parser->setData(['article' => $article->toArray()])->render('article.html');

@SteeveDroz
Copy link
Contributor Author

@MGatner Because this method is undocumented and I just learn from it with your very revelant question.

The feature request issue seems to be turning into a documentation missing issue.

Thanks for the hint/answer!

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

Successfully merging a pull request may close this issue.

4 participants