I am working with Grocery CRUD (PHP library that creates a full functional CRUD) and I have very basic table of users.
id | first_name | last_name
Some of this users are relatives. So I want to have separate relation table that looks like
id | first_users_id | second_user_id | relation_type
How can I add this relation to Grocery CRUD so while I'll be editing user's profile I would select other users that are relatives providing relation type for each of them?
Without much to go on you should be able to accomplish it like this: https://www.grocerycrud.com/examples/set_a_relation_n_n
You can also reference the docs here for using the function https://www.grocerycrud.com/documentation/options_functions/set_relation_n_n
$crud->set_table('user_table');
$crud->set_relation_n_n('relatives', 'user_user_table', 'user_table', 'id', 'id', 'id');
Related
I'm working on my first project (a project management system) to understand PHP and MySQL better. I currently have 3 tables in my database, one that lists all the projects ('projects' table), one that stores the users username and password ('users' table) and then a third, small 'permissions' table that is meant to filter out what projects a user can see from the projects table.
The third 'permissions' table consists of an unique id, and then a "user_id" and a "project_id" in the other two columns. The "user_id" references the id of the user in the "users" table, and the "project_id" references the id of a project in the projects table.
Note that more than one user can have access to a single project, that's why there isn't just a single column for this in the projects table, and I read that using comma separated lists for the multiple usernames is a bad way to go about it. As such I have set up foreign keys on "user_id" and "project_id" that reference the id of each accordingly.
Now I'm not sure if this is a good way to set things up but it seems okay in my head.
Really what I want to do is retrieve all the projects a certain user has access to. So something kind of like (but obviously works):
SELECT * FROM projects WHERE user_id = 3;
Is there a simpler way to do this? Something to do with the join command but I could never get it right.
Thanks in advance!
EDIT:
Apologies for the ambiguity, gets a bit tricky to explain.
Essentially I just want a way to filter what rows are displayed from the projects table by the user_id in the users table (note again more than one user can access a project so I can just put a "user_id" column in the projects table and leave out the third table altogether).
My tables are pretty much like so:
projects:
|id|project_title|project_notes|project_status|
|1 |A Project |Some Notes |Finished |
|2 |A 2nd Project|Some more... |In Progress |
users:
|id|username|password|
|1 |johndoe |*********|
|2 |benhill |*********|
permissions:
|id|user_id|project_id|
|1 |1 |2 |
|2 |2 |2 |
user_id = id from users table.
project_id = id from projects table.
Then in HTML/PHP I only want to retrieve the projects that a user has access too.
Unfortunately your question isn't very clear but I assume you want to have a single query to find all project that a particular user has access to, you can simply join up the tables with the id as such:
SELECT * from projects INNER JOIN permissions ON
projects.id = permissions.project_id WHERE
permissions.user_id = 3
Assume the user you are looking for has id of 3
Assuming you have
Users (Id, Name)
Projects (Id, Name)
Permissions (UserId, ProjectId)
and you want to "retrieve all the projects a certain user has access to", and you have the user id stored in a variable $userId, you could use a simple query like this:
select * from Projects pr
join Permissions pe on pr.Id = pe.ProjectId
and pe.UserId = $userId
Google around for more info on SQL syntax and joins. Here's a link I just found that looks like a good place to start: http://www.sitepoint.com/understanding-sql-joins-mysql-database/
I need to create a relation based on a field that is not the primary key. Many of the examples of how to do this are based on One to many and many to many relationships. I have tried the suggestions from the following without success
Relation in YII with not "ID" as primary key
Yii CActiveRecord: find related data, but not using the primary key
Yii Relations with non-Primary keys
Yii Model Relation Join (HAS_ONE)
I have the following table structure:
+------+---------+-----------+
| id | name | status_id |
+------+---------+-----------+
| 1 | service1| 1 |
+------+---------+-----------+
| 2 | service2| 2 |
+------+---------+-----------+
This is my table active_service. I also have the following table
+----------+----------+---------------------+-----------+
|id |related_id|related_text | text |
+----------+----------+---------------------+-----------+
|65 |1 |ActiveServices_status| Open |
+----------+----------+---------------------+-----------+
|72 |2 |ActiveServices_status| Active |
+----------+----------+---------------------+-----------+
|102 |3 |ActiveServices_status| Closed |
+----------+----------+---------------------+-----------+
This is my related_fields table
This table holds all the fields used for dropdown etc. The related_text tells us what it is for and the related_id is the id of the status and this is the field i need to link to. So the status_id in the active_service table relates to the related_id field of the related_fields table where the condition is met, ie the related_text is set to ActiveServices_status. How would I go about creating this relation. This is the best example of what I have done so far (in the ActiveServices model).
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'rl_status'=>array(self::BELONGS_TO,'RelatedFields','status_id','condition'=>'related_text = "ActiveServices_status"','on'=>'status_id = related_id'),
);
}
Any help would be appreciated.
So finally figured this thing out after trying about 100 different lines of code. So heres the solution that worked for me.
'rl_status' => array(self::BELONGS_TO, 'RelatedFields', '', 'foreignKey' => array('status_id'=>'related_id'),'condition'=>'related_text = "ActiveServices_status"',
For the record, in Yii 1.1.9 this was addressed; see issue #2706. It is not terribly obvious from the documentation, but this exact thing can be accomplished by putting an array in place of where the foreign key name would ordinarily go, with local column as key and foreign column as value.
So, for instance, if you had two local columns 'fk1' and 'fk2' referencing a composite unique key with columns 'col1' and 'col2' in the table for model "Foo", your entry in the relationship array would look like this:
'foo' => array(self::BELONGS_TO, 'Foo', array('fk1'=>'col1','fk2'=>'col2'))
Quoted from the documentation on CActiveRecord.relations():
In case you need to specify custom PK->FK association you can define it as array('fk'=>'pk').
'rl_status' => array(self::BELONGS_TO, 'RelatedFields', '', 'foreignKey' => array('status_id'=>'related_id'),'condition'=>'related_text = "ActiveServices_status"'
The question is not new in any way but it has a small twist to it.
My webpage is a membership page where users places bets. My idea is to create a new table for the users(with a naming convention like TABLE userBet+$userid) bets. User login information is already handled, my goal is now to save the bets of the user to a new table. A table which is created when users register. This will hopefully make score counting easier. Am I right or wrong? Could this be done in a better way? (Everything is done in PHP MySQL)
User registers -> Table for bets get created
"CREATE Table $userID ,id_bet, games, result, points"
And then matching this table against the correct result?
So again my questions: Is this a good way to do it? Is creating a table with the userID a smart thing to do?
EDIT
The bets is always 40 matches, which makes the tables Huge with columns and rows.
Should I make 40 Tables, one for each games instead? and put all users in there?
Am I right or wrong?
You are wrong. Dynamically altering your database schema will only make it harder to work with. There's no advantage you gain from doing so. You can do the same things by storing all bets within the same table, adding a column userid.
Posting as an answer due to author's request : )
Suggested database schema:
table matches:
id | name |
---------------
1 | A vs B |
table user_bets
id | user_id | match_id | points | result |
-------------------------------------------
1 | X | 1 | Y | Z |
Where match_id is related on matches.id
user_id = user.id
user_bets is only one table, containing all the info. No need of separate tables, as it was clear from the comments it's considered bad practice to alter the db schema via user input.
how I can use array in cell related with another table in database like ?
Users table:
Name | languages_id
Anas | 1,2,3
Languages table:
id | language
1 | English
2 | Arabic
it’s work or not ?! and do you know what can I use in yii to do this ?
Don't do this.
Don't store mutiple items as comma separated column, it is really bad.
You should keep your tables normalized, by creating a new table UsersLanguages as a many to many table between the USERS and Languages table. Somrthing like this:
Users:
UserId,
UserName,
... other details.
Languages:
LanguageId,
LanguageName,
...
UserLanguages:
UserId foreign key references Users(UserId),
LanguageId foreign kery references Languages(LanguageId).
You can use FIND_IN_SET.
SELECT
name,
language
FROM
users
INNER JOIN
languages ON FIND_IN_SET(languages.id, languages_id) != 0
GROUP BY
name
Although Mahmoud Gamal's comment would perhaps be the better way to go about it.
At the moment I have three tables that I am trying to connect and figure out what queries will get the results I need and also follow best practices. MySQL is still pretty new to me, and this is my first stumbling block that I can't figure out.
I am trying to build a simple URL shortener that can "link" multiple long URLs to one short URL. I basically want to have a link www.example.com/google then have google.com, google.co.uk, google.it, etc... and GEO target when the user accesses the link.
My three tables are set up as:
short_id | user_id | short_url //Short URL Table
long_id | user_id | long_url | country_code //Long URL table
user_id | name | password | email | created //User table
I am not sure if foreign keys are the best route. Also, I understand how to add a user, but what queries would I have to run to have a user add a short/long url and have the user_id field in "user" table match the user_id fields in the other tables.
Thanks for the help.
select * from short_url_table
left join long_url_table
using (user_id)
left join user_table
using (user_id) ;