I'm developing a site like "yellow pages" with more complex DB than I had ever experienced before.
For example:
This DB has 3 tables:
1. organisations – with some general information.
2. services - with list of all services that can be provided.
3. relation table.
Organisations table structure:
id name city phone etc.
1 CompanyName Farewell 987-65-43
Services table structure:
id ServiceType
1 purchase
-----
2 sale
-----
3 exchange
etc..
you get the point
And relation table structure:
id OrganisationID ServId
1 1 2
----
2 1 3
----
3 1 6
----
4 2 3
In website output I need a list of all organisations with all the services they provide.
I can't fully understand (even after reading Linking Models Together section in the CookBook) how I should combine all this tables using Cake's syntax and conventions.
I'll be glad if anyone can provide me a Cake's model/model's (how many I need?) example with ability to retrieve this data.
I can't comment on your question so I'll ask here...
Organisations can have many services and services can have many organisations? Is it HABTM relationship? (Just checking...)
If it is, I'll then edit this and try to give you the answer. :)
EDIT:
TABLES
1. table: organisations
2. table: services
3. table: organisations_services (sorted alphabetically)
1. table: id, name, city...
2. table: id, service_type
3. table: id, organisation_id, service_id (singular, sorted alphabetically)
MODELS
Organisation - inside your organisation model you must have:
var $hasAndBelongsToMany = array('Service');
Service - inside your service model you must have:
var $hasAndBelongsToMany = array('Organisation');
And that's all. Everything else leave to the beautiful Cake. :) If you don't understand something, feel free to ask and here you can get more information.
Related
I am new to laravel and using laravel 8. I have following tables and I am not able to understand how to establish eloquent relation between them.
Table - Fabrics
id
user_id
name
1
1
fabric 1
2
1
fabric 2
Table - Mesh
id
name
1
mesh 1
2
mesh 2
Table - Designer
id
user_id
mesh_id
fabric_id
name
1
1
1
1
designer 1
2
1
1
2
designer 2
I have done all the basic relations. Each designer has one mesh and one fabric. there is a unique combination of fabric_id and mesh_id in designer table meaning one fabric and one mesh can not have more than one row. now I want the list of fabrics with the sublist of meshes with which designers are created. basically from the designers table instead of
with(designers) I want with(meshes). Please help. Thank you in advance.
Looks like a many to many relationship problem. See this documentation
There is a good example on many to many relationship.
You have a many to many relationship between fabrics and mesh table with designer table being the intermediate table linking both tables together.
Therefore you should already have meshes relationship in your fabrics table using belongsToMany(Mesh::class) and have fabrics relationship in your mesh table using belongToMany(Fabrics::class).
You can retrieve all the fabrics and its related meshes through its relationship.
$fabrics = Fabrics::get()
foreach($fabrics as $fabric) {
$fabric->meshes
}
Information:
A sound company has many employees.
The employee has many positions in the company.
The company has many events. And for every event, they need a crew.
A crew is composed of many employees holding certain positions. An employee can hold multiple positions in a crew.
For example:
The list of employees is:
Employee 1 is a Driver, Sound Engineer and Stage Hand
Employee 2 is a Driver, Sound Engineer
Employee 3 is a Sound Engineer and Stage Hand
Employee 4 is a Stage Hand
Employee 5 is a Stage Hand
The event is called: Event 1
The crew is:
For the position of Sound engineer:
Employee 1
Employee 2
For the position of Stage Hand:
Employee 3
Employee 4
Employee 5
For the position of Driver:
Employee 2
Employee 1
Problem:
I believe this is done by using a pivot table that holds the event_id, employee_id and position_id
But when I follow this approach, I get stuck on feeding the data and the methods to use to create new data.
Is there a different approach?
you need two pivot tables. One for employee and position which hold employee id and position id. Another one between employee and event which holds event id and employee id. I think this is a better way to handle this. You can use the attach and detach method handling pivot table in laravel.
I use MySQL and trying to write a PHP script for my school project.
There is one table named lessons contains this columns:
-id
-lessonid.
-studentid
I also have two different tables for notes and announcements
announcements and notes tables contains these columns:
-id
-lessonid
-content
-createdtime
I need to order both announcements and notes from latest to oldest by createdtime but also need to show all lessons a student takes.
For example: A students takes maths and physics lessons. I need to display him/her both notes and announcements for both of physics and maths and all items should be ordered by date. (like a timeline.) And of course I will not show him/her the notes and announcements for chemistry lesson. Also it will be good if I can say it is note or announcement on the list.
Can you help me to write SQL and PHP code for that?
Thanks.
EDIT: This is where I have stuck:
I have combined two tables and ordered them by date. But can't combine them with the lessons a student take.
SELECT title, created, lessonid FROM (SELECT title, created, lessonid FROM notes UNION SELECT title, created, lessonid FROM announcements) as a ORDER BY created DESC
First of all, thanks for letting us know that this is for a school project - therefore I won't give you the answer. If it is in the project then your teacher should have given you the concepts to come up with a solution.
Your question is well put together and I can see how to solve it but ... It's your project so you need to have a crack at it and post what you come up with.
I will give you some hints to get you started.
You need a query to combine the announcements and notes table. Then you need to group the data by the lesson and join that to the students. This is all basic SQL.
Good luck. Post what you come up with.
I'll also, follow fellow posters advice, and not do the legwork for you. but won't let you go empty handed, so will give you the concept.
there is a thing called third normal form, we decide how many tables according to that concept, so if its a big database then separate table for first name and separate for last name, as many people share those among themselves, so saves space and redundancy etc. so one table for person has personid as primary, and has lastname foreign key to refer to last name table , we generally name it lastNameRef, similarly firstNameRef. so now, each person has lot of classes, and each class has lot of persons(students) in it. so this is a many-many relation - we create a allreference table to solve this many to many problem. so there is one table for classes which has class id as primary key, so now u create a all reference table which a recordId as primarykey, (just for namesake) and personRef(refers to personId in person table) and classref(refers to classId in class table) if one person has two classes, another entry with same personId but different class Id, at the end, you can query the name of person from person table, and name of class from class table and create join on their foreign keys but use all three tables, result is (JOHN MATH, JOHN SCIENCE) etc, same way you display all notes for john searching name in person table, and subject in class table,and notes in notes table
I am not necessarily looking for MySQL or PHP code. Rather I'm trying to get a concept of how to set everything up.
I want to create a database using MySQL (and using PHP to update it) of all the books my family owns. I want to set up different 'bookshelves' for each person in my family so we can see who has a certain book.
My first thought was to have a table for all the titles, authors, etc and have a field for user id to show who had the book. However, I might have a copy of Hunger Games and my grandmother might have a copy of Hunger Games. I want to be able to show it on both bookshelves. The only way my idea would work is if we had no duplicate books.
My next idea was to use a different table for each user and have a field that contains the book id for each book the user owns. I think this would work on a small scale but it does not seem like an efficient design. I am planning on making the database public for everyone in my town to use (thousands of people) once I get a stable website going so I want to start off with the right kind of design.
How should this be designed?
BOOK
--------
book_id
title
other_book_related_info
PERSON
-------
person_id
name
other_person_info
BOOK_PERSON
-------------
book_id
person_id
possibly-dates-when-this-person-owned-this-book
Here is one simple solution i can think of:
Book Table : List of all unique books
User Books : contains the user id and the book id. multiple users can own the same title.
Users : List of users;
This is pretty basic. Owner, book and author should be self explanatory. Add any additional fields to those tables you want. The bookshelf and book_authors are both cross reference tables so each book can have multiple owners and each book can have multiple authors.
**owner:**
owner_id
owner_name
...
**book:**
book_id
book_name
...
**author:**
author_id
author_name
...
**bookshelf:**
owner_id
book_id
**book_authors:**
book_id
author_id
You might like to differentiate between ownership of the book and current possession, since people will doubtless be borrowing. So the tables of BOOK (best call it ITEM if you're going to expand to DVD's etc) and PERSON, and the ownership table BOOK/MEDIA_OWNER, might be usefully accompanied by an ITEM_LOAN table.
You might like to also allow grouping of sets of items so that multiple volumes of a book, or discs of a show season, can be identified individually. Books (and films etc) also come in series, so think about how to represent that as well.
By the way, it's a generally accepted rule that if an edition of a work changes by more than 20% between print runs then it is a new impression, but it is not always granted a new ISBN. Depends on the publisher. Also, the hierarchy for books is based on Work -< Edition -< Impression, and these folks would be a good source of information of data structures relating to books.
Here's another solution:
**** BOOK ***
book_id
book_title
book_desc
book_bought
*** USERS ***
user_id,
name,
dateOfBirth
** Copies **
copy_id (PK)
user_id (FK)
book_id (FK)
NoOfCopies
I'm trying to build a searchable database of acronyms and their definitions that are specific to a certain industry. It has been years since I've done any real programming, so I'm a little behind the learning curve.
I'm writing the code in PHP, and I'm using MySQL as the database. If this can be done easier in Postgres, I'm not opposed to switching DBs, but I can't use Oracle or any other commercial system.
So here's the question:
I'd like to set it up so that each acronym can: (1) apply to 1, multiple, or no specific organizations; (2) have 1 or more associated definitions.
The complexity--at least in my mind :D--comes in that it is conceivable that some organizations might have a single acronym with multiple definitions that all relate to that one organization. At the same time, the acronym may have 1 or more definitions that relate to OTHER organizations as well.
Am I over complicating this?
I'd like to better understand how to setup the table structure and relationships in MySQL--what fields and relationships would be in each table.
A SQL statement would be helpful if anyone feels so inclined, but I'm hoping to at least get a solid grasp on the database schema so I can get the tables created and some sample data imported.
Many, many thanks to all...
Dan
The solution should contain 4 tables: Acronyms, Definitions, Organizations, and AcronymOrganization.
Acronym(id, acronym, definition_id)
Definitions(id, definition)
Organizations(id, organization)
AcronymOrganization(id, acronym_id, organization_id)
If I understand your question, you can use three separate tables. First, have the table of acronyms/definitions, then have a table of Organizations. Finally, have an AcronymOrganization table, that just references a key from the acronym table, and a key from the organization table. This way, you can have as many acronyms for an organization as you please.
After you set up the database, you need to use a couple inner joins to join the three tables, collecting only the acronyms for the appropriate organization ID.
I'd just create an acronym table, an organization table, and a definition table. Put two foreign keys in the definition table: one for the entry in the acronym table, and the other for the entry in the organization table.
If you want to have a n:m relationship between tableA and tableB, then you need a third table.
table A. Fields : ID,name
table B. Fields : ID,name
table AB. Fields : A,B (A is a reference to A.ID, B is a reference to B.ID)
[TABLEA]1-----*[TABLE_AB]*-----1[TABLEB]
Example
Contents of table a:
ID Name
1 John
2 Mary
3 Piet
Contents of Table b:
ID Name
1 Microsoft
2 Google
3 Philips
Contents of Table ab:
ID Name
1 2
1 3
2 2
3 1
3 3
Then select everything like this:
select a.name,b.name
from a,b,ab
where a.id=ab.a and b.id=ab.b
Result:
a.name b.name
John Google
John Philips
Mary Google
Piet Microsoft
Piet Philips