cakepp模型saveAssociated错误-不能将字符串偏移量用作数组


cakephp model saveAssociated error - Cannot use string offset as an array

我这里有一个梨形的。这让我抓狂,我花了一天的大部分时间。我希望有人能快速浏览一下,并指出显而易见的地方。

我几乎已经完成了从1.3.14到2.4.2的cakehpp升级。我遇到的一个遗留问题是saveAll模型调用。。。它实际上是正确地插入第一个数组的数据,但对其余数组抛出异常

我得到的错误是:

Error: Cannot use string offset as an array 
File: /srv/www/test/public_html/app/Lib/cakephp/lib/Cake/Model/Model.php    
Line: 2453

如果我查看Model.php,问题出现在这里:

        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

我在这里转储了输出,本例中$values[$I][$association]=3,它不是数组。

现在,看看发布的数据,我看不出有什么问题。它和Cake.3.14中的工作一样。

发布数据的var_dump:

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"
  }
}

控制器的一个片段:

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);
                }
            }
        }
}

以下是有问题的相关模型:

客户:

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'      => ''
        )
    );

您的数据格式不正确,无法保存hasMany关联数据。检查文档中的Model::saveAssociated()

为了保存一条记录及其具有hasMany关联的相关记录,数据数组应该是这样的:

$data = array(
    'Article' => array('title' => 'My first article'),
    'Comment' => array(
        array('body' => 'Comment 1', 'user_id' => 1),
        array('body' => 'Comment 2', 'user_id' => 12),
        array('body' => 'Comment 3', 'user_id' => 40),
    ),
);

因此,您的ClientRateGroup密钥应该包含一个数据数组数组,类似于:

'ClientRateGroup' => array(
    array('rate_group_id' => 3)
)

也许你的ClientExtra数据也应该这样格式化,不确定,因为Client模型上没有合适的关联,很可能这实际上应该是Client hasOne ClientExtra关联,很难说。