我想根据cakePHP3中的MVC范例编写我的代码:
我有一个公式来注册一个所有者,但作为一个拥有者基本上是一个有附加信息的用户,我有2个SQL表;一个用于指向所有者的用户,一个用于所有者。
这里的想法是用户不一定是所有者。
所以我目前编写代码,在OwnerController.php的add动作中执行我想要的操作。
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.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?
}
}
User.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);
}
}
UsersTable.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',
]);
}
你能告诉我两者之间的关系吗?
答案 0 :(得分:0)
首先,您应该规范化您的数据库(在您的Owners实体上,我可以看到您拥有一组用户的用户)。
另外,使用CakePHP 3命名约定:http://book.cakephp.org/3.0/en/intro/conventions.html
然后,因为这似乎是你进入CakePHP 3的第一步,你应该尝试烘焙(CakePHP Scaffolding)你的应用程序。
烘焙(CakePHP脚手架):http://book.cakephp.org/3.0/en/bake/usage.html
烘焙是一种了解如何在CakePHP 3中开始编码的好方法。
希望这有帮助。