What data-type does Symfony/Doctrine recommend to persist the selection of a ChoiceType field?












0















I've tried many different approaches (see below), but I can't seem to find what Symfony/Doctrine recommends. I've searched the docs and all, with no luck.



Here's what I've considered:




  1. Having a static array with labels on the entity class and use it as a choices options when building a form. Then storing the selected item's index as an integer.

  2. Same thing, but mapping each label to a string key, and storing the key.

  3. Using an ENUM, but that doesn't seem to be recommended.


Of course, an important thing is also to be able to validate the supplied choice (be it an integer, a string, or anything else). I'm currently relying on @AssertChoice but I'm not sure that's perfect.



Do you know where I could find help/best practices on the subject?



Thanks!










share|improve this question

























  • It looks like you're starting from the wrong side. First create entities, then build forms for them. Not the vice-versa. Syfmony's Form Component is flexible enough to handle any case you could need.

    – Jakub Matczak
    Nov 23 '18 at 10:38











  • That's what I always do, yes :) But after doing that for years, I always wonder if there's a better way to save the choice selection. My question is really about the recommended way to do that.

    – Quentin
    Nov 23 '18 at 11:15













  • So why do you ask how your entity should look like basing on your form, instead of asking about how the form should look like basing on your entity?

    – Jakub Matczak
    Nov 23 '18 at 11:17











  • You're right, my question is probably not clear enough.

    – Quentin
    Nov 23 '18 at 11:18













  • For a limited list of choices (like ['Mr', Mrs']) I usually use your second approach: define the choices in a static array on the entity class, use @AssertChoice on the entity property to validate it, and use array_combine(Entity::choices, Entity::choices) as choices on the ChoiceType. So the label equals the key and is stored in the database. I think there is nothing wrong with this approach given that doctrine does not support real enums.

    – wasinger
    Nov 23 '18 at 14:42


















0















I've tried many different approaches (see below), but I can't seem to find what Symfony/Doctrine recommends. I've searched the docs and all, with no luck.



Here's what I've considered:




  1. Having a static array with labels on the entity class and use it as a choices options when building a form. Then storing the selected item's index as an integer.

  2. Same thing, but mapping each label to a string key, and storing the key.

  3. Using an ENUM, but that doesn't seem to be recommended.


Of course, an important thing is also to be able to validate the supplied choice (be it an integer, a string, or anything else). I'm currently relying on @AssertChoice but I'm not sure that's perfect.



Do you know where I could find help/best practices on the subject?



Thanks!










share|improve this question

























  • It looks like you're starting from the wrong side. First create entities, then build forms for them. Not the vice-versa. Syfmony's Form Component is flexible enough to handle any case you could need.

    – Jakub Matczak
    Nov 23 '18 at 10:38











  • That's what I always do, yes :) But after doing that for years, I always wonder if there's a better way to save the choice selection. My question is really about the recommended way to do that.

    – Quentin
    Nov 23 '18 at 11:15













  • So why do you ask how your entity should look like basing on your form, instead of asking about how the form should look like basing on your entity?

    – Jakub Matczak
    Nov 23 '18 at 11:17











  • You're right, my question is probably not clear enough.

    – Quentin
    Nov 23 '18 at 11:18













  • For a limited list of choices (like ['Mr', Mrs']) I usually use your second approach: define the choices in a static array on the entity class, use @AssertChoice on the entity property to validate it, and use array_combine(Entity::choices, Entity::choices) as choices on the ChoiceType. So the label equals the key and is stored in the database. I think there is nothing wrong with this approach given that doctrine does not support real enums.

    – wasinger
    Nov 23 '18 at 14:42
















0












0








0








I've tried many different approaches (see below), but I can't seem to find what Symfony/Doctrine recommends. I've searched the docs and all, with no luck.



Here's what I've considered:




  1. Having a static array with labels on the entity class and use it as a choices options when building a form. Then storing the selected item's index as an integer.

  2. Same thing, but mapping each label to a string key, and storing the key.

  3. Using an ENUM, but that doesn't seem to be recommended.


Of course, an important thing is also to be able to validate the supplied choice (be it an integer, a string, or anything else). I'm currently relying on @AssertChoice but I'm not sure that's perfect.



Do you know where I could find help/best practices on the subject?



Thanks!










share|improve this question
















I've tried many different approaches (see below), but I can't seem to find what Symfony/Doctrine recommends. I've searched the docs and all, with no luck.



Here's what I've considered:




  1. Having a static array with labels on the entity class and use it as a choices options when building a form. Then storing the selected item's index as an integer.

  2. Same thing, but mapping each label to a string key, and storing the key.

  3. Using an ENUM, but that doesn't seem to be recommended.


Of course, an important thing is also to be able to validate the supplied choice (be it an integer, a string, or anything else). I'm currently relying on @AssertChoice but I'm not sure that's perfect.



Do you know where I could find help/best practices on the subject?



Thanks!







php symfony doctrine-orm persist choice






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 10:31







Quentin

















asked Nov 23 '18 at 10:26









QuentinQuentin

5712




5712













  • It looks like you're starting from the wrong side. First create entities, then build forms for them. Not the vice-versa. Syfmony's Form Component is flexible enough to handle any case you could need.

    – Jakub Matczak
    Nov 23 '18 at 10:38











  • That's what I always do, yes :) But after doing that for years, I always wonder if there's a better way to save the choice selection. My question is really about the recommended way to do that.

    – Quentin
    Nov 23 '18 at 11:15













  • So why do you ask how your entity should look like basing on your form, instead of asking about how the form should look like basing on your entity?

    – Jakub Matczak
    Nov 23 '18 at 11:17











  • You're right, my question is probably not clear enough.

    – Quentin
    Nov 23 '18 at 11:18













  • For a limited list of choices (like ['Mr', Mrs']) I usually use your second approach: define the choices in a static array on the entity class, use @AssertChoice on the entity property to validate it, and use array_combine(Entity::choices, Entity::choices) as choices on the ChoiceType. So the label equals the key and is stored in the database. I think there is nothing wrong with this approach given that doctrine does not support real enums.

    – wasinger
    Nov 23 '18 at 14:42





















  • It looks like you're starting from the wrong side. First create entities, then build forms for them. Not the vice-versa. Syfmony's Form Component is flexible enough to handle any case you could need.

    – Jakub Matczak
    Nov 23 '18 at 10:38











  • That's what I always do, yes :) But after doing that for years, I always wonder if there's a better way to save the choice selection. My question is really about the recommended way to do that.

    – Quentin
    Nov 23 '18 at 11:15













  • So why do you ask how your entity should look like basing on your form, instead of asking about how the form should look like basing on your entity?

    – Jakub Matczak
    Nov 23 '18 at 11:17











  • You're right, my question is probably not clear enough.

    – Quentin
    Nov 23 '18 at 11:18













  • For a limited list of choices (like ['Mr', Mrs']) I usually use your second approach: define the choices in a static array on the entity class, use @AssertChoice on the entity property to validate it, and use array_combine(Entity::choices, Entity::choices) as choices on the ChoiceType. So the label equals the key and is stored in the database. I think there is nothing wrong with this approach given that doctrine does not support real enums.

    – wasinger
    Nov 23 '18 at 14:42



















It looks like you're starting from the wrong side. First create entities, then build forms for them. Not the vice-versa. Syfmony's Form Component is flexible enough to handle any case you could need.

– Jakub Matczak
Nov 23 '18 at 10:38





It looks like you're starting from the wrong side. First create entities, then build forms for them. Not the vice-versa. Syfmony's Form Component is flexible enough to handle any case you could need.

– Jakub Matczak
Nov 23 '18 at 10:38













That's what I always do, yes :) But after doing that for years, I always wonder if there's a better way to save the choice selection. My question is really about the recommended way to do that.

– Quentin
Nov 23 '18 at 11:15







That's what I always do, yes :) But after doing that for years, I always wonder if there's a better way to save the choice selection. My question is really about the recommended way to do that.

– Quentin
Nov 23 '18 at 11:15















So why do you ask how your entity should look like basing on your form, instead of asking about how the form should look like basing on your entity?

– Jakub Matczak
Nov 23 '18 at 11:17





So why do you ask how your entity should look like basing on your form, instead of asking about how the form should look like basing on your entity?

– Jakub Matczak
Nov 23 '18 at 11:17













You're right, my question is probably not clear enough.

– Quentin
Nov 23 '18 at 11:18







You're right, my question is probably not clear enough.

– Quentin
Nov 23 '18 at 11:18















For a limited list of choices (like ['Mr', Mrs']) I usually use your second approach: define the choices in a static array on the entity class, use @AssertChoice on the entity property to validate it, and use array_combine(Entity::choices, Entity::choices) as choices on the ChoiceType. So the label equals the key and is stored in the database. I think there is nothing wrong with this approach given that doctrine does not support real enums.

– wasinger
Nov 23 '18 at 14:42







For a limited list of choices (like ['Mr', Mrs']) I usually use your second approach: define the choices in a static array on the entity class, use @AssertChoice on the entity property to validate it, and use array_combine(Entity::choices, Entity::choices) as choices on the ChoiceType. So the label equals the key and is stored in the database. I think there is nothing wrong with this approach given that doctrine does not support real enums.

– wasinger
Nov 23 '18 at 14:42














0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444889%2fwhat-data-type-does-symfony-doctrine-recommend-to-persist-the-selection-of-a-cho%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444889%2fwhat-data-type-does-symfony-doctrine-recommend-to-persist-the-selection-of-a-cho%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?