symfony2 one-to-one inverse always return the same result

108 views Asked by At

I have two tables linked by a 1::1 relation:

UPDATE1 Here is the controller :

public function ajaxSearchAction(Request $request)
{
    if (! $request->isXmlHttpRequest()) {
        throw new NotFoundHttpException();
    }

    $em = $this->getDoctrine()->getManager();
    $sc = $this->get('security.context');
    $bh = $this->get('helper.behavior');

    $form = $this->createForm(new ContactSearchType($em, $sc, $bh), new ContactSearch());
    $form->handleRequest($request);

    $contacts = null;
    if ($form->isValid()) {
        $contacts = $em
            ->getRepository('CoreBundle:Contact')
            ->getSearchResult($form->getData());
    }

    return $this->render('CoreBundle:Contact:searchResults.html.twig', [
        'contacts' => $contacts,
    ]);
}

Its to display a result from a search Form. The form is typical (a proxy entity ContactSearch that map all fields needed for the search, and a form ContactSearchType)


UPDATE2 a more simple controller :

$contacts = $this->getDoctrine()
        ->getRepository('CoreBundle:Contact')
        ->findBy([], ['name' => 'ASC'], 10);

foreach ($contacts as $contact) {
    var_dump($contact->getAddress()->getCity());
}

The var_dump give me the same city for all contact But when i Check the debugger and look for the queries played by Doctrine, i see:

  • on query on the Contact table
  • 10 more queries for retrieving the City, and those queries are well different (returning a different city for each contact)

The only solution i found for now, is to make a join on Address table and return an array of result:

$qb = $this->createQueryBuilder('c');
$qb ->select('c, a')
    ->join('c.ctcAdr', 'a');
$qb ->getQuery()->getArrayResult();

and then it works.


Contact

/**
 * @var Address
 * @ORM\OneToOne(targetEntity="MyBundle\Entity\Address", mappedBy="contact")
 */
private $address;

/**
 * Get address
 *
 * @return Address
 */
public function getAddress()
{
    return $this->address;
}

/**
 * Set address
 *
 * @param Address $address
 * @return Contact
 */
public function setAddress(Address $address)
{
    $this->address = $address;
    $address->setContact($this);

    return $this;
}

Address

/**
 * @var Contact
 *
 * @ORM\OneToOne(targetEntity="MyBundle\Entity\Contact", inversedBy="address", cascade={"persist"})
 * @ORM\JoinColumn(name="C_ID", referencedColumnName="C_ID")
 */
private $contact;

/**
 * Set contact
 *
 * @param Contact $contact
 * @return Contact
 */
public function setContact(Contact $contact)
{
    $this->contact = $contact;

    return $this;
}

/**
 * Get contact
 *
 * @return Contact
 */
public function getContact()
{
    return $this->contact;
}

In my ContactRepository, I have this simple Query Builder :

ContactRepository

$qb = $this->createQueryBuilder('c');
$qb ->orderBy('c.Name')
    ->setMaxResults(10);

return $qb->getQuery()->getResult();

In my database, all those ten contact results have a different address (City, Postal code, etc ...)

Twig template

{% for contact in contacts %}
    <div>
        - {{ contact.name }} : {{ contact.address.city }}
    </div>
{% endfor %}

But When I want to display them, all the 10 contacts displays the same city (belonging to the first results of the Contacts array

  • Contact 1 : New York
  • Contact 2 : New York
  • ...
  • Contact 10 : New York

If I change the first result :

$qb = $this->createQueryBuilder('c');
$qb ->orderBy('c.Name')
    ->setMaxResults(10)
    ->setFirstResult(151);

I get something like :

  • Contact 151 : Los Angeles
  • Contact 152 : Los Angeles
  • ...
  • Contact 160 : Los Angeles

What am I doing wrong?

Thanks.

0

There are 0 answers