0

I've got a Cakephp Project with an 'Addresses' table with the following structure:

CREATE TABLE `addresses` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`name` varchar(50) NOT NULL,
`company` varchar(50) NOT NULL,
`address1` varchar(50) NOT NULL,
`address2` varchar(50) DEFAULT NULL,
`city` varchar(40) NOT NULL,
`state` varchar(2) NOT NULL,
`country` varchar(2) NOT NULL,
`zip` varchar(5) NOT NULL,
PRIMARY KEY (`id`)
)

There is a page in this project which asks the user for both a Shipping Address and a Billing Address, and im not certain how to structure the names of the form inputs to allow multiple instances of the same database fields on one page

In the View, I've attempted to use an alias to seperate the two instances of the Address fields

I.E.-

<?=$this->Form->input('Shipaddress.zip', array('label' => 'Zip Code'));?>
...
<?=$this->Form->input('Billaddress.zip', array('label' => 'Zip Code'));?>

then in the view, i tried to seperate the two instances, validate both, and set the appropriate $this->validationError values to properly display the errors to the correct field views

// place in arrays with proper model name ['Address']
$ship_array['Address'] = $this->request->data['Shipaddress'];
$bill_array['Address'] = $this->request->data['Billaddress'];

//Set Data to model, Validate Against model, change model name in validationErrors to  match aliased fields, and remove validationErrors for ['Address']
$this->Address->set($ship_array);
$shipping_valid = $this->Address->validates(array('fieldList' => array('name', 'company', 'address1', 'address2', 'city', 'state', 'country', 'zip')));
$this->validationErrors['Shipaddress'] = $this->validationErrors['Address'];
$this->validationErrors['Address'] = array();

//Do it again for Billing Address fields
$this->Address->set($bill_array);
$billing_valid = $this->Address->validates(array('fieldList' => array('name', 'company', 'address1', 'address2', 'city', 'state', 'country', 'zip')));
$this->validationErrors['Billaddress'] = $this->validationErrors['Address'];
$this->validationErrors['Address'] = array();

unfortunately, this doesnt appear to work, and i'm afraid that I've gone too far trying to make this work...

can someone give my a kick in the right direction on how this can be done properly?

user782161
  • 63
  • 6

1 Answers1

0

Figured out how to do it on my own...

in /app/Model i created 'ShippingAddress.php' and 'BillingAddress.php', Both Extend "Address"

//ShippingAddress.php
<?php  
App::uses('Address', 'Model');
class ShippingAddress extends Address { 
} 

//BillingAddress.php
<?php  
App::uses('Address', 'Model');
class BillingAddress extends Address { 
} 

To prevent the new models from using tables named after them, we edit the parent Address.php and set $useTable so that both extended models use Addresses Table

//Address.php
...
public $useTable = 'addresses';
...

then its just a matter of inserting the two instances of the input fields into the view... no renaming models, no modifying validationErrors, it just works :)

user782161
  • 63
  • 6