Currently my CGridView gives following result.
http://imageshack.us/a/img821/2391/44264318.png
Here City is actually CityID From a parent table. City table has one-to-many relationship with Campus table. I want to show city.cityname instead of city.cityid from parent table. Can someone help please.
Database structure is as
http://imageshack.us/photo/my-images/845/82338990.png/
change that column to
array (
'name'=>'mycol
'value'=>'$data->city->cityname',
);
where city is a relation name, and cityname is an attribute from the linked class.
To sort see this article http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/
in your grid columns use:
array (
...
'city.cityname',
... other columns...
);
Related
How can i create and migrate lots of tables in laravel with a variable in their name?
I need to create a table "time-{{user_id}}" every time a new user is added to a table called "users".
How can i realize this and how does the Migrationfile/Controller/whatever has to look like?
Thanks!
"Now i need a table for each user where i can store something like a CV.. Date begin, date end, City etc"
Sounds like you need another table with a 1:1 relationship to the users table
Your new table will look something like
tblFurtherUserInfo
FurtherUserInfoId
CV
DateBegin
DateEnd
City
You should add a new row to this table and set FurtherUserInfoID to be the same as the userID in your user table. You should do this whenever you add a record to your user table
EDIT
Now I know a user can have more than one CV, you need a new table as follows
tblCV
CVId
UserId
CV
DateBegin
DateEnd
City
This is a 1 user : many CVs relationship
Is there a way to Make Database Table Name as Prefix of Database Table's Field Name. For example: Say I have TABLE1 which has ID Column. I'd like to make the field name as TABLE1_ID or TABLE1ID instead of TABLE1.ID.
In other words is there a way to Qualify Field names?
Your help is really appreciated.
I don't think that's possible, because you're looking at the problem in the wrong way(whatever your problem is):
Your field name is ID and TABLE1.ID is how you access that field so when you say TABLE1.ID you can think of Field 'ID' from table 'TABLE1'.
Think about it like a class property and let's take an example in Python:
class someClass():
def __init__(self,id):
self.ID = id
TABLE = someClass(3)
print(TABLE.ID)
So I'm printing the ID of the TABLE and not the TABLE.ID
I have two table "Container" and "Control". These are existing tables and there is no foreign key relationship between the two. These are also very old tables so are not normalized. And I cannot change the structure now.
Below is the structure of the two tables.
Container table :
Control Table :
The Name field in Control table contains CTableName+CPName from Container table.
I want to update the columnName field of Control table with the value of CID column of Container table. and also want to insert one more record (for ctable2 i.e the fourth row in final Control table below) in Control table.
The tablename and columnname columns have will always be have default values.
The final Control table should look like this:
How do I do this?
I hope you want to apply this fix because you want normalize your table structure.
Try this:
First step:
In this way you'll UPDATE all Control rows with the value of Container table where the couple fields CTableName and CPName are the same of Name (excluding the rows of Container with the same couple fields)
UPDATE Control
SET ColumnValue = (
SELECT c.CID
FROM Container c
WHERE c.CTableName + '+' + c.CPName = Control.Name
AND NOT EXISTS(
SELECT 'PREVIOUS'
FROM Container c2
WHERE c.CTableName = c2.CTableName
AND c.CPName = c2.CPName
AND c.CID < c2.CID
)
),
TableName = 'default', ColumnName = 'default'
WHERE ColumnValue IS NULL
Second step:
Adding elements don't present in Control table
INSERT INTO Control (field list)
SELECT field list
FROM Container co
WHERE NOT EXISTS(
SELECT 'in_control'
FROM Control ct
WHERE co.CID = ct.ColumnValue
)
After these two steps you can drop column Name in Control table
I am an Oracle plsql programmer and worked with Sql-server as well.
First you should describe the relationship between the 2 tables, in the end i could figger it out but it's better you explain it yourself.
To update a table with information from another table you should ask yourself:
- when should the update take place?
- what are the conditions to start the update?
- how should the update be done?
In Oracle there is a database object called a trigger. It's quite a handy object and probably just what you need. I believe that sql-server has it too.
Pls fee free to ask any questions but do read the sql-server appropriate manual as well.
Good luck, Edward.
I have 3 tables: Book, Section and Content. I want to add many-to-many relation between Section and Content. Section and Content tables have a PageNo column. A Page may have many contents and many Sections. In Brief:
Book 1----* Section (on BookId)
Book 1----* Content (on BookId)
Section *-----* Content (on PageNo)
The PageNo is not unique for both Section and Content tables. So I can't add foreign key for PageNo in Sql Server.
I tried to create a junction table like this:
SectionContent: [SectionId, ContentId]
And I added FKs for this junction table. So entity framework could understand the junction table and it set up many-to-many relationship on SectionId and ContentId. But everytime when I need to insert one of Section or Content Table, I have to insert to SectionContent junction table, too. So first I have to check if there is a same record already in the junction table. Also there are a lot of insert operations in the project. I have to search for all insert operations and I have to add extra query to insert into the junction table.
Also I need get the sections and contents in a page. This is extra effort for me.
I can remove the relationship between Section and Content tables. And I can use extra join queries on PageNo column. But I want to use entity. I want to get Contents in entity way like Section.Contents and I want to get Sections in the same way like Content.Sections.
So can I add many-to-many association between Section and Content on PageNo column without SQL Server's FKs?
Edit: Also If I use the junction table above, I must execute an sql query like this, do I?
INSERT INTO SectionContent
SELECT * FROM
(
SELECT Section.id AS SectionId, Content.id AS ContentId
FROM Section
LEFT OUTER JOIN Content
ON Section.PageNo = Content.PageNo AND
Section.BookId = Content.BookId
UNION
SELECT Section.id AS SectionId, Content.id AS ContentId
FROM Section
RIGHT OUTER JOIN Content
ON Section.PageNo = Content.PageNo AND
Section.BookId = Content.BookId
) AS T
WHERE SectionId is not NULL AND ContentID is not NULL
GROUP BY T.SectionId, T.ContentId
I solved this issue by using EF Code First. I've implemented all tables as a class and EF handled all many-to-many relationships.
Web app is being written in classic ASP with a MSSQL backend. On this particular page, the admin can select 1 or any/all of the employees to assign the project to. I'm trying to figure out a simple way to store the employee IDs of the people assigned to it in one column.
The list of employees is generated from another table and can be dynamic (firing or hiring) so I want the program to be flexible enough to change based on these table changes.
Basically need to know how to assign multiple people to a project that can later be called up on a differen page or from a different query.
Sorry for the n00bish question, but thanks!
Don't store multiple ID's in one column! Create another table with the primary key of your existing table and a single ID that you want to store. You can then insert multiple rows into this new table, creating a 1:m (one to many) relationship. For example, let's look at an order table:
order:
order_id
order_date
and I have a product table...
product:
product_id
product_name
Now, you could go down the road of adding a column to order that let you list the products in the order, but that would be bad form. What you want instead is something like..
order_item:
order_item_id
order_id
product_id
quantity
unit_price
You can then perform a join to get all of the products for a particular order...
select
product.*
from orders
inner join order_item on order_item.order_id = order.order_id
inner join product on product.product_id = order_item.product_id
where orders.order_id = 5
Here's an example order_id of 5, and this will get all of the products in that order.
You need to create another table that stores these values such as. So this new table would store one row for each ID, and then link back to the original record with the original records ID.