I have been playing with fuelphp lately and am trying something with the ORM.
Never used an ORM before so I think I don't understand very well.
My database consists of object types with multiple objects meta´s.
So I have three tables.
object_types which has multiple object_types_meta which is a join table to objects_meta_type
But I don't know how to convert this to the ORM. Can someone please point me in the correct direction?
I thought it would be:
object_types has many object_types_meta and then object_types_meta belongs to objects_types but what do I do with object_meta_type?
Thanks in advance!
It looks like you may be using a many to many relationship with object_types_meta being the in between table?
http://fuelphp.com/docs/packages/orm/relations/many_many.html
If the join table doesn't contain any extra columns except the two foreign keys, you should use a many-many relation, and use the join table as 'through' table.
If it does contain columns you need to access, you have to create a model for the join table too, and create two one-to-many relations from the join table to the two other models.
Related
Heya I am novice web dev or actually I am still in education.
I got this situation Where I have 3 tables lets say : Students, Groups and a join table Student_group.
I put my data from Students in the student model and from groups I put its data in the Group Model so I can use it my application. But I store a date in the Student_group table because I need to know when a student changed from a group.
So my question is in which model do I put this date? Do i need to make a new model for the combined tables or do I need to add another attribute to the student model?
Thanks in advance ;D
That depends. Will the student be in many groups, or one?
If one (one to one relationship), you can decide where to put it. The column could be in either the Student table, or the Student_group. In this case, though, it may be advisable to flatten the data and simply add group columns in your Student table. You decide that as well - if it seems unnecessary to have a join for a one to one relationship (usually it is, not always), then flatten it. In either case, the data should stay in its respective model. That said, you should use the Student model if you handle it in the Student table.
If many (one to many relationship), I'd advise putting it in the Student_group table and leaving it in that model as well.
All in all, the model should be a direct reflection of the data it's representing. You could make some methods inside your Student model to make it easier to get the date, for example. However, I'd personally handle that date inside of the proper model, Student_group. As mentioned, the model should be a direct representation of the data. Again, though, there's nothing wrong with making things a bit easier by creating some methods that help out the developer.
I have 2 tables and I need to join those tables.
I need to select id and name from galleries, where share gal.id = galleries.id and user_id = auth::id().
Tables:
Galleries: id, name
Share: gal_id, user_id
Please show me example in laravel. And I need to display it.
To achieve this, you have Relationship in Eloquent ORM. The official website states :
Of course, your database tables are probably related to one another. For example, a blog post may have many comments, or an order could be related to the user who placed it. Eloquent makes managing and working with these relationships easy. Laravel supports many types of relationships:
You can read all about eloquent relationship over here
If you do not want to use relationship, following is an example on how to join two tables :
DB::table('users')
->select('users.id','users.name','profiles.photo')
->join('profiles','profiles.id','=','users.id')
->where(['something' => 'something', 'otherThing' => 'otherThing'])
->get();
The above query will join two tables namely users and profiles on the basis of user.id = profile.id with two different conditions, you may or may not use where clause, that totally depends on what you are trying to achieve.
Is it possible while defining a related Model in an ActiveRecord definition to specify a relation scope that allows only those models to be related whose corresponding column(s) match some predefined critaria in the joining table ?
eg. let us have a users table with fields : id(pk), username(pk),pwd_hash(text), pwd_salt(text)
and an items table : id(pk), itemData(text)
and a correlating table : id(pk),user_id(pk), item_id(pk), some_attribute(int)
Now I would like to define a Many-to-Many relationship such that User Model has a field xitems which would provide me only those items for which the value of some_attribute is greater than some value y. Is it possible to do so using Yii ActiveRecord implementation.
I do understand that I can define a model corresponding to the correlating table which would have belongs-to relation with both items and users and query this table ... but I was looking for a more succinct approach.
[edit]
Probably my best bet would be define a model method which abstracts an inner join operation.
I'm fairly sure you do need a model for the join table to get access to its data, which your example needs. Without the join table model, AR will only deal with the FKs in the join table (AFAIK).
This article may be of help to you: Accessing data in a join table with the related models
I have Doctrine setup with all my tables in my database, and working properly.
I have those three entities: Users, Groups and News
There is a many-to-many relationship between Users and Groups. (I put my users in groups)
There is also a many-to-many relationship between News and Groups. (I give access to a News item to a few Groups)
Database Schema:
I want to get the News that a given User has access to.
Is there an efficient way to do this in Doctrine?
EDIT: I should add that I already had the solution nailed down with a straight SQL query before I started to use Doctrine, I want to know the Doctrine way to do this.
This thread explain the issues about many to many relationships in doctrine.
many-to-many relationship in doctrine
This blog post explain how to handle such events.
http://melikedev.com/2009/12/09/symfony-w-doctrine-saving-many-to-many-mm-relationships/
I have two tables that look as Follows:
Person (Table Name)
Name1/Phone1/Email1/Address1/Organization1/Notes1 (Fields)
Organization (Table Name)
Organization1/Phone2/Email2/Address2/Web2/Notes2 (Fields)
Organization1 is the only field in common between the two tables.
When I display data on a person, I want to also check and see if there is data on their organization and display it as well if it exists. I'm using PHP to interface with mySQL.
You need to JOIN the tables.
SELECT * FROM Person LEFT JOIN Organization ON Person.Organization = Organization.Name;
This assumes the relationship is the Organization Name. I've done a LEFT JOIN since you said if exists. Check out this tutorial for more detail on joining tables.
Note: I agree and would recommend making your database more relational by adding Primary Keys and using them as Foreign Keys in your other tables.
This post is an explanation of relations, not code for you to use. If you want that, look elsewhere
Well, connections between tables are called relations. There are 3 types of relations.
1) One -> One - This type of relation means 1 row is related to 1 other row in a different table
2) One -> Many - This type of relation means 1 row is related to a variable number of rows in a different table.
An example may be A folder can have multiple files, but a file can't have multiple folders. So in this case the 1 would be the folder, and the many would be the files.
3) Many -> Many - This type of relation means many rows can relate to many other rows.
An example may be labels. You can label many things the same name (desk appliance for example), and each thing can have multiple labels (a lamp can have both desk appliance & light labels).
.
So now that you know the different relations, we will go into your question. The relation you are looking at is a one to many, one corporation can have many people, but a person can only have one corporation. I suppose a person could work for multiple people, but that is much more complex (so we'll skip it).
One to many relations are by far the most common, and are pretty easy to do. This is where joins come in (left, right, and inner joins). Tizag has an excellent tutorial on joins here: http://www.tizag.com/sqlTutorial/sqljoin.php.
Hope that helps.
You should use a foreign key, but you need to use the InnoDB storage engine (MyISAM does not support foreign keys yet).
Make your tables look something like this:
Person_ID, Name, Phone, Email, Address, Organisation_ID, Notes (or if you have multiple notes, create a seperate table that maps person_id to a note).
Organisation_ID, Name, Phone, Email, Address, Web, Notes.
Select your person, then if Organisation_ID exists, select the Organisation where Organisation_ID equals the ID you obtained from the person row.