0

I am experiencing a problem, and will be appreciate for help from the experts.

I have entities:

Product

// src/Acme/DemoBundle/Entity/Product.php

/**
 * @ORM\Entity
 * @ORM\Table(name="products")
 */
class Product
{
   ...
   /**
     * @ORM\Column(name="file_uuid", unique=true, nullable=true)
     * @ORM\OneToOne(targetEntity="Acme\DemoBundle\Entity\File")
     * @ORM\JoinColumn(name="file_uuid", referencedColumnName="uuid")
     *
     */
    protected $file;

    /**
     * @ORM\OneToMany(targetEntity="Acme\DemoBundle\Entity\Image", mappedBy="product")
     *
     * @var ArrayCollection Collection of Image entities
     */
    protected $images;
    ...
}

File

// src/Acme/DemoBundle/Entity/File.php

/**
 * @ORM\Entity
 * @ORM\Table(name="product_files")
 */
class File
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    protected $id;

    /**
     * @ORM\Column(name="uuid", type="string", length=64)
     */
    protected $uuid;

    /**
     * @ORM\Column(name="product_id")
     */
    protected $product;

    ...
}

Image

// src/Acme/DemoBundle/Entity/Image.php

/**
 * @ORM\Entity
 * @ORM\Table(name="product_images")
 */
class Image extends File
{

    /**
     * @ORM\ManyToOne(targetEntity="Acme\DemoBundle\Entity\Product", inversedBy="images")
     * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
     */
    protected $product;

    /**
     * @ORM\Column(name="extension", type="string")
     */
    protected $extension;

    ...
}

I have my sql correct:

product_files
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| uuid        | varchar(64)  | NO   |     | NULL    |                |
| product_id  | varchar(255) | NO   |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+


product_images
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| uuid        | varchar(64)  | NO   |     | NULL    |                |
| product_id  | varchar(255) | NO   |     | NULL    |                |
| extension   | varchar(64)  | NO   |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

But when I run my app, I've got an error:

An exception has been thrown during the rendering of a template ("Notice: Undefined index: product in .../vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1575") in AcmeDemoBundle:Product:edit.html.twig at line 24.

Where am I wrong?

In edit.html.twig I just trying to display product images.

I can avoid this issue removing from the File entity annotation for $product:

@ORM\Column(name="product_id")

But then, after doctrine:schema:update I loose my product_id field in product_images table.

And what I want:

  • to have one file per one product
  • to have multiple images per one product
7
  • your product field in product_file should be a reverse OneToOne to the product_id not a column, see : docs.doctrine-project.org/en/2.0.x/reference/…
    – Snroki
    Jul 15, 2013 at 13:42
  • @Coussinsky the case is I want to have this column as well. Because I want to find a file by product_id. Anyway. If I do your changes: File /** * @ORM\OneToOne(targetEntity="Acme\DemoBundle\Entity\Product", mappedBy="file") / protected $product; and Product /* * @ORM\Column(name="file_uuid") * @ORM\OneToOne(targetEntity="Acme\DemoBundle\Entity\File", inversedBy="product") * @ORM\JoinColumn(name="file_uuid", referencedColumnName="uuid") */ protected $file; I lose product_id field in both tables. Jul 15, 2013 at 14:04
  • when you're in a relation between two entities you must not have the @ORM\Column annotation. Doctrine will handle it by itself. Just add the annotations for the relation, update your schema and that's it !
    – Snroki
    Jul 15, 2013 at 15:17
  • @Coussinsky I've added as you said. But now doctrine:schema:update deleted the 'product_id' from the 'product_images' Jul 15, 2013 at 15:36
  • why does your image entity extend file? You've a OneToOne between product and file, and then a ManyToOne between product and image which extend file. I'm not sure but it seem strange.
    – Snroki
    Jul 15, 2013 at 15:50

1 Answer 1

0

Okay, I got it.

The issue is that it is not possible to override relationship because I am trying to denormalize my DB.

So for implementing what I want doctrine gives me 3 correct ways to do it:

  • create a superclass which is not an Entity, with all properties I need and then create Entities extends from it with describing all relationship what I need;
  • create a 'single table' mapping. When you can use 1 table for multiple entities with discriminator column which will determine what entity should be loaded;
  • and 'mapped superclass', where you will have one base table with basic fields set and extended table with only extended fields and one extra field id which will refers to the base table;

Hope someone will avoid time spent on finding out the same thing.

Links:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.