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 8758243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:29:40+00:00 2026-06-13T14:29:40+00:00

I have a ‘person’ entity that has a manyToOne relationship with a ‘city’ entity:

  • 0

I have a ‘person’ entity that has a manyToOne relationship with a ‘city’ entity:

Here’s the person’s relationship mapping:

/**
* @ORM\ManyToOne(targetEntity="City", inversedBy="persons", cascade={"persist"})
*/
private $city;

and city’s:

/**
* @ORM\OneToMany(targetEntity="Person", mappedBy="city")
*/
private $persons;

I use the basic Symfony 2 ‘person’ controller generated by doctrine:generate:crud:

$entity  = new Person();
$form = $this->createForm(new PersonType(), $entity);
$form->bind($request);

if ($form->isValid()) {

        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();
}

The PersonType form type instantiates a CityType

class PersonType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
        $builder
        ->add('city', new CityType())
        ->add('firstname')
        ->add('lastname')
        ->add('gender')
        ...

The CityType form type has a single text field

class CityType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
        ->add('cityname', 'text');
     }
}

When I submit the form, the Person entity is created, as well as the City entity, thanks to the cascade={“persist”}.

The problem arrises when I fill out the form with a city name that already exists in the DB, Doctrine creates another entry with the same city name.

Is there a simple way Doctrine could detect that, and use the existing city_id in the Person table?

  • 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-13T14:29:41+00:00Added an answer on June 13, 2026 at 2:29 pm

    As @vadim said, you must use datatransformer. It might be confusing at start but after ~30 minutes you will learn it. Keep in mind that around the world, you will have many cities with same name.

    The best solution is this; in that text field, user would type a name like Belgrade, Montana, Usa (btw; Belgrade is capital of Serbia, just giving you an example of cities with same name). Datatransformer will read DB and find the correct city of Belgrade that you could link with.

    After you do this (and it is very simple), that you could build auto-completion for that field. @artworkad suggestion is overly complicated and will bring more problems later.

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

Sidebar

Related Questions

Have a simple form that has a PictureBox in one location. I want to
Have data that has this kind of structure. Will be in ascending order by
Have data that has this kind of structure: $input = [ { animal: 'cat',
Have an issue with marshall and unmarshall readers and writers. So here it is.
Have a form here with bunch of input text fields and a file upload
I've got a string that has curly quotes in it. I'd like to replace
Have a look at one of my websites: moskah.com The problem is that it
Have a simple contact us XPage created. Have server side validation in place that
Have a network location that shows paths in the 8.3 short format. I need
Have searched for the answer but no joy, so here goes... I'm working on

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.