Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8577279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:07:25+00:00 2026-06-11T20:07:25+00:00

There are two entities, Category and Device . A device can be related to

  • 0

There are two entities, Category and Device. A device can be related to exactly one category. A category can be related to many devices.

I use following custom form type to create and edit a device:

class DeviceFormType extends AbstractType {

    public function buildForm(FormBuilder $builder, array $options) {

        $builder->add('name');
        $builder->add('category', 'entity', array(
            'class' => 'MySupportBundle:Category',
            'property' => 'name'
        ));
    }

    public function getName() {
        return 'device';
    }

    public function getDefaultOptions(array $options) {
        return array(
            'data_class' => 'My\SupportBundle\Entity\Device',
        );
    }

}

So basically each device has a name and is assign to one category. This works fine. Now when I try to edit the device and change the category, in the choice list the current category is not selected:

 public function editDeviceAction($id, Request $request) {

        $em = $this->getDoctrine()->getEntityManager();
        $device = $em->getRepository('MySupportBundle:Device')->find($id);

        $form = $this->createForm(new DeviceFormType(), $device);

        $valid = false;

        if ('POST' == $request->getMethod()) {
            $form->bindRequest($request);

            if ($form->isValid()) {
                $device = $form->getData();
                $device->setUpdatedAt(new \DateTime('now'));
                $em->flush();

                return $this->redirect($this->generateUrl('view_shop'));
            } else {
                $valid = true;
            }
        } else {
            $valid = true;
        }

        return $this->render('MySupportBundle:Shop:editDevice.html.twig', array(
                    'form' => $form->createView(),
                    'valid' => $valid,
                    'device' => $device
                ));
    }

Any ideas how to set the category as selected?

Device:

<entity name="My\SupportBundle\Entity\Device" table="device">   
        <id name="id" type="integer" column="id">
                <generator strategy="AUTO" />
        </id>

        <many-to-one field="category" target-entity="Category"/>

        <field name="name" column="name" type="string" length="255"/>     
</entity>

Category:

<entity name="My\SupportBundle\Entity\Category" table="category">   
    <id name="id" type="integer" column="id">
            <generator strategy="AUTO" />
    </id>

    <one-to-many field="device" target-entity="Device" mapped-by="Device"/>

    <field name="name" column="name" type="string" length="255"/>              
</entity>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:07:26+00:00Added an answer on June 11, 2026 at 8:07 pm

    Finally I found a solution by myself.

    Obviously when creating form view like this

    {{ form_widget(form.type)}}
    

    and in form type like this

        $builder->add('type', 'entity', array(
            'class' => 'MyBundle:Category',
            'property' => 'name'
        ));
    

    it won’t work. Because “type” is wrong. It has to be “category” instead of “type”. Very strange but this was the problem 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one many-to-many relationship in my model. There are two entities, Emergencies and
There are two JPA entities: User and Order with one-to-many relationship. /** * User
I have two entities, A and B. A has to-Many relation with B. One
My data model contains two entities: Author and Book with a one to many
I have two entities that usually have one-to-many relationship, but in rare cases should
I have two entities TestA and TestB, the relationship is one to many. After
I have two entities, Job and Language , in a many-to-one relationship. The mapping
I have two entities named Category and Article which have a many to many
Scenario: There are two entities say 'Books' and 'Book Reviews'. There can be multiple
Is there a way to use RequestFactory to create two entities in a single

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.