Link tables with extra column in Symfony 2 / Doctrine - doctrine

In a database I have the tables User, Group and UsersInGroup and generate the entities from the database using the Symfony 2 console. The table UsersInGroup is not generated as an entity but is partly generated into User and Group. This would be perfect if UsersInGroup hat only two columns userId and groupId (together primarykey), in this case the table UsersInGroup also contains a third column Role. This field is can never be filled using the generated entities
How should I fill the Role column in the tabel UsersInGroup?

You have to declare your relation table manually and to map the relations.
You'll find an interesting discussion here:
Doctrine2: Best way to handle many-to-many with extra columns in reference table

Related

Join database table based on Json colum having multiple key

I am trying to create an ecommerce application using laravel, So when someone orders something, I store data in orders table, in which a column "products" exists.
I store json object in this column.
this is products json:
[{"pid":65,"min_price":"349.0000"},{"pid":71,"min_price":"349.0000"}]
Now, I want to show these order details to the user profile. So, i need to fetch rows from an another table (products), with the primary key same as "pid" in given json object.
please, suggest me how to resolve it.
You can't do this without retrieving it first and looping over it or something.
What you might want to consider is the following:
Remove the column products from the orders table and create a order_product table. This is called a pivot table.
You can create this table by running the command php artisan create:migration CreateOrderProductTable
Now in this table you define 2 foreign keys. order_id and product_id.
If you have done this, you have to create the relations in the Product model and the Order model.
You can now use this relation to query all products that an order has like this: Order::find(1)->products()->get()

How to select specific columns in a polymorphic relationship where table columns are different?

How can I only select specific columns in a polymorphic relationship because the table columns are different?
$query->with(['Postable' =>function($query){
$query->select('business_title','name ','last_name');
}
The business_title column exists in the agency table. Name and last_name exist in the user table. If I select one table other table gets an error of column not found.
Either develop a naming convention that fulfills your needs (ex: columns like 'name', 'title' can be found on many tables), or do not use select in your query and get all coulmns: $query->with(['Postable']); and you would need appropriate ways to read your query results. Or simply use multiple queries to read from different tables which seems to be the rational option.

Laravel data migration to new structure

We are planning a major database restructure and instead of "Create" all the migrations are "Alter"-ing already existing tables.
For example:
There's table users that holds a column, e.g. role.
After running migrations this column will be dropped and table "Roles" will be created with a hasOne relationship (just an example).
How should I approach converting the old data into the new structure as seamlessly as possible?
It depends on the environment you are working in. for example in production level you can't just move those values from one column to another because the database Is heavily under usage of customers. but for instance you can create a migration to only create the roles table. then create a new migration to copy the corresponding role of each row to the newly created table(roles).
Roles
user_id | role_id
Users
id | role_id
After moving the whole role_id from users table to Roles table then you can push the new code implementation.
I don't really like the idea of removing the role_id column from users table immediately. i would suggest to keep that column for a while till you finish the whole changes related to that column then you can remove it safety without worry about it.

how to make oracle apex wizard steps with multiple related tables?

In Oracle Apex, I have some related tables i.e:
**Person Table**
Id
Name
**Person_Contact Table**
Id
PersonFK
Tel
Address
**Person_Account Table**
Id
PersonFK
BankName
AccountNumber
these are 1-to-1 tables and PersonFK is the Foreign Key.
Now, I want to create a wizard with 3 steps.
how can I do this?
should I define 3 tables with 1-to-1 relation (like above) or create just one big table?
and
what is the best practice in Oracle Apex (in wizard steps forms) for this purpose?
If you are sure that all three tables will remain have common attributes then the option of single table is fine. More detail can be find
here

LINQ: I have 3 tables and I want to make 2 join tables

I have three tables, Question, SubjectType and CoreValue.
Question table has many to many association to SubjectType table and CoreValue table.
I want to make 2 join tables: one between Question and SubjectType and one between Question and CoreValue.
How do I make sure that the Association tables with CoreValue FK and Question FK gets filled without inserting any values in Corevalue? CoreValue Table already have the values that is needed. I just need to be able to have FK on Question and Corevalue in same association table without inserting any data same goes to Question and SubjectType.
Thanks for advice!
Best Regards!
Just create the tables as pure join tables in the database.
EF will generate a model with navigation properties (Question - SubjectTypes, and so on). You probably want to remove the associations SubjectType.Questions and CoreValue.Questions.
See also this tutorial (the Class-Student part).

Resources