CakePHP - Second Level Model Association Naming Convention - cakephp

I have Project, ProjectImage, and ProjectImageCategory.
a Project hasmany ProjectImage and a ProjectImage belongs to a Project
a ProjectImageCategory hasmany ProjectImage and a ProjectImage belongs to a ProjectImage
How should I name the model classes respectively the database tables and foreign keys, so that CakePHP would bind them.
Thanks

Project model use projects table and ProjectImageCategory use project_image_categories table and ProjectImage use project_images table, right?
the FK is ta name of the model with slug +_id , for example Project , to use a FK with this model you need set project_id , if project hasMany ProjectImage, in your project_images table you need a project_id column.

Related

How to implement many-to-many relationship with shared foreign keys?

I am struggling with modelling my database so that entities can only have a many-to-many relationship if they share the same "parent" entity.
Here's an example:
Folders have a one-to-many relationship with Presentations.
Folders have a one-to-many relationship with Photos.
Presentations and Photos have a many-to-many relationship.
Is there a way of enforcing that Presentations and Photos are only related if they belong to the same Folder - that they share the same foreign key?
Folder holds the many-to-many relationship - related presentations and photos share the same folder, so:
create table folder (
id int,
-- other columns
);
create table presentation (
id int,
folder_id int not null references folder,
-- other columns
);
create table photo (
id int,
folder_id int not null references folder,
-- other columns
);
Join presentations and photos using folder_id.
As an example, to find photos related to a presentation:
select ph.*
from presentation pr
join photo ph on ph.folder_id = pr.folder_id
where pr.id = ?

I want to create one common login table for different tables like Patient , Doctors, receptionist but not sure how to do it

I am using ASP.net MVC.
my question is that I have 2 tables patient and doctors and I want to create one common Userlogin table for both this table. So when I store username and password in UserLogin table it can determine whether it is Patient username or password or Doctor.
Can anyone please help me what should do? And give me some more idea about how and what I should change.
Doctors and patients are both subclasses of a more generic class, Users. You may want to research a topic called "Class Table Inheritance" There is a tag with that name here in Stackoverflow, and there are many articles on this topic on the web.
Class Table Inheritance is one way of making up for the fact that the relational model does not have a built in mechanism for inheritance.
Briefly, Your userlogin table needs a primary key, and username may not be suitable for this purpose. Call this key, UserId. Doctors and Patients do not need a unique id of their own. Instead, you can include UserId in both the doctor table and the patient table. This column should be the primary key of the doctor and patient table. In addition, this field should be named as a foreign key that references UserID in the UserLogin table.
The UserLogin table should probably include columns (fields) for all the features that are common to both doctors and patients, such as first name, last name, etc.
When you want to know if a user is a doctor or not, just join UserLogin and Doctor. Non doctors will drop out of the join.
Step-1. Create RoleMaster Table and add patient , doctors , receptionist as roleName in rolemaster table.
ColumnName Datatype
RoleId int
RoleName nvarchar(50)
Status bit
IsDeleted bit
CreatedDate datetime
step-2 Add RoleId As Reference in userLogin table and profileTable.

How do I create a relationship between two tables where one already has a relationship with another table?

I am trying to create a Many to Many relationship with SQL Server Management studio between two tables called Courses and Students. This relationship uses a junction table called Enrollment. I started by creating a 1:M relationship between Students and Enrollment so that the studentId column in the Enrollment table points to the studentId column in the Student table. That worked fine. My problem is occurring while I create my 1:M relationship between Courses and Enrollment. The courseId column in the Enrollment table needs to point to the courseId column in the Courses table. The relationship dialogue comes up and the columns under the Primary Key Table(Enrollment are automatically populated with courseId and studentId. The Courses table doesn't have a studentId column, so I remove this from the columns under the primary key table. This is where I get the error message 'The columns in table Enrollment do not match an existing primary key or UNIQUE value. This method worked fine for creating the 1:M between Students and Enrollment. Why am I getting this error all of a sudden?
The reason is your Courses table has 2 field in it's primary key.
On solution is you add a new field named semesterId in Enrollment table and use both semesterId and courseId when creating foreign key.
Your Courses primary key includes a SemesterID which isn't in the Enrollment table. I'd suggest the SemesterID should be in the Enrollments table rather than in the Courses table as a Student would be enrolled in a course in a particular Semester.

how to configure Model Options

I want to reverse engineering a DB I create for the CakePHP framework.
So the naming convention is this one:
Tables are always named in plural.
If users belongsTo or belongsToMany users, relationships are declared like:
users owners
id
owner_id --> id
If sites hasAndBelongsToMany users, relationships are declared like
sites sites_users users
id
id <-- site_id
user_id --> id
So I would like to configure Model Options in MySQLworkbench in order to create diagram.
So I tried to set the following options but it doesn't work:
Column Defaults:
PK Column name: id PK Column Type: INT
Column name: %col Column Type: VARCHAR(45)
Foreign Key/Relationship Defaults:
FK name: %dtable%_id Column name: %dtable%_id
ON UPDATE: NO ACTION ON DELETE: NO ACTION
Associative Column name: %stable%s_%dtable%s
Is it the right way to do what I want?

How to write custom insertQuery for HABTM( HasAndBelongsToMany ) relationship in Cakephp?

In my Cakephp application I have define HABTM relation between User Table ( /app/Model/User.php) and Group Table (/app/Model/Group.php ) on 'username'(of User Table) and 'group_id'(of Group Table).
And the relation table is user_groups ( /app/Model/UserGroup.php).
In my application I want to add one raw in UserGroup containing group_id="something" and username=NULL.
As per HABTM defination it's not allow to add raw contain null 'username'. But in CakePHP Documentation there is option for custom insertQuery.
SO how to write this custom insertQuery for some special condition ?
In cakephp you cannote modify the table of join of an HABTM relationship.
If you want a custom join table you have to create manually inside the database it, create its model and in UeserModel use an hasMany relation with UserGroup and the same thing inside the table Group.
This is how to customize a join table in a HABTM relationship in cakephp.

Resources