1

I am building a new feature onto a Symfony 2.8 application using a few Sonata bundles.

My Page.orm.xml file contains the following:

    <one-to-many target-entity="AppBundle\Entity\Synonym" field="equivalents" mapped-by="page">
        <cascade>
            <cascade-all/>
        </cascade>
    </one-to-many>

... and my Synonym.php entity definition contains the following:

/**
 * @var \Application\Sonata\PageBundle\Entity\Page
 * @ORM\ManyToOne(targetEntity="Application\Sonata\PageBundle\Entity\Page", inversedBy="equivalents", cascade={"persist"})
 */
private $page;

... and my PageAdmin.php file contains the following:

            ->add('equivalents', 'sonata_type_collection', array(
                'label' => "Equivalents",
                'cascade_validation' => true,
                'required' => false,
            ), array(
                'edit' => 'inline',
                'inline' => 'table',
                'targetEntity' => 'AppBundle\Entity\Synonym',
                'admin_code' => 'app.admin.synonym',
            ))

... and when I try to load a page admin screen, I get this error:

Notice: Undefined index: joinTable

So my question is: What do I need to add in order to make Symfony happy with this relationship?

Edit #1: Don't ask why I'm using "equivalents" rather than "synonyms." It's a long and strange story that's not worth getting into here.

Edit #2: For what it's worth, I would argue that this is not a duplicate question, since I am looking for a way to develop within the context of an existing vendor library. For example, "edit the core Doctrine functionality to avoid this index" would solve the linked problem, but it would not solve my problem, since I need to work within the constraints of an existing system.

  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Scriptman Feb 14 '18 at 09:08
  • I would argue that this is a Symfony/Doctrine question rather than a general PHP question like the one you linked, and that an answer specifically related to why Symfony/Doctrine is sending this notice would be helpful to other users. That said, I appreciated your fast response. – Mayor of the Plattenbaus Feb 14 '18 at 09:11
  • 1
    True that, but I can't help you with Symfony. Have a nice day :) – Scriptman Feb 14 '18 at 09:13
  • Can you define `@ORM\JoinTable(name="page_id", , referencedColumnName="id")` under `@ORM\ManyToOne` in your `Synonym.php` class for the field `page`. Do you still have your error? – Preciel Feb 14 '18 at 09:18
  • What is the output of this command `php bin/console doctrine:schema:validate`? – etudor Feb 14 '18 at 09:18
  • Also, I think you are missing this line from the definition of $page `* @JoinColumn(name="page_id", referencedColumnName="id") ` – etudor Feb 14 '18 at 09:23

1 Answers1

2

There you have it... ;)

You should define @ORM\JoinTable

/**
 * @var \Application\Sonata\PageBundle\Entity\Page
 * @ORM\ManyToOne(targetEntity="Application\Sonata\PageBundle\Entity\Page", inversedBy="equivalents", cascade={"persist"})
 * @ORM\JoinTable(name="page_id", referencedColumnName="id")
 */
private $page;

Do you still have your error?

Preciel
  • 2,666
  • 3
  • 20
  • 45