CakePHP Associated Models available after save? - cakephp

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.

Related

Cakephp 2 user have multiple group access

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

Cakephp, HABTM association

Cake newb here.
I have two tables. Users and Events. An user can subscribe to Multiple events.
What is the best way to implement this?
Do I have to create another table and link them or is there any other better approach.
If I do create a new table, how do i link them in cake model?
As said by jQuery.PHP.Magento.com you should use HABTM relationship but the name of the third table should be events_users because the table names should be in alphabetical order.
From the doc:
Table names are in alphabetical order by convention. It is possible to
define a custom table name in association definition.
You should use HABTM relationship.
Reason
See users will subscribe to Multiple events and
One event have multiple users subscribed for.
So this is two way relationship. Therefore you need following tables
users : To store user's data,
events : To store user's data,
events_users : To store Which user joined Which event and Vice versa(Events with n number of users)
So users_events will have 2 fields user_id , event_id , both are foreign keys and here you dont need primary key in HABTM relationship.

Database Tables - To decouple or not?

Is it better to create tables that store a lot of data that are related to an entity (User for example) or many tables to store said data?
For example:
User Table
Name
Email
Subscription Id
Email Notifications
Permissions
Or
User Table
Name
Email
Subscription Table
User ID
Subscription ID
Notification Table
User ID
Receives?
... etc
Please consider code in this as well, or I would have posted to ServerVault.
From a relational design standpoint what is important is the normal form you're aiming for. In general, if the "column" would require multiple values (subscription_id1, subscription_id2, etc) then it is a repeating group, and that would indicate to you that it needs to be moved to a related table. You've provided very general table and column notes, but taking a cue from the fact that you named "Email Notifications" and "Permissions" with plurals, I'm going to assume that those require related tables.

CakePhp form validation when having two models on two different database

I've a not common problem to do a form validation.
First let me explain a part of the problem: I'm doing a cakePhp website, this website will be used to sell product to customer. To do this, we have two database: one database(database A) relative to products, customer references, bills(provided by the ERP), and one database (database B)relative to information that the website has to store only for the website(passwords of users, cart content, comments on a products, ...).
To register ONE user on our website, I've to:
Create one "address" in the database A
Create one "customer" in the database A
Create one "user" in the database B.
This has to be only one action.
I'm on the user controller, so no problem to validate every fields of the "user", but how to make this form validate all constraints I have in my customer and address models?
The problem is that because user and customer are not in the same database, I can't(in fact I'm not sure of that, but it seems to be logic, because of automatic Left join) declare the $belongsTo and $hasOne relationship between user and customer.
So how could I make the check of those constraints?
Thank you very much
You can validate fields manually.
$this->Customer->set( $this->data );
$this->Address->set( $this->data );
if( $this->User->validates() && $this->Customer->validates() && $this->Address->validates() ) {
// save data
}

Recursive Pagination in CakePHP Not Working

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.

Resources