How to insert into multiple tables with foreign keys in Joomla? - joomla

I want to know how to handle mysql tables created with constraints in joomla.
for a example,
theater_table
id , name, description, image, address, tel, fax ,email
theater_facility_table
id, theater_id, facility_id
facility_table
id, name, description, image
Facility table already filled with data and id is the primary key. When creating a theater I am adding facilities to it. I created facility and theater JTables.
Do I have to create theater_facility JTable too?
Using theater Model class how I insert data to theater_facility table. I know I can insert data after theater stored successfully creating and calling storeTheaterFacility() method where it contains insert query to save required information. But I feel it can't be a good method to do so. Please help me to solve this.

Depending on how you implemented the theater - facility relationship, you can handle insering new data in different parts of your code. I mean, if for example your JTable class (the one that loads theaters) is loading/saving the theater-facilities relationship too, then the same class should delete it.
May be you can take a look at other components (for example, com_content, which relates an article to a category, or K2, where you can have multiple tags related to multiple "items"(articles)), so you can take a look on how do these components handle these kind of relationships.
Another important point you shouldn't forget is to update your facility model / table to delete records from the relationship table upon facility deleting.
I hope it helped!

Related

Laravel models, database and pivot tables question

Hello I am working with Laravel,
I have to create two simple models, let's say Stores and Books.
Stores can have one or multiple Books and Books can belong to many Stores.
Of course I will use a many to many relationship, with a pivot table.
Books the can have different prices depending the store.
I think a separate table can only complicate things, in my mind the pivot table associating books and stores should have a price column, but pivot tables only contains store_id and book_id.
Should I create a book_prices and associate it with books and to stores? What is the best approach?
You are free and able to set other attributes on your pivot table. You can read more about it in the docs.
https://laravel.com/docs/9.x/eloquent-relationships#retrieving-intermediate-table-columns
You have to define the relationship accordingly, the following should clarify how this works. In this example you use the many-to-many relationship and add the price column to every retrieved pivot model.
public function books()
{
return $this->belongsToMany(Book::class)
->withPivot('price')
}
For example, you are able to access the pivot column in a loop like this
foreach ($shop->books as $book)
{
echo $book->pivot->price;
}
You can define additional columns for your pivot table in the migration for the pivot table, and then when defining the relationship use withPivot to define the additional columns so they come through in the model:
return $this->belongsToMany(Book::class)->withPivot('price');
(Adapted from the Laravel documentation, see https://laravel.com/docs/9.x/eloquent-relationships#retrieving-intermediate-table-columns)
Depends on the complexity of your case, but yes, you have two options for it. Let's say that the pivot table is called as book_store:
Directly adds price column to book_store. This is obviously the simpler option. The drawbacks are:
The history of the price changes isn't logged. You'll have to create another table for logging if you want to keep this history information.
Changes made to price will directly change the price of the related book_store record. Meaning that a price is being updated "live" e.g users cannot update the price now but "publish" it some time later just like this example in the doc.
Create a new, different table to store the price. This may seems relatively more complex, but it may also be more future-proof.
Basically, you get 2 things that you miss in the first option above.
Don't think too much about book_store being a pivot table. One way to see it is like this: book_store IS a pivot table from books and stores tables viewpoints, but it's also just a normal SQL table which could relate to any other tables using any kind of relationships.
If you want to implement this, make sure to create a primary-key in the book_store table.
Alast, it all depends on what you need. Feel free to ask if you need more insight about this. I hope this helps.

Should I create three models or a polymorphic type

I have a Laravel 8 application and am wondering how to solve the problem of how to solve a typical polymorphic issue. I have an Employee model. That Employee can be an ExecutiveEmployee or EntryLevelEmployee. There will be methods an ExecutiveEmployee has that an EntryLevelEmployee doesn't have and the inverse is also true.
Using Laravel 8, is it right to create a base Employee model (without a corresponding table?) and then create two models named ExecutiveEmployee and EntryLevelEmployee that inherit from Employee? This would also imply that both employee types will have two different database tables, even though there will be a lot of overlapping data.
Does it make sense to just have one Employee model and create a migration that has the employee type listed in the model? I am assuming that it's ok if an EntryLevelEmployee has some database attributes which are relevant to it that may or may not be relevant to an ExecutiveEmployee type here, or is that an incorrect assumption?
What's the correct way to model this in Laravel 8? I prefer to keep everything in one table because of how similar the models are. I do have to keep in mind that there will be data that one has that the other doesn't. There will be different accessor methods as well.
Is it possible to have everything in one employees table while utilizing multiple models? Meaning, if I create two models named ExecutiveEmployee and EntryLevelEmployee they would both query the underlying table employees?
UPDATE 1
The more I research, the more I think polymorphism is the incorrect approach here and what I might need is Single-Table Inheritance. This package seems to bring the capability to Eloquent. Would there be a good reason to not use this?
I would use polymorphic relationships in this case, because you are more flexible and have less coupling.
Using the Single Table Inheritance (STI), you can add type specific columns in the employees table and make them nullable. But think about adding/removing types in the future.
executive_employees
id - integer
executive_specific - string
entry_level_employees
id - integer
entry_level_specific - string
employees
id - integer
name - string
email - string
employable_id - integer
employable_type - string
As for the STI the same would be
employees
id - integer
name - string
email - string
type - string
executive_specific - nullable string
entry_level_specific - nullable string
So STI would be suitable when you don't have type specific columns. But you want to add specific behavior in your code. For example a User type (Admin, Author).
Even so, it's a matter of preferences.
It really depends on the state and behavior of your employee object.
Below are few points I will consider to make a decision
If your objects' states/properties are different then definitely you will create different models as your data will be stored in different tables.
If most states/properties are same and some are different, you can
consider storing all in one table/model and for the difference in
behavior create separate table like Ron Van Der Heijden has
suggested and you can consider query scope with that to make
transaction with database.
And another view will be
How many JOINs you will create if you will create different tables,
will that impact the performance and other stuffs, will it make your
code complex?
Can you make simpler relations and handle stuffs independently?
When you are making an API, will your
code make the api overworking? or you need to create too many request
for any operation?
These stuffs will decide how you will make a decision.
Update 1:
Another point I would like to add about the package you are thinking to use, consider using a parent key in table and you can define relationships in a single model.I do not think you need to use a package, you can define it yourself, I guess.
I don't understand why you don't create a simple one-to-many relation. Based on the information you provided, the polymorphic relation looks unnecessary. I think the right way is to create employee_roles table and relations. Then you can give different permissions to different employee types. There are several ways to do that. You can create a middleware to create route restrictions. You can check the role before executing a function in the controller, and run only if the employee has permission. You can use if-else in blade not to render the parts that can't be used by auth user etc.
If you have different “types” of employees, and each employee type should have different logic then yeah, that sounds like a polymorphic relationship.

laravel define relation over multiple tables

I have a table customers with the fields id, name and so on.
One table doctors with the fields id, name.
Then there is one table subject_areas which has all subject areas which a doctor can have. The fields are id, text.
So, each doctor can have multiple subject areas. There is one pivot table doctor_subject which is a belongsToMany relation.
Here is my problem: A customer can have multiple doctors, but only for a specific subject area. I tried it with a new table customer_doctor with the fields id, customer_id and doctor_subject_id. But how do i map this in Eloquent?
Issue was in relation between tables. After chat clarification this came out as solution:
Html form is written in a way that customer first choose doctor, then depending on selection choose several of his available areas.
In that scenario customer needn't to be related to areas directly and should be related to areas only over relation with doctor.
Also as side note, if needed deeper relations, models on pivot tables could be created and used as well.

How to store different type and number of fields in one database table?

Hello everybody I'm making a "Bulletin board", like this: http://stena.kg/ad/post, I'm using Laravel 5.0, and don't know how to store different fields in database table, for example if I choose "Cars" category I should to fill Mark, Model, Fuel (etc fields for cars category), If I choose Flats category I should fill fields like Area, Number of rooms etc...How to organize all of this? I tried some ideas but nothing helped me(
Try to save data as json in table. Parse json format to string and save it in db, but it will cause many problems in future, so not recommend that solution. I recommend to store data in separate tabels, each one for category. For optimise process it is possible to create catregory table, and category_item table with fields like name, description and so on. Different category demands sp=ecific fields, so best solution is to create table per category.

Oracle ADF many-to-many with additional attribute

I'm developing a ADF Fusion Web Application and have some problems with EntityAssociations and ViewLinks.
I have a database table Project with id, name etc. Also I have a table Technology which only contains a id and the name of the technology, for example "ADF".
The relationship between theese two entities is many-to-many. Means one project can have multiple technologies assigned and inverse a technology can be assigned to multiple projects.
This relationship is described trough a join table named Project_Technology. Columns of this table are Project_FK, Technology_FK and Effort. Project_FK and Technology_FK are a composite primary key, Effort is an additional attribute.
Can someone explain me how to map EntityObject and ViewObjects that I can access the Effort, too? "Regular" many-to-many associations aren't that hard to implement but I am really struggling with the additional attribute.
Any help is appreciated. Thanks in advance!
Edit:
I could solve my issues. See answer below for details.
I got it working.
The Problem was the composite primary key on my join tables. It seems like ADF doesn't like them. I put a extra column ID on the join tables and now I can insert values by using the CreateInsert Data Controls.

Resources