gpt4 book ai didi

orm - 在 CakePHP 3 Controller 中保存相关数据

转载 作者:行者123 更新时间:2023-12-04 05:51:22 27 4
gpt4 key购买 nike

在文档中确切地说关联数据可以像这样保存:

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

我在我的代码中试过这个但是我得到错误:

fatal error 错误:找不到类“App\Controller\TableRegistry”
文件/Users/mtkocak/Sites/gscrm/src/Controller/BusinessesController.php行:62

这是我的 Controller 代码。我怀疑上面的代码对实体模型是否有效。

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

感谢任何帮助。

最佳答案

关于orm - 在 CakePHP 3 Controller 中保存相关数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27094959/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com