I have a pear shaped one here. It's driven me nuts and have spent a good part of a day on it. I am hoping someone can have a quick skim through this and point out the obvious.
I've almost completed a cakephp upgrade from 1.3.14 to 2.4.2. One of the remaining issues I am having is with a saveAll model call... it's actually inserting the data for the first array correctly, but throwing an exception with the rest.
The error I'm getting is:
Error: Cannot use string offset as an array
File: /srv/www/test/public_html/app/Lib/cakephp/lib/Cake/Model/Model.php
Line: 2453
If I look at Model.php, the issue arises here:
case 'hasMany':
foreach ($values as $i => $value) {
if (isset($values[$i][$association])) {
$values[$i][$association][$key] = $this->id;
} else {
$values[$i] = array_merge(array($key => $this->id), $value, array($key => $this->id));
}
}
$values[$i][$association][$key] = $this->id;
I've dumped the output here, and this case $values[$i][$association] = 3, which isn't an array.
Now, looking at the data posted I can't see anything wrong with it. It is the same as it was working in cake1.3.14.
A var_dump of the post data:
array(3) {
["Client"]=>
array(20) {
["NAME"]=>
string(6) "ZZTEST"
["PHONE"]=>
string(0) ""
["X_PHONE2"]=>
string(0) ""
["FAX"]=>
string(0) ""
["EMAIL"]=>
string(0) ""
["X_REQUIRE_PO"]=>
string(1) "N"
["TAXREG"]=>
string(0) ""
["CREDLIMIT"]=>
string(5) "10000"
["X_QCHECKAPP"]=>
string(1) "Y"
["X_PUBLIC_LIABILITY_EXPIRY"]=>
string(10) "19-03-2014"
["ADDRESS1"]=>
string(0) ""
["ADDRESS2"]=>
string(0) ""
["POST_CODE"]=>
string(0) ""
["DELADDR1"]=>
string(0) ""
["DELADDR2"]=>
string(0) ""
["DELADDR6"]=>
string(0) ""
["NOTES"]=>
string(0) ""
["HEAD_ACCNO"]=>
int(-1)
["CURRENCYNO"]=>
int(0)
["AVE_DAYS_TO_PAY"]=>
int(-1)
}
["ClientRateGroup"]=>
array(1) {
["rate_group_id"]=>
string(1) "3"
}
["ClientExtra"]=>
array(2) {
["client_type_id"]=>
string(1) "1"
["client_salesperson_id"]=>
string(2) "25"
}
}
A snippet of the controller:
class ClientsController extends AppController
{
public $name = 'Clients';
public $uses = array('ExonetClient', 'Quote');
// The saveAll call is causing the problem
function add()
{
if (!empty($this->request->data)) {
$this->request->data['ExonetClient']['HEAD_ACCNO'] = -1;
$this->request->data['ExonetClient']['CURRENCYNO'] = 0;
$this->request->data['ExonetClient']['AVE_DAYS_TO_PAY'] = -1;
$this->Client->create();
unset($this->ExonetClient->ClientRateGroup->validate['client_id']);
unset($this->ExonetClient->ClientExtra->validate['client_id']);
// Allow an entry to clients_rate_groups table if the rate group is
//selected for a client
if (!empty($this->request->data['ClientRateGroup']['rate_group_id'])
|| !empty($this->request->data['ClientExtra']['client_type_id'])
) {
if ($this->ExonetClient->saveAll($this->request->data, array('validate' => true)) === true) {
//
// If an AJAX request, then echo the ID of the item.
if ($this->RequestHandler->isAjax()) {
echo $this->ExonetClient->getLastInsertId();
exit;
}
else {
$this->Session->setFlash(
__('The client has been saved successfully'),
'default',
array('class' => 'success')
);
$this->redirect(array('action' => 'index'));
}
}
else {
$error = __('The client could not be saved. Please, try again.');
if ($this->RequestHandler->isAjax()) {
$this->set('error_message', $error);
}
else {
$this->Session->setFlash($error);
}
}
}
}
Here are the associated models in question:
Client:
App::uses('AppModel', 'Model');
class ExonetClient extends AppModel
{
public $name = 'ExonetClient';
public $actsAs = array(
'CakephpAssets.Logable' => array('change' => 'serialize', 'userModel' => 'User', 'userKey' => 'user_id'),
'LocationStorage' => array(),
);
public $useDbConfig = 'exonet';
public $useTable = 'DR_ACCS';
public $primaryKey = 'ACCNO';
public $displayField = 'NAME';
public $hasMany = array(
'ContactLink' => array(
'className' => 'ExonetClientContact',
'foreignKey' => 'ACCNO',
'dependent' => true,
),
'SubcontractorIncompatibility' => array(
'className' => 'ClientsSubcontractor',
'foreignKey' => 'client_id',
'dependent' => true,
),
'Docket' => array(
'className' => 'Docket',
'foreignKey' => 'client_id',
'dependent' => false,
),
'SpreadsheetQueue' => array(
'className' => 'SpreadsheetQueue',
'foreignKey' => 'client_id',
'dependent' => false,
),
'Spreadsheet' => array(
'className' => 'Spreadsheet',
'foreignKey' => 'client_id',
'dependent' => false,
),
'ClientRateGroup' => array(
'className' => 'ClientRateGroup',
//'joinTable' => 'clients_rate_groups',
'foreignKey' => 'client_id',
//'associationForeignKey' => 'client_id',
//'unique' => true,
'dependent' => true,
'conditions' => '',
//'fields' => '',
'order' => array('effective_date' => 'DESC', 'expiry_date' => 'DESC'),
'limit' => '',
'offset' => '',
'finderQuery' => ''
//'deleteQuery' => '',
//'insertQuery' => ''
),
);
ClientExtra:
App::uses('AppModel', 'Model');
class ClientExtra extends AppModel
{
public $name = 'ClientExtra';
public $validate = array(
'client_type_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'allowEmpty' => true,
),
),
'client_id' => array(
'numeric' => array(
'rule' => array('numeric'),
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
public $belongsTo = array(
'ClientType' => array(
'className' => 'ClientType',
'foreignKey' => 'client_type_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Client' => array(
'className' => 'ExonetClient',
'foreignKey' => 'client_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Salesperson' => array(
'className' => 'Salesperson',
'foreignKey' => 'client_salesperson_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
ClientRateGroup:
App::uses('AppModel', 'Model');
class ClientRateGroup extends AppModel
{
public $useTable = 'clients_rate_groups';
public $name = 'ClientRateGroup';
public $validate = array(
'rate_group_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'allowEmpty' => true,
),
),
'client_id' => array(
'numeric' => array(
'rule' => array('numeric'),
),
),
'effective_date' => array(
'date' => array(
'rule' => array('date'),
),
),
);
public $belongsTo = array(
'RateGroup' => array(
'className' => 'RateGroup',
'foreignKey' => 'rate_group_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Client' => array(
'className' => 'ExonetClient',
'foreignKey' => 'client_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);