Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 424
J4N Avatar asked May 14 '26 12:05

J4N


1 Answers

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
}
like image 171
JJJ Avatar answered May 18 '26 22:05

JJJ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!