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

just started apache .htaccess rewrite to lighttpd conversion
Problems with php and mysql db [duplicate]
Large sql database contain syntax errors how to handle and import
PHP Array To HTML Table Issue
laravel 4 Undefined index: password
How to customize redirect url after request validation fails in laravel 5?
zf3 - exception while calling authenticate method of Zend Authentication Service
multiple message using SESSION
html container scrolling bug
Trying to change how the remove button works
How to secure video url using laravel?
cakephp3- hashed password doesnt match when compared
How i can get first child element from xml?
PHP - Is there a way to get a variable value if it is set?
Visual Composer: Add options to “Post Grid” component
Joomla 3.7 administration link doesn't work

Categories

HOME
cocoapods
xpath
jboss
entity-framework
svn
blast
jms
kendo-dropdown
enums
ews
openflow
u-boot
anchor
msmq
siesta
supervisord
exec-maven-plugin
maven-plugin
http-authentication
scheduled-tasks
infrared
clr
unity-container
workday
maven-surefire-plugin
pdf.js
unordered-multimap
tortoisehg
django-import-export
source-insight
delayed-job
nest
dreamfactory
xerces-c
numerics
spinner
selenide
game-center
nuclio
spongycastle
image-registration
patternlab.io
dtd
case-when
msxml
ltrace
dymola
nrf51
dpkg
jtag
dojo-build
piping
uibinder
gulp-uglify
as3-api
taco
seadragon
django-validation
stack-smash
opencyc
enaml
replicaset
webproject
scala-macros
fuseesb
subfolder
android-audiomanager
rebar
iplimage
quantify
full-text-indexing
webaii
principalcontext
cloo
saga
discovery
infinite
android-authenticator
cmath
simplecov
lynx
cgrectmake
kobold2d
acpi
n-queens
gwt-celltable
space-partitioning
kernel32
principles
facebook-fbml
ppc
firephp
fxruby
iphone-sdk-3.1.3
service-factory

Resources

Encrypt Message