let's say that i have 3 tables:
books
properties
book_properties
of course, i would like that when i want to insert new book (or update existing), to fill the form.
fields that exist on form have to be defined as records in table properties.
when i fill up those fields, data has to be saved in table book_properties.
can you help me by giving some advices and references, how to achieve that?
thank you very much in advance!
You will need to create 3 tables: books (id, author, title, etc.), properties (id, name) and books_properties (id, book_id, property_id, property_value). Notice that the third table must be named books_properties, not book_property. Then you will need to create models for the books and properties tables. See the manual here. Create correct assosiations. Books HasAndBelongsToMany Properties. If you use cake console it will be easier to create models, controllers and views.
Related
I am working with Laravel 8 and i have these database tables structure
users table structure
id and name
articles table structure
id, title,content, and authors
the models are
Article and User
I store the users ids in authors field in articles table as following format
1,6,9 for example
but unfortunately I stucked how I can retrieve all articles row data with it's authors id and name and get confused how to write the query
First you need to have this in create articles migration.
$table->bigInteger('authors');
$table->foreign('authors')->references('id')->on('users');
Second you need to have this code in User Model.
public function articles() {
return $this->belongsTo( User::class, 'authors' );
}
and then access user's articles like this.
$user->articles
I have this scenario where I have three entities Book, Review, comment and have the following tables:
Books (Id, ReviewCounts)
BookTranslations (BookId, LanguageId, IsDefault, Title, Description)
Reviews (Id, BookId, CommentCounts)
ReviewTranslations (ReviewId, LanguageId, IsDefault, Content)
Comments (Id, ReviewId, Content)
Now, I want to add two other entities to the design, Vote and Report. The vote entity represent a vote the user do on all of the previous three entities, Book, Comment, Review. And the report, where the user should be able to report fake Books, inaccurate Reviews, hate and illegal Comments and fake User profiles.
The problem is, I need now to add three tables for the vote BookVotes,CommentVotes, and ReviewVotes. And four different tables for the report UserProfileReports, BookReports, ReviewReports, CommentReports.
Now, I think about adding one base table so to speak, called Entities, and another table EntityTypes, where EntityTypes contains data like "Book,Review,Comment". And the same primary key will be in Entity and Review or Entity and Book and so on. This way I will solve the vote issue, but not the report because the user can report a user profile which is a User entity. It doesn't make any sense to add the User entity to the list of the three aforementioned entities and "derive" it from the Entities "base" table too.
To be honest, I don't think the usage of one "base" table Entities make sense either, because conceptually the book, review, and comment are three totally different entities.
So, my question is, should or shouldn't I go with the "base" table solution, and if so, how should I solve the report problem?
I would NOT go with the single "base" table idea. Since a single vote can only be about either a book or a comment or a review, but not about multiple items, it makes more sense to me to keep them in separate tables. Same for reports.
Databases with lots of small tables tend to perform better than databases with just a few big tables, so unless there's a modelling reason to combine the entities, I wouldn't do it.
I have a database with a table which named Category, different category contains different articles, but as time goes by, only Category is not enough for distinguish articles on website, so... to meets our demands, we prepare to create new tables.
our category table like this:
Category
-------------
id
name
description
image
we want to create new table(more than one) like this:
Topic
--------------
id
name
description
image
icon(.svg)
display(boolean)
new tables are just like Category table, but add more one or two column.
In this situation, what choice is better for Content Management and server/AWS-RDS query efficacy? (or can't get both?)
create new table
add a column which like Class(class1 = category, class2 = topic, class3 =...) to redefine existing content?
or other suggestions?
It makes more sense to create a new table because these are 2 separate concepts in your domain logic. Also, it is better for query performance and you get more flexibility because, if you need to, you can easily add columns to each table independently.
I have a newb database question. I have a list of publications, and I want to create a database. I had been thinking I would do:
UID, Author, URL, Title, Publication, start page, end page, volume, year
for the columns in my database,but then I saw that some publications have numerous authors, and I'm not sure how to handle that. What should I do?
The normal action would be to create a separate table for the authors and then create a separate table, where you store the primary keys for author and books, take a look at the following example (Fourth Normal Form):
Database Normalization
Abstract extra simplified example of issue:
DB structure is (simple version):
orders {
id,
name
}
items {
id,
name
}
items_orders {
order_id,
item_id,
quantity # problem item - many to many tables don't usually have this can't get cake to upport
}
So I have controllers and models for orders and items, they both habtm each other via the joinTable items_orders with the foreign keys specified, and I can load an order and all it's items, or an item and all the orders it is in, and save them, etc, however I can't figure out how to save/recover the quantity.
I am assuming I'll need to create an extra model to handle this, but can't find any documentation or similar posts here that explain how, or if there's an easy shortcut?
Using extra fields in join table is easy and explained in the cookbook.
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany-through-the-join-model