在 cakephp 3 中保存 hasOne 关联
Saving a hasOne association in cakephp 3
我有两个模型(或 Cakephp 3 中的 Tables),其中一个是 Department,另一个是 Manager,我在这两者之间建立了 'hasOne' 关联:
部门Table(数据库):
------------------------
| id | name | location |
------------------------
| 1 | IT | New York |
------------------------
经理Table(数据库):
-----------------------------------------
| id | name | address | department_id |
-----------------------------------------
| 1 | Mike | New York | 1 |
-----------------------------------------
DepartmentsTable.php
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class DepartmentsTable extends Table {
public function initialize(array $config){
}
}
ManagersTable.php
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class ManagersTable extends Table {
public function initialize(array $config){
$this->hasOne('Departments');
}
}
我创建了一个表格,这样我就可以保存一个人的地址:
<?php echo $this->Form->create($manager); ?>
<?php echo $this->Form->input('Manager.name'); ?>
<?php echo $this->Form->input('Manager.address'); ?>
<?php echo $this->Form->input('Manager.department.name'); ?>
<?php echo $this->Form->input('Manager.department.location'); ?>
<?php echo $this->Form->end('valider'); ?>
这是控制器:
<?php
namespace App\Controller\Adminocity;
use App\Controller\AppController;
use Cake\Core\Configure;
use Cake\Network\Exception\NotFoundException;
use Cake\View\Exception\MissingTemplateException;
use Cake\Cache\Cache;
use Cake\ORM\TableRegistry;
use Cake\Event\Event;
class ManagersController extends AppController {
public function edit_manager($id=null)
{
$this->loadModel('Departments');
$manager = $this->Managers->newEntity();
if($this->request->is('post')){
$managersTable = TableRegistry::get('Managers');
$manager = $managersTable->patchEntity($manager,$this->request->data(), ['associated' => ['Departments']]);
if($managersTable->save($manager)){
$this->Flash->success(__('Manager ajouté avec succès'));
return $this->redirect(['action' => 'list_managers']);
}
}
$this->set('manager', $manager);
}
}
当我填写完表格后尝试提交时,'Departments' 和 'Manager' 两个新行添加成功,但 'Manager' 的新行的值为 'NULL' 在 'department_id' 列上,而不是最近创建的新 'Department' 行的 ID 值。
拜托,谁能帮我解决这个问题?
关系类型应该是belongsTo()
作为一般规则,您可以知道何时使用 belongsTo 或 hasOne:
- belongsTo:外键在同一个table(经理有一个department_id)
- hasOne:外键在另一个table。 (部门有一位经理)
我有两个模型(或 Cakephp 3 中的 Tables),其中一个是 Department,另一个是 Manager,我在这两者之间建立了 'hasOne' 关联:
部门Table(数据库):
------------------------
| id | name | location |
------------------------
| 1 | IT | New York |
------------------------
经理Table(数据库):
-----------------------------------------
| id | name | address | department_id |
-----------------------------------------
| 1 | Mike | New York | 1 |
-----------------------------------------
DepartmentsTable.php
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class DepartmentsTable extends Table {
public function initialize(array $config){
}
}
ManagersTable.php
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class ManagersTable extends Table {
public function initialize(array $config){
$this->hasOne('Departments');
}
}
我创建了一个表格,这样我就可以保存一个人的地址:
<?php echo $this->Form->create($manager); ?>
<?php echo $this->Form->input('Manager.name'); ?>
<?php echo $this->Form->input('Manager.address'); ?>
<?php echo $this->Form->input('Manager.department.name'); ?>
<?php echo $this->Form->input('Manager.department.location'); ?>
<?php echo $this->Form->end('valider'); ?>
这是控制器:
<?php
namespace App\Controller\Adminocity;
use App\Controller\AppController;
use Cake\Core\Configure;
use Cake\Network\Exception\NotFoundException;
use Cake\View\Exception\MissingTemplateException;
use Cake\Cache\Cache;
use Cake\ORM\TableRegistry;
use Cake\Event\Event;
class ManagersController extends AppController {
public function edit_manager($id=null)
{
$this->loadModel('Departments');
$manager = $this->Managers->newEntity();
if($this->request->is('post')){
$managersTable = TableRegistry::get('Managers');
$manager = $managersTable->patchEntity($manager,$this->request->data(), ['associated' => ['Departments']]);
if($managersTable->save($manager)){
$this->Flash->success(__('Manager ajouté avec succès'));
return $this->redirect(['action' => 'list_managers']);
}
}
$this->set('manager', $manager);
}
}
当我填写完表格后尝试提交时,'Departments' 和 'Manager' 两个新行添加成功,但 'Manager' 的新行的值为 'NULL' 在 'department_id' 列上,而不是最近创建的新 'Department' 行的 ID 值。
拜托,谁能帮我解决这个问题?
关系类型应该是belongsTo()
作为一般规则,您可以知道何时使用 belongsTo 或 hasOne:
- belongsTo:外键在同一个table(经理有一个department_id)
- hasOne:外键在另一个table。 (部门有一位经理)