2014-11-20 43 views
0

我想根據MVC範例中cakePHP3寫我的代碼: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('[email protected]') 
        ->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

你能展示模型嗎? – 2014-11-21 10:00:56

+0

@may saghira,是的,我編輯了我的文章。 – 2ndGAB 2014-11-21 12:06:21

+0

哪裏的兩個模型之間的關係?我沒有看到任何belongsTo或hasMany? – 2014-11-21 14:00:10

回答

0

首先,你應該正常化您的數據庫(在你的業主單位,我可以看到你的用戶是用戶的一個數組)。

另外,使用CakePHP 3命名約定:http://book.cakephp.org/3.0/en/intro/conventions.html

然後,因爲這似乎是你的第一個步驟到CakePHP的3,你應該嘗試烘(CakePHP的腳手架)應用程序。

烘烤(CakePHP的腳手架):http://book.cakephp.org/3.0/en/bake/usage.html

烘焙的是看你如何開始在CakePHP中編碼3

希望這有助於一個偉大的方式。