I apologize if this question has been asked and answered elsewhere but I have looked and have not been able to find it.
I have three models:
Manager HABTM Tenant
I also have a ManagersTenant model to tie the two together.
I'm trying to use pagination to recursively display fields from Manager and Tenant in the ManagersTenant controller. Here is my code:
$this->ManagersTenant->recursive = 2;
$this->set('managersTenants', $this->paginate('ManagersTenant',array(),array('recursive'=>2)));
This displays only the fields in ManagersTenant (id, tenant_id, manager_id) but does not retrieve data from the associated Manager and Tenant models.
I am also doing a debug($this->ManagersTenant->find('all')); which performs the recursion perfectly and displays the right arrays.
What am I doing wrong? Do I need to do anything special with my model(s)?
Any help is much appreciated.
//edit:
What I'm trying to do is display all matches where Tenant_id or Manager_id matches the logged-in user's id. For example, if a logged-in Manager performs the index function on the Tenant model, I would like for all Tenants to be displayed for Tenant_id where Manager_id (in the ManagersTenant model) == $this->Auth->User('id'). I was under the impression that in order to do this, I had to utilize a HABTM table. But if it is possible for me to do Manager HABTM Tenant without a joining table, I am all for trying it.
You probably don't need to define the ManagersTenant model. You should define HABTM in the Manager and in the Tenant model, and use those models for your query.
If you really need a ManagersTenant model you should use the join model relationship:
Manager hasMany ManagersTenant
Tenant hasMany ManagersTenant
ManagersTenant belongsTo Manager and Tenant.
Related
How to change cakephp concept user belongsTo Group, to user hasMany Group,
apart make new table called users_groups, and what should I change to make my user really have access to many groups that user have?
Make Things Simple:
In such scenario you already needed three tables.
Users table
id
name
phone
Groups
id
group(title)
User_Groups
id
user_id
group_id
As you need relations like user has many groups then you need extra table which at least should include foreign key like user_id, and group_id.So don't hesitate to add extra table for making things simple.
You'd be using 3 tables, and then a HasAndBelongsToMany relationship over the Users_Groups. Users HABTM Groups
I need a data-filter based on a child model HABTM relation condition.
My table structure is as following:
Project hasMany TaskGroup hasMany Task hasAndBelongsToMany User.
I need my find function to get only the Projects with specific TaskGroups that contain Tasks assigned to some User.id. In other words, I need my Tasks filtered by User.id, and structured as Project -> TaskGroup -> Task. Is there a way Cake model bindings handle this, or do I need to write the joins manually?
You'll need to use Joins - see link for how to build joins in CakePHP:
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#joining-tables
I am writing an application in Google App Engine with python and I want to sort users and user posts into groups. Users will be able to tag a post with a group ID and then that post will be displayed on the group page.
I would also like to relate the users to the groups so that only members of a group can tag a post with that group ID and so that I can display all the users of a group on the side. I am wondering if it would be more efficient to have a property on the user which will have all of the groups listed (I am thinking max 10 or so) or would it be better to have a property on the Group model which lists all of the users (possibly a few hundred).
Is there much of a difference here?
Your data model should derive from the most likely use cases. What are you going to retrieve?
A. Show a list of groups to a user.
B. Show a list of users in a group.
Solution:
If only A, store unindexed list of groups in a property of a user entity.
If both, same as above but indexed.
If only B, store unindexed list of users in a property of a group entity.
NB: If you make a property indexed, you cannot put hundreds of user ids in it - it will lead to an exploding index.
I would like to make a model agency based on codeigniter, but im a but stuck with the database, exactly the registration part.
I would like to allow users to sign up as, model, photohgrapher, agency, or make-up artist.
So could someoone give me an opinion how to make the database? Like seperate the models, photographers, agencies, and artists in diferent tables, and at the registration form only ask for baseic info? like name, password, email, D.O.B., or there is a nother way?.
Thank you
You should use entity sub-typing with a parent type of "USER", which will contain your basic information, and with sub-types of "MODEL", "AGENCY", "PHOTOGRAPHER", "MAKEUP_ARTIST". This will allow you to have a better user experience for the inevitable case where there is overlap. I'm sure there are photographers who have agencies and agencies that do make-up etc. It would be much better for these types of users to have a single user ID and password despite having different types of profiles.
Make a drop down for different type of people signing up which the data for drop down comes from a separate table (e.g. person_type) from database and save the basic details of the person in separate table with the ID of the person_type table.
You can make a model for getting, inserting and updating records for this purpose.
EDIT, I am rewriting the question for more clarity.
I have a "profile" model that has a belongs to relationship to a "user" model.
A certain user exists already. Later he creates a profile. The profile controller takes care of creating a new entry, but then needs to update a profile_id field as part of the associated user. I was under the impression that saveAll could take care of all the associations but it is not working?
What is the easiest/CakePHP standard way to do something like this? Thanks!
saveAll() creates new records. So you can't use it to update an already existing record in the Users table. As Anh Pham already mentioned, you've got your associations wrong. A Profile belongs to a User, and a User has one Profile. By having a profile_id field in your Users table, you're doing it the other way around.
So remove the profile_id field from the Users table, add a user_id field to the Profiles table, and update your model associations in user.php & profile.php.
To save a new Profile for an existing User, you can then either query the user id for the current User, or for example retrieve it through Auth, and add it manually to $this->data prior to calling the save() method of your Profile method.
You shouldn't have a profile_id field in the users table, you should have user_id field in profile table (the foreign key is similar to hasMany relationship). I'm surprised that the app still works http://book.cakephp.org/view/1041/hasOne
Also, I usually don't have hasOne relationship. If User hasOne Profile, then just include all fields in the profiles table into users table, unless there's some reason not to do it. But again, it's just my preference.