I have 2 models user and category. each user will select multiple categories of data, I'm using HABTM. Is this right method?. If yes How can I store multiple category details with each user in join table?
Never use HABTM, if you want to add attributes to your join table and interact with your join table.
Use has_many though. You can read more about it in the docs below
http://guides.rubyonrails.org/association_basics.html#choosing-between-has-many-through-and-has-and-belongs-to-many
Related
I have a table customers with the fields id, name and so on.
One table doctors with the fields id, name.
Then there is one table subject_areas which has all subject areas which a doctor can have. The fields are id, text.
So, each doctor can have multiple subject areas. There is one pivot table doctor_subject which is a belongsToMany relation.
Here is my problem: A customer can have multiple doctors, but only for a specific subject area. I tried it with a new table customer_doctor with the fields id, customer_id and doctor_subject_id. But how do i map this in Eloquent?
Issue was in relation between tables. After chat clarification this came out as solution:
Html form is written in a way that customer first choose doctor, then depending on selection choose several of his available areas.
In that scenario customer needn't to be related to areas directly and should be related to areas only over relation with doctor.
Also as side note, if needed deeper relations, models on pivot tables could be created and used as well.
I'm trying to do a one_to_many association through a join table.
I have Orders with a cart_id field pointing to a Cart, which has no relevant keys. In the CartItem table there's a cart_id pointing to the cart. Poring through the documentation, I see how to implement the case where the Cart has a order_id column, the many_to_many association but not for the other way around. many_to_many seems to expect the left key to be in the join table. And one_to_many seems to not allow the use of a join table at all. Or maybe I'm just missing something?
After getting on IRC with Jeremy Evans, he said I should use the following options on a many_to_many.
:left_primary_key=>:cart_id, :left_key=>:id, :right_key=>:id, :right_primary_key=>:cart_id
In CI, how do you relate each other the models?I have four models right now Users, UsersDepartment, UsersToDepartment, UserStatus and I need to join those four models to be able to pick up all the data.
I have this code in my controller to pick all users data from the Users Table:
function view($user_id){
$data['user'] = $this->User_model->get_by_id($user_id)->row();
}
The user_status saved in the Users Table is only the status_id so I need to connect to the UserStatus table to get the equivalent name of the users_status_id. I need to know the list of group of which the user belongs to. So I need to get it from the UsersToDepartment Table based on the Users.userid. Then get the equivalent groupname in the UsersDepartment Table. Please see my diagram to explain further.
I know in the native PHP, this can be done by using join. How is that done in CI?
I know with yii, you can do it this way
$posts=Post::model()->with(
'author.profile',
'author.posts',
'categories')->findAll();
Is this possible with CI too?
example u have table_one and want to join table_two using their id
$this->db->select('columns');
$this->db->from('table_one');
$this->db->join('table_two', 'table_two.id = table_one.id');
//then do the query
you can read this link below for more complete tutorial :
https://www.codeigniter.com/userguide2/database/active_record.html
code igniter is not a ORM framework for php...
you can not treat it like ORM frameworks(Laravel is good example for ORM frameworks).
but you can simulate that with join on query.
this work just get you the others models data and not get you those models object ...
Refer to $this->db->join(); heading in Active Record: CodeIgniter
I know codeigniter is not that good here. So I always prefer Yii over it.
Try use this query of joining table
Select a.*,b.*
from table_one a
inner join table_two b where b.id=a.id
I'm using CodeIgniter and I'm starting to work with gas orm.
One of my m-n-relationship-tables using a composite key has also some additional attributes to the releation.
For Example:
Table teams, Table employees, and a m-n table which binds them together + adding the attribute role
Is it possible to get the attribute using GAS ORM?
Yes, it is possible.
Simply create a new relationship in one of the two tables you are going to link with the pivot table that refers to the pivot table itself as a has_many relation. (But dont do the linking stuff in the model file, eg:
ORM::has_many('\\Model\\User\\Role')
instead of
ORM::has_many('\\Model\\User\\Role => \\Model\\Role')
See http://ellislab.com/forums/viewreply/1050559/ for exact the same question.
I have three models, Employee, Assignment, and Store.
Employees have many Stores through Assignments, and Stores have many Employees through Assignments.
(Assignments link Employee and Store together via foriegn keys)
Employees can only have one current assignment to a store.
What I'm trying to do in the Employee's index view is to display the employee's currently assigned store name (name is a field in the Store model).
How would I do this?
Assuming your relations are set up in the usual way, use includes to eager-load the associated records:
#employees = Employee.includes( :stores => :employees ).all
Then in your view you'll have access to #employees.stores[n].name (.stores is a collection because you said Employee has_many :stores, ...).