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

how to use select item in select-option php
slim Authorization is not working after a while
Why when I put this piece of code in the file I get HTTP ERROR 500?
Laravel return back with post data
Magento Product Custom Ordering
Slim 2.6.2 render() Only Works for Index.html
looping through json from mysql or xcode
PHP Gearman too much mysql connections
Is it possible to send XML format with PHP to sql server
How to impliment links in yii 1?
Failed to open stream: FTP server reports STOR FTP wrapper
facebook sdk and .htaccess redirects
How to pass image Star Rating to Jpeg image using PEL Exif library
PHPExcel For loop data to array conversion
Get the difference between two dates in every rows
usort() drops indexes of ingested arrays leading to “Undefined Index” Error

Categories

HOME
cocoapods
amazon-web-services
oracle
entity-framework
events
extjs
vagrant
focus
enums
systemd
antd
message-queue
twitter-fabric
lvm
greasemonkey
ipv6
impala
supercollider
specifications
msmq
slide
weex
managed-c++
crud
hdmi
pyspark-sql
autodesk-model-derivative
vcenter
payeezy
db2-luw
xerces-c
textmate2
cargo
installanywhere
telephonymanager
fiware-wirecloud
spongycastle
intellij-lombok-plugin
aws-kinesis-firehose
tinyioc
nonlinear-functions
amazon-elastic-beanstalk
sharpdx
gestures
gtk#
1010
android-4.2-jelly-bean
wolfram-language
node.js-connect
deferred-rendering
simian
exists
preferenceactivity
cryptojs
unhandled
uitest
swagger-maven-plugin
deque
gflags
r.java-file
subview
nanomsg
google-hangouts
iplimage
igor
nachos
neo4jphp
fileshare
air-native-extension
oxygene
jchartfx
jms-serializer
principalcontext
coalesce
quickgraph
dynamic-rdlc-generation
arel
significant-digits
gamesalad
liveconnect
expression-web
filelock
office-automation
android-2.1-eclair
ios-4.2
resharper-5.0
zend-test
comment-conventions
downloadfile

Resources

Encrypt Message