我想根据 cakePHP3 中的 MVC 范例编写我的代码:
我有一个公式来注册所有者,但作为所有者基本上是具有附加信息的用户,我有 2 个 SQL 表;一个用于指向所有者的用户,一个用于所有者。
这里的想法是用户不一定是所有者。
所以我目前编写了代码,它在 OwnerController.php 中的添加操作中执行我想要的操作。
public function add() {
$this->loadModel('Users');
$user = $this->Users->newEntity($this->request->data);
$owner = $this->Owners->newEntity($this->request->data);
$addDatas = [
'owner_id' => 'id',
'email' => $owner['email'],
'role' => 'owner',
'token' => md5(time() . '-' . uniqid()),
];
$user = $this->Users->patchEntity($user, $addDatas);
$owner->users = [$user];
if ($this->request->is('post')) {
if ($this->Owners->validate($owner)) {
if ($this->Owners->save($owner)) {
$email = new Email('gmail');
$email->template('activationLink')
->emailFormat('text')
->to($owner['email'])
->from('myemail@monsite.fr')
->subject(__('Votre inscription sur monsite.fr'))
->viewVars(['user' => $user, 'id' => $user['id']])
->send();
$this->Flash->success(__("Merci de vous être enregistré. un email a été envoyé à {0} pour activer votre compte", $owner['email']));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__("Impossible de vous enregistrer, veuillez corriger les erreurs"));
} else {
$this->Flash->error(__("Impossible de vous enregistrer, veuillez corriger les erreurs"));
}
}
$this->set(compact('owner'));
}
现在,我想这样写:
public function add() {
$owner = $this->Owners->newEntity($this->request->data);
if ($this->request->is('post')) {
if ($owner->addOwner($this->request->data)) {
$this->Flash->success(__("Merci de vous être enregistré. un email a été envoyé à {0} pour activer votre compte", $owner['email']));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__("Impossible de vous enregistrer, veuillez corriger les erreurs"));
}
}
$this->set(compact('owner'));
}
所以,我会在我的 Owner.php 中有一个 addOwner() 操作,让他做所有需要的事情。
我的问题是我在文档中没有看到如何从我的 Owner 模型访问我的 User 模型,更准确地说,如何使 User 模型验证并保存其记录。
也许我错了,但我在教程中并没有真正看到这种编码方式。
所有者.php
class Owner extends Entity {
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* @var array
*/
protected $_accessible = [
'email' => true,
'phone' => true,
'company' => true,
'tva_number' => true,
'address' => true,
'postcode' => true,
'city' => true,
'users' => true,
];
public function addOwner($data = array()) {
// This is where I want to create both the owner and the user
// as the owner is basically a user with additional contact infos.
// But how to access the User model here to realize the validation
// as save the record?
}
}
用户.php
class User extends Entity {
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* @var array
*/
protected $_accessible = [
'username' => true,
'password' => true,
'first_name' => true,
'last_name' => true,
'email' => true,
'role' => true,
'owner' => true,
'sites_user' => true,
'active' => true,
'token' => true,
];
protected function _setPassword($password) {
return (new DefaultPasswordHasher)->hash($password);
}
}
用户表.php
public function initialize(array $config) {
$this->table('users');
$this->displayField('id');
$this->primaryKey('id');
$this->belongsTo('Owners', [
'foreignKey' => 'owner_id',
]);
$this->belongsTo('SitesUsers', [
'foreignKey' => 'sites_users_id',
]);
}
OwnersTable.php
public function initialize(array $config) {
$this->table('owners');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->hasMany('Users', [
'foreignKey' => 'owner_id',
]);
}
你能告诉我两者之间的关系吗?