php


Creating multiple models from one model in CakePHP


I have a users and a clients table.
They are linked as follows:
-- -----------------------------------------------------
-- Table `users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `users` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`email` VARCHAR(45) NOT NULL,
`password` VARCHAR(255) NOT NULL,
`usertype` CHAR(2) NOT NULL,
`created` DATETIME NOT NULL,
`modified` DATETIME NULL DEFAULT NULL,
`firstname` VARCHAR(45) NOT NULL,
`lastname` VARCHAR(45) NOT NULL,
`phonenumber` VARCHAR(45) NOT NULL,
`suburb` VARCHAR(45) NOT NULL,
`state` VARCHAR(10) NOT NULL,
`businessname` VARCHAR(45) NULL DEFAULT NULL,
`image` BLOB NULL DEFAULT NULL,
`valid` INT NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `email_UNIQUE` (`email` ASC))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `clients`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `clients` (
`id` INT NOT NULL AUTO_INCREMENT,
`user_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `CLIENTS_FK_idx` (`user_id` ASC),
CONSTRAINT `CLIENTS_FK`
FOREIGN KEY (`user_id`)
REFERENCES `users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
I have baked these two tables and I have created a users registration page. Upon form submission I want the database to create both a user and a client object.
Here is my UsersController method form CakePHP:
public function clientregistration()
{
//Register a new client. Client enters all details and the system sets usertype=CL
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->getData());
$user->usertype = 'CL';
$user->valid = 1;
if ($this->Users->save($user) && $this->Coaches->($coach)) {
$this->Flash->success(__('You have successfully registered.'));
return $this->redirect(['action' => 'login']);
}
$this->Flash->error(__('Registration failure. Please, try again.'));
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
I want the code to automatically pre-fill all data in the clients table as well.
How do I do this?

Related Links

Show numbers and names of connected users
mySQL and PHP login page problems
Laravel new command 404 error
WooCommerce Default Shipping Zip Code
[function.filemtime]: stat failed
Adding 2 queries from 2 php files together
When running the project I'm getting error: Warning: opendir failed to open dir: not implemented
error when replace with str_replace and preg_replace
CodeIgniter inser/update_batch (probably)
XSS attack in client's IP address
Side by side multilevel MultiCells in FPDF
mysql query no longer works
User friendly back-uping system - autoadding script to crontable
cannot modify header information - Download File [duplicate]
Codeigniter Joining 4 Tables
Select files which name starts with some text [closed]

Categories

HOME
elasticsearch
knockout.js
keyboard
transactions
devexpress
ios-simulator
websocket
elk-stack
ui-automation
fasm
xades4j
match
ex
vertica
office-ui-fabric
string-formatting
parceler
pfobject
data-type-conversion
sqlplus
multi-upload
managed-c++
strophe.js
clipboard
uilocalnotification
openbr
commonsware-cwac
sql-execution-plan
xmlunit
widestring
rdp
task-parallel-library
export-to-pdf
sendinput
xcode8.2
flume-twitter
music21
libreoffice-writer
non-linear-regression
subforms
sequence-diagram
aurelia-fetch-client
alertify
elastic4s
ndk-build
seccomp
android-4.2-jelly-bean
android-sdcard
perforce-integrate
ajaxmin
verisign
currency-exchange-rates
seadragon
myfaces
servicestack-bsd
directx-9
dsa
android-studio-import
html-escape-characters
sql-processor
juttle
datagridcomboboxcolumn
utf8-decode
device-admin
windows-rt
tabris
back
full-text-indexing
squeezebox
webautomation
particle-swarm
pbkdf2
grails-2.3
mp4parser
statechart
unicoins
notin
dibs
user-forums
multiscaleimage
sfinae
pageheap
prefuse
autosize
type-safety
n-queens
yuidoc
dynamic-c
office-automation
startupscript
asp.net-mvc-views
turbine
tabbarcontroller
error-detection

Resources

Encrypt Message