【问题标题】:Weird session ehavior with a model which is associated to User与用户关联的模型的奇怪会话行为
【发布时间】:2014-09-23 03:32:24
【问题描述】:

我的会话有问题。用户可以拥有公司或城镇。对于城镇,我可以用Auth.Town 发布有关城市的信息(table users 的town_id = 表格城镇的id)。公司也是一样(company_id = id),问题是我无法创建会话:Auth.Company,存在连接问题,正如您在我的数组中看到的那样,我的 id 值为 3,名称为测试一下,这个对应company 3表的id,我应该有id 16的信息,这是怎么回事?我的模型有错误?

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

            }
    }
}

奇怪的是它适用于“Town”,但不适用于“Company”。 对于公司,“关联保存”在我的注册功能中起作用,因此连接正常,我无法恢复会话的值,为什么,我不知道,谢谢...

【问题讨论】:

    标签: cakephp


    【解决方案1】:

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

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

    代码更改应该是:

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

    【讨论】:

    • 谢谢 ;) 还是一样,我猜它是我的数组中用于 Auth.Company 的列的第一个 id,它必须与 company_id 的值相同,所以 16 .. . 这很奇怪:/ 我也需要改变我的公司模式吗?
    • @user3790914 - 我相信您的登录代码假定公司和城镇是在您使用 field 方法之前设置的。尝试 $this-&gt;User-&gt;Company-&gt;id = $this-&gt;Auth-&gt;user('company_id') 作为 IF 块中的第一条语句。
    • 谢谢,你能编辑你的帖子吗?我必须把这段代码放在哪里?我的意思是在哪种情况下?
    • 天哪,它有效!我在第二个声明中写了这个 coe。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2013-06-27
    • 2014-03-03
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多