skip to Main Content
+971 55 881 1725 hmimthiaz @ imthi.com

Symfony Doctrine Many to Many Relationship (ManyToMany)

Many to many relations are one of the mostly used relations in the relational database. It took a while for me to figure how to properly configure this relation in Symfony Doctrine. I am sharing this so it might be useful for someone.

Used very simple example students and course. A student can opt for multiple courses and a single course can be opted by multiple students.

And Entity class should be like this…

The above entities will create the following SQL and it will also create the cross-reference table.

Hope this helps 😉

Install MySQL 5.6 and phpMyAdmin on Ubuntu 16.04

To Install MySQL 5.6 and phpMyAdmin on Ubuntu just follow the command one by one bellow.

For phpMyAdmin to work, you need PHP to be installed. To install PHP 5.6 😉

 

MySQL ORDER BY FIELD – Custom Field Sorting

Today I ran into a problem. I need to sort results of a given query by fixed column values.

Example

I have a column named status and which holds the following values 1,2,3,4

When I user normal sort method like

Order By status ASC will return 1,2,3,4

Order By status DESC will return 4,3,2,1

Suppose If I need the result to be sorted in a particular Order 3,2,4,1 then I ran into problem. Both method cannot solve my problem.

Then Googled and I found a thread in MySQL forum where Papalagi Pakeha had the same problem.

http://lists.mysql.com/mysql/209777

For that Johan Höök replied with a solution

http://lists.mysql.com/mysql/209784

Where I can user Order By Field function to get the needed result. So my query will look something like

Order By Field(status,3,2,4,1) which does the magic. I searched the whole MySQL documentation but I am unable to find the proper documentation. Who cares it works for me 😉

Back To Top