CakePhp form validation when having two models on two different database - cakephp

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
}

Related

cakephp Model Relationships

I have an acl controlled cake application and specifically there are two tables I am having an issue with: 'users' and 'forms'. All of my tables have a 'created_by' field which links back to users giving me the user id of the records author. This is set on save within the forms model from Auth.
The issue I have is that each form record must be authorised by a user with a role of manager prior to being visible on site. I have therefore included a 'signoff_id' field which needs to relate back to the user table id.
In my Form model I have included the following
public $hasOne = array(
'User'=>array(
'className'=>'User',
'foreignKey'=>'created_by'
),
'Signoff'=>array(
'className'=>'User',
'foreignKey'=>'signoff_id'
),
'Db',
'Identity'
);
This works ok with created_by and I get the correct info back from finds, but signoff_id is looking for a Signoff.signoff_id field (and trying to bring back all of the User table fields but substituting Signoff for User.
Any ideas gratefully received - I've tried the RT(F)M option but am no further forwards.
Finally joined the dots together ...
my Form model links back to the User model with a foreign key of 'id', and the User model links to Form with a foreign key of 'signoff_id'
Simple now I've sorted it.

cakephp database table defining three dependent relationships

I have three tables (blocks, roles, users) where the following is true in the schema:
blocks have many users
users have many blocks
users have many roles, but the roles are specific to the block (and the user)
blocks have many roles, but the roles are specific to the user (and the block)
For example:
user => john
belongs to block => 'metro' with role => 'builder'
belongs to block => 'rural' with role => 'investor'
user => dan
belongs to block => 'metro' with role => 'resident'
belongs to block => 'rural' with role => 'investor'
I have a join table named block_role_user with the following columns:
id block_id role_id user_id created modified
I guess this would be a hasandbelongstomany relationship amongst all three tables. I thought about laying this out with a hasandbelongstomany table between blocks and users and another hasandbelongstomany table between roles and users but this will not fully define the unique relationship between a block, role and user.
I'm looking for advice on the best way to approach this and appreciate any advice.
I've looked into binding the model, but this doesn't seem to apply for the unique three part relationship I have here.
I thought about creating a model called blockroleuser and setting up the relationships using standard cakephp convention. If this is an acceptable way, can you give some guidance on naming the model and controller files?
Thanks again...
If I got you right, then you are looking for a relation less loose than HABTM, which will define a specific connection between a User, a Block and a Role.
Personally I would probably use a model that defines these relations using belongs to, where a User can have many of those, something like:
User hasMany Whatever
Whatever belongsTo User
belongsTo Block
belongsTo Role
This is probably more or less what you ment when you were talking about a BlockRoleUser model, the proper naming might depend on what exactly blocks are. Of course a generic name like for example Involvement or even UserBlockRole might be fine too, I don't think you should bother too much about this.
Anyways, this looks pretty straightforward and strict to me, and if necessary for your application, then additional HABTM relations could be set up too utilizing the same table. These HABTM relations would most probably only be used for selects only, for example when there's need for querying all the Blocks a User is related to, or when testing whether a User is related to a specific Block, etc...

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.

Model agency database opinion

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.

CakePHP Associated Models available after save?

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.

Resources