cakephp model saveAssociated error - 不能将字符串偏移量用作数组

时间:2013-11-19 11:22:21

标签: php arrays cakephp model

我这里有一个梨形的。这让我疯狂,并且花了很多时间在它上面。我希望有人可以快速浏览一下并指出显而易见的事情。

我差不多完成了从1.3.14到2.4.2的cakephp升级。我遇到的其中一个问题是使用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,这不是一个数组。

现在,看看发布的数据,我看不出它有什么问题。它与cake1.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'      => ''
        )
    );

1 个答案:

答案 0 :(得分:0)

您的数据格式不正确,无法保存hasMany相关数据。查看docs for 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协会,很难说。