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.
Related
I'm trying to implement a database that has inheritance between some tables, there is three tables involved in the question problem: Customers, Users and Addresses (actually there is more tables involved, but with the same problem, so..).
The Customers table inherits from Users table, and the Users table has a relationship with the Addresses table (1 to many, respectively).
So My problem is that I want that table 'Customers' to has the same relationship that 'Users' has with 'Addresses', cause Customers is inherits from it. I also try to insert data to 'Addresses' with an ID from 'Customers', but this give an foreign key constraint violation, the value doesn't exists in table "myDb.users" error
this is a image of my modeling:
(Note: I'm actually using PostgreSQL, I'm just using the ADO.NET to modeling, and I know a way to get around this, but if has no way by inheritance I will change the entire DB to full relational-database.)
I assume that you're using PostgreSQL table inheritance which, unfortunately, doesn't work quite as we would expect. In particular, although records from child tables appear in selects from parent table, they are not physically recorded there, and thus their ids can't be used in foreign keys referencing parent tables.
You may consider implementing inheritance using classic approach:
CREATE TABLE Users(id INT PRIMARY KEY, user_property INT);
CREATE TABLE Customers(id INT PRIMARY KEY REFERENCES Users, customer_property INT);
CREATE TABLE Addresses(user_id INT REFERENCES Users, address TEXT);
This way you physically store properties of Customer in two tables, and you are sure that for every Customer there is a record in Users table which can be referenced from other tables.
-- inserting customer with id=1, user_property=10, customer_property=20
INSERT INTO Users(id, user_property) VALUES (1, 10);
INSERT INTO Customers(id, customer_property) VALUES (1, 20);
-- Inserting address
INSERT INTO Addresses(user_id, address) VALUES (1, 'Wall Street');
The drawback is that you need to join Users and Customers if you want to get all properties of a single customer from both tables:
-- All customer properties
SELECT * FROM Customers JOIN Users USING(id) WHERE Customers.id=1;
-- Customer and address
SELECT * FROM Customers JOIN Users USING(id) JOIN Addresses ON Users.id=Addresses.user_id WHERE Customers.id=1;
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 can i query to Access Extra fields(Batch,Part) in my Association table (StaffCourses) for many-to-many relationship in ASP.Net MVC.
Edited: I have Staff_Id in hand. so how to query through Staff_Id .
.
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.
I am trying to add a view to an entity data model but I get the error below.
The view is a group by with a count. I don’t understand this because a view does not have a primary key by it’s nature.
I modified the original post because I figured out how to add a key to the view. But I still have the same problem.
warning 6013: The table/view
'fmcsa.dbo.vieFMCSADocumentCount' does
not have a primary key defined and no
valid primary key could be inferred.
This table/view has been excluded. To
use the entity, you will need to
review your schema, add the correct
keys, and uncomment it.
Here is the View
CREATE VIEW [dbo].[vieFMCSADocumentCount] with SCHEMABINDING
AS
SELECT COUNT_BIG(*) AS CountOfDocs, ROLE_ID, OWNER_ID
FROM dbo.FMCSA_DOCUMENT
GROUP BY ROLE_ID, OWNER_ID
then I can add a key
CREATE UNIQUE CLUSTERED INDEX [MainIndex] ON [dbo].[vieFMCSADocumentCount]
(
[OWNER_ID] ASC,
[ROLE_ID] ASC
)
Still not working.
You didn't specify, but I'm assuming you're using EF4. I've come across this before--you either want to define a key manually or edit recreate your view WITH SCHEMABINDING and reimport.
Schema binding effectively tells SQL to track dependencies for your view. It's both a blessing and a curse (try adding a column to FMCSA_DOCUMENT once this view has schema binding), so you might want to read up on the effects.
CREATE VIEW [dbo].[vieFMCSADocumentCount] WITH SCHEMABINDING
AS
SELECT COUNT(ID) AS CountOfDocs, ROLE_ID, OWNER_ID
FROM dbo.FMCSA_DOCUMENT GROUP BY ROLE_ID, OWNER_ID
Alternately, in the EF Model Browser Go to the Entity Types folder, find your view (right click and choose Show in Designer). Then on the view, highlight the column(s) that comprise your primary key and right click and choose "Entity Key"