Nodir Rashidov
Nodir Rashidov

Reputation: 732

Symfony OneToMany persisting data

I am using Symfony3.4 and have the following relationship in the database between House and Type entities:

                  house 
+----------------------------------------------+
| id |     title    |       description        |
+----------------------------------------------+
| 1  | some title 1 |    some description 1    |
+----------------------------------------------+
| 2  | some title 2 |    some description 2    |
+----------------------------------------------+

       type 
+----------------+
| id |   name    |
+----------------+
| 1  | shortstay |
+----------------+
| 2  | rent      |
+----------------+
| 4  | sell      |
+----------------+

                  house_types
+-----------------------------------+
| id | house_id | type_id |  price  |
+-----------------------------------+
| 1  |    1     |   2     | 1000    |
+-----------------------------------+
| 2  |    1     |   3     | 1000000 |
+-----------------------------------+
| 3  |    2     |   1     | 100     |
+-----------------------------------+
| 4  |    2     |   3     | 200000  |
+-----------------------------------+

Here are my entities:

House entity

class House extends EntityBase
{
    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\HouseHasTypes", mappedBy="houses", cascade={"persist","remove"})
     */
    protected $hasTypes;

Type entity

class Type extends EntityBase
{
    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\HouseHasTypes", mappedBy="types", cascade={"persist","remove"})
     */
    protected $hasHouses;

HouseHasTypes entity

class HouseHasTypes extends EntityBase
{
    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\House", cascade={"persist"}, fetch="EAGER")
     * @ORM\JoinColumn(name="house_id", referencedColumnName="id", nullable=true)
     */
    protected $houses;
    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Type", cascade={"persist","remove"}, fetch="EAGER" )
     * @ORM\JoinColumn(name="type_id", referencedColumnName="id",nullable=true)
     */
    protected $types;
    /**
     * @var int
     * 
     * @ORM\Column(name="price", type="integer")
     */
    protected $price;

I did not quite get the difference between ManyToOne and OneToOne relationship. Which one should mine be?

Also, what would be the easiest way to handle inserting new data into the 2 entities (House and HouseHasTypes). Right now I can render the checkboxes for each row in type table as follows:

$builder
...
->add('hasTypes', EntityType::class, array(
                'required' => true,
                'class' => Type::class,
                'expanded' => true,
                'multiple' => true,
            ))

And the in the controller I am planning to make a foreach(){} for each of the checked Types and setHouse() and setPrice() there. Is there any other more efficient way to do this?

How can I render as many textboxes as there are the abovementioned EntityType checkboxes to define a price for each Type selected? I am currently hardcoding three html input text elements and hiding them with javascript based on checked checkboxes. If I make a custom form, how can I tell it to render 3 text inputs (since there're 3 rows in the entity table)

Sorry for asking so many questions. Any help would be much appreciated

Upvotes: 0

Views: 70

Answers (1)

Zahori
Zahori

Reputation: 446

Your database model seems correct.

To define you the OneToMany, ManyToOne and OneToOne relationship, I made you little examples:

Imagine we have two entities: USER and ORDER.

OneToMany:

An user can do 0, 1 or many orders right ? It means that One user can be linked To 0, 1 or Many orders.

USER(id:1) --> ORDER(id:1)
           --> ORDER(id:3)
           --> ORDER(id:8)

ManyToOne:

An order can be done by one and only one user right ? It means that Many orders can be linked To One user.

ORDER(id:1) --> USER(id:1)
ORDER(id:3) --> USER(id:1)
ORDER(id:8) --> USER(id:1)

OneToOne:

Now imagine we have two others entities: USER and EMAIL.

An user can have one and only one email address and an email address can have one an only one user right ? It means that One user can be linked To One email address and One email address can be linked To One user.

USER(id:1) --> EMAIL(id:6)
USER(id:3) --> EMAIL(id:7)
USER(id:8) --> EMAIL(id:1)

Hope I was clear.

For your others questions, Symfony will automatically insert data on form submit if you have correctly defined your getter and setter in entities no needs to do a foreach

Upvotes: 1

Related Questions