2014-11-23 74 views
2

在文檔準確說,相關數據可以保存,如:保存相關數據在CakePHP中3控制器

use App\Model\Entity\Article; 
use App\Model\Entity\User; 

$article = new Article(['title' => 'First post']); 
$article->user = new User(['id' => 1, 'username' => 'mark']); 

$articles = TableRegistry::get('Articles'); 
$articles->save($article); 

我想這在我的代碼,但我得到的錯誤:

致命錯誤 錯誤:類 '應用程序\控制器\ TableRegistry' 未找到
文件/Users/mtkocak/Sites/gscrm/src/Controller/BusinessesController.php 行:62

這裏是我的控制器代碼。我懷疑上面的代碼對於實體模型是有效的。

public function add() { 
    $business = $this->Businesses->newEntity($this->request->data); 
    $record = new Record($this->request->data['Records']); 

    $address = new Address($this->request->data['Addresses']); 
    $telephone = new Telephone($this->request->data['Telephones']); 
    $email = new Email($this->request->data['Emails']); 

    $record->business = $business; 
    $record->address = $address; 
    $record->email = $email; 

    if ($this->request->is('post')) { 
     var_dump($this->request->data['Records']); 
     $records = TableRegistry::get('Records'); 
     $records->save($record); 
     // if ($this->Businesses->save($business)) { 
     //  $this->Flash->success('The business has been saved.'); 
     //  return $this->redirect(['action' => 'index']); 
     // } else { 
     //  $this->Flash->error('The business could not be saved. Please, try again.'); 
     // } 
    } 
    $telephonetypes = $this->Businesses->Records->Telephones->Telephonetypes->find('list'); 
    $records = $this->Businesses->Records->find('list'); 
    $businesstypes = $this->Businesses->Businesstypes->find('list'); 
    $this->set(compact('telephonetypes','business', 'records', 'businesstypes')); 
} 

這裏是表的我的SQL轉儲:

CREATE TABLE IF NOT EXISTS `businesses` (
    `id` int(10) unsigned NOT NULL, 
    `record_id` int(10) unsigned DEFAULT NULL, 
    `businesstype_id` int(10) unsigned DEFAULT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_turkish_ci AUTO_INCREMENT=1 ; 

ALTER TABLE `businesses` 
ADD PRIMARY KEY (`id`), ADD KEY `FK_businesses_records` (`record_id`), ADD KEY `FK_businesses_businesstypes` (`businesstype_id`); 

任何幫助表示讚賞。

回答

7
+0

我有 「使用」 的語句和我當仍然得到錯誤。見[這個問題](http://stackoverflow.com/questions/34157865/why-am-i-getting-tableregistry-not-found-in-cakephp-3-0)。 – 2015-12-08 15:52:53