与用户关联的模型的奇怪会话行为

时间:2014-09-22 17:07:22

标签: cakephp

我遇到了会话问题。用户可以拥有公司或城镇。对于小镇,我可以使用Auth.Town发布有关城市的信息(table_id表用户=表城镇的id)。这对公司来说是一样的(company_id = id),问题是我无法创建会话:Auth.Company,存在连接问题,你可以在我的数组中看到我有一个id值是3,名称是测试,当我应该有id 16的信息时,这对应于公司3表的id。它是怎么回事?我的模特出错了?

 [Auth] => Array
        (
            [User] => Array
                (
                    [id] => 96
                    [email] => jetest@test.com
                    [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; 

    }

}

模特公司:

<?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...'
          )
      )
    );

}



?>

在UsersController中登录函数

    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'));

            }
    }
}

奇怪的是它适用于&#34; Town&#34;但不适用于&#34;公司。&#34; 对于公司,&#34;相关保存&#34;在我的注册功能工作,所以连接功能正常,我无法恢复会话的值,为什么,我不知道,谢谢...

1 个答案:

答案 0 :(得分:1)

首先将用户模型中的relationships从hasOne更改为belongsTo。

  • hasOne 适用于1:1关系
  • belongsTo 适用于M:1

代码更改应为:

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