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

Support virtual fields in $_extract list for CSV generation #81

Closed
JayWalker512 opened this issue Jun 2, 2017 · 2 comments
Closed

Support virtual fields in $_extract list for CSV generation #81

JayWalker512 opened this issue Jun 2, 2017 · 2 comments

Comments

@JayWalker512
Copy link

JayWalker512 commented Jun 2, 2017

I make liberal use of virtual fields in my application for generating data on the fly, it would be nice to have access to these just like other fields when doing the setup for CSV generation. Currently I'm doing something like this:

$_extract = [
    'id',
    'full_name' => function ($row) {
        return $this->Sps->get($row['id'])->full_name;
    },
    'user.email',
    'birthdate',
];

Which works, and I can live with it, but it would be nice to be less clunky. If this is already supported in some way, it doesn't seem to be documented.

@josegonzalez
Copy link
Member

Interesting. I guess a virtual field isn't returned in toArray(): https://github.com/FriendsOfCake/cakephp-csvview/blob/master/src/View/CsvView.php#L310

@JayWalker512
Copy link
Author

Turns out my workaround is unnecessary. You can allow virtual fields to be exported in toArray() by specifying the name of the virtual field in the $_virtual array on the entity class ala:

class User extends Entity
{
    protected $_virtual = ['full_name'];
}

as pulled from here in the cookbook.

Then specifying the name of the virtual field in $_extract works as expected.

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

2 participants