Many-to-one relationship mapping using Doctrine 2 in Symfony 2 in YAML

Hello doctrine lovers,

Well come back. In my previous post, we discussed how to map one-to-one relationships. If you have not read it but want to make yourself aware of one-to-one relationship mapping in Doctrine using YAML, make sure you read it before reading this post. So, today I am going to show you how to map many-to-one relationships in both unidirectional and bidirectional ways using Doctrine 2. As you are aware by now I will be using YAML instead of PHP annotations or XML. It is simply because YAML is very easy to understand (though you need to stick with proper indentation, which is not a big deal), will not run into unexpected problems, such as pre-compilers/caching engines tend to ignore comments, which will be used in annotations and few other things. By the way, in Symfony 2, you can use either YAML, Annotations or XML as part of the Doctrine configuration, but use one, not all. Symfony 2 evaluates all these methods equally. At the end of this post, you will learn a technique to evaluate your mapping whether it’s correct or not. So make sure you don’t miss anything in this post.

So back to the topic. Let’s have a quick look at what’s many-to-one relationship is all about. In simple terms, many-to-one is many rows of a table can be relate with only one row of another table. Take a look at the image below,

many-to-one-relationship

So basically, what this tells is, one supplier can have more than one product. For an in instance supplier ‘acer’ can have more than one products such as aspire e1 series laptops, e2 series laptops and so on.

Unidirectional – Many to One

In Unidirectional many-to-one mapping, you will only point to the target table from the parent table (same as in the one-to-one example in my previous post). I call ‘product‘ table as the owning side (parent table) and ‘supplier‘ table as the target table. The reason why I declare parent table to be the owning side is below, Continue reading

Share

One-to-one relationship mapping using Doctrine 2 in Symfony 2 in YAML

Hi doctrine 2 lovers,

As I promised in my previous post, here I came with my very next post, which is the relationship mapping using Doctrine 2. Before going any further make sure that you have read my previous post, which is about table mapping using Doctrine 2.  And I assume you have at least a little bit of knowledge about using Doctrine 2 YAML syntaxes and indentation, which is also highlighted in my previous post.  Ok, enough about my previous post, now let’s get back to the today’s topic 🙂

As you know, or will know in a moment, there are two major types of relationships between tables. Which are,

  1. Unidirectional
  2. Bidirectional

And with above two types, the relationship could be,

  1. One-To-One
  2. One-To-Many
  3. Many-To-One
  4. Many-To-Many

In this post I will be discussing on all eight combinations starting on Unidirectional/Bidirectional relationship types of one-to-one.

Continue reading

Share
Quote

Table Mapping using Doctrine 2, in Symfony 2 in YAML

Hello techs,

I hope you are doing good 🙂  Recently, I have been working on a Symfony 2 project and in Symfony 2, the data layer is managed by using Doctrine ORM. So today, I planned to post an article on how to correctly map an existing data table using Doctrine 2.x object relationship mapping (ORM) techniques.  Later, (in next post) I will be showing how to map a relationship such as one-to-one, one-to-many, many-to-one, unidirectional and bidirectional using the same Doctrine ORM techniques.

Before continuing the Doctrine mapping procedure, we need a pre built table. This table can be any database format. For the sake of easiness I will be using a very simple MySQL data table.  The name of the table is ‘user’ and table looks like this,

 

Column Name Data Type Attributes
id integer primary key, auto increment, not null
first_name string not null
last_name string not null
email string
mobile Integer length 11
date_of_birth date
f_key_1 Integer unique

Continue reading

Share