2014-09-22 64 views
0

我遇到了会话问题。用户可以有公司或城镇。对于城镇,我可以使用Auth.Town发布有关城市的信息(城镇用户ID =表镇镇的ID)。这对公司来说是一样的(company_id = id),问题是我无法创建会话:Auth.Company,存在连接问题,正如你可以在我的数组中看到的那样,我的id值是3,name是测试,这对应于公司3表的ID,当我应该有ID 16的信息。这是怎么回事?我的模型中有错误?与用户关联的模型的奇怪会话行为

[Auth] => Array 
     (
      [User] => Array 
       (
        [id] => 96 
        [email] => [email protected] 
        [avatar] => 
        [firstname] => Brian 
        [lastname] => Ferui 
        [created] => 2014-09-22 11:05:55 
        [modified] => 2014-09-22 11:05:55 
        [role] => user 
        [town_id] => 0 
        [company_id] => 16 
       ) 

      [Company] => Array 
       (
        [id] => 3 
        [name] => test 
        [legal_form] => 
       ) 

     ) 

型号用户:

<?php 

class User extends AppModel{ 


    public $actsAs = array(
    'Upload' => array(
     'fields' => array(
      'avatar' => 'img/avatars/:id1000/:id' 
    ) 
    ) 
); 

    public $hasOne = array(
     'Company' => array(
      'className' => 'Company', 
      'foreignKey' => 'id', 
      'dependent' => true 
     ), 
     'Town' => array(
      'className' => 'Town', 
      'foreignKey' => 'id', 
      'dependent' => true 
     ) 
    ); 

    public $recursive = -1; 

    public $order = 'User.id DESC'; 

    public $validate = array(

     'email' => array(

      'rule'  => 'isUnique', 

      'allowEmpty' => false, 

      'message' => "Ce adresse e-mail est déja prise ou vide..." 

     ), 
     'password' => array(
      array(
      'rule' => 'notEmpty', 
      'allowEmpty' => false, 
      'message' => 'Le mot de passe ne peux pas être vide...' 
     ), 
      array(
      'rule' => array('minLength', 4), 
      'message' => 'Le mot de passe doit avoir au moins 4 caractères...' 
     ) 
    ), 
     'firstname' => array(
      array(
      'rule' => 'notEmpty', 
      'message' => 'Le prénom ne peux pas être vide...' 
     ) 
    ), 
     'lastname' => array(
      array(
      'rule' => 'notEmpty', 
      'message' => 'Le nom ne peux pas être vide...' 
     ) 
    ), 
    'avatar_file' => array(
      array(
      'rule' => array('fileExtension', array('jpg','png')), 
      'message' => 'Extension invalide...' 
     ) 
    ) 
    ); 

    public function beforeSave($options = array()){ 

     if(!empty($this->data['User']['password'])){ 

      $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']); 

     } 

     return true; 

    } 

} 

示范企业:在UsersController

<?php 

class Company extends AppModel{ 

    public $actsAs = array(
    'Upload' => array(
     'fields' => array(
      'logo' => 'img/logos/:id1000/:id' 
    ) 
    ) 
); 

public $hasMany = array(
    'User' => array(
     'className' => 'User', 
     'foreignKey' => 'company_id', 
     'dependent' => false 
    )); 

    public $validate = array(
    'name' => array(
      array(
      'rule' => 'notEmpty', 
      'message' => 'Le nom de l\'entreprise ne peux pas être vide...' 
     ) 
    ), 
    'logo_file' => array(
      array(
      'rule' => array('fileExtension', array('jpg','png')), 
      'message' => 'Extension invalide...' 
     ) 
    ) 
    ); 

} 



?> 

功能登录

public function login() { 
    if ($this->request->is('post')) { 
     if (isset($this->request->data['UserLogin'])) { 
      $this->request->data['User'] = $this->request->data['UserLogin']; 
    } 

     if ($this->Auth->login()) { 

    if ($this->Auth->user('town_id') > '0') { 

    $towns = array('name', 'country', 'localisation', 'statut'); 
foreach($towns as $column) { 
    $this->Session->write(
     'Auth.Town.' . $column, 
     $this->User->Town->field($column) 
    ); 
} 

} 

    if ($this->Auth->user('company_id') > '0') { 

    $companies = array('name', 'legal_form'); 
foreach($companies as $column) { 
    $this->Session->write(
     'Auth.Company.' . $column, 
     $this->User->Company->field($column) 
    ); 
} 

} 

       return $this->redirect($this->Auth->redirect()); 

      }else{ 

       $this->Session->setFlash("L'adresse électronique ou votre mot de passe ne correspond pas","notif",array('type'=>'error')); 

      } 
    } 
} 

有什么奇怪的是,它为 “镇”,而不是为“公司”。 对于公司,“关联保存”工作在我的注册功能,所以连接功能,我无法恢复会话的值,为什么,我不知道,谢谢...

回答

1

从改变开始用户模型中的relationships从hasOne到belongsTo。

  • hasOne是1:1间的关系
  • 属于关联是M:1名

代码更改应该是:

public $belongsTo = array(
    'Company' => array(
     'className' => 'Company', 
     'foreignKey' => 'company_id' 
    ), 
    'Town' => array(
     'className' => 'Town', 
     'foreignKey' => 'town_id' 
    ) 
); 
+0

感谢;)它仍然是相同的,我想这是我的数组中使用的Auth.Company列的第一个id,它必须是company_id的相同值,所以16 ...它是w eird:/我是否也需要改变我的公司模式? – user3790914 2014-09-22 20:10:34

+1

@ user3790914 - 我相信你的登录代码假设在你使用'field'方法之前设置了公司和城镇。作为IF块的第一条语句,试试'$ this-> User-> Company-> id = $ this-> Auth-> user('company_id')'。 – AgRizzo 2014-09-22 20:59:09

+0

谢谢,你能编辑你的文章吗?我需要把这个代码放在哪里?我的意思是在哪种情况下? – user3790914 2014-09-22 21:02:12