0
我對以下CakePHP結果有些困惑。我包含了兩個Model和一個Controller的代碼(剝去了不必要的東西)。CakePHP saveAll/saveAssociated問題
問題:一切正常保存。唯一的問題是store_users表的user_id字段沒有保存。
有什麼顯而易見的,你可以看到我做錯了?我嘗試了saveAssociated和saveAll。
型號/ Store.php
<?php
App::uses('AppModel', 'Model');
class Store extends AppModel {
public $belongsTo = array(
'User'
);
public $hasMany = array(
'StoreUser'
);
}
型號/ StoreUser.php
<?php
App::uses('AppModel', 'Model');
class StoreUser extends AppModel {
public $belongsTo = array(
'User',
'Store'
);
}
控制器/ StoresController.php
<?php
App::uses('AppController', 'Controller');
class StoresController extends AppController {
public $uses = array('Store');
public function create() {
$this->Store->create();
$storeData = array(
'Store' => array(
'title' => 'New Store',
'user_id' => $this->Auth->user('id')
),
'StoreUser' => array(
'user_id' => $this->Auth->user('id')
)
);
if($this->Store->saveAll($storeData) !== false) {
// Success
} else {
// Error
}
}
}
個結果DB
stores table:
id: 1
title: New Store
user_id: 1
...
store_users table:
id: 1
store_id: 1
user_id: 0
...