Cakephp 具有与用户关联的模型的怪异会话ehavior

Cakephp 具有与用户关联的模型的怪异会话ehavior,cakephp,Cakephp,我对会话有问题。用户可以拥有公司或城镇。对于town,我可以使用Auth.town发布有关该城市的信息(town_id of the table users=table towns的id)。对于公司(company_id=id)也是一样,问题是我不能创建会话:Auth.company,有一个连接问题,正如你在我的数组中看到的,我的id值是3,name是test,这对应于company 3表的id,而我应该有id 16的信息。怎么回事?我的模型有错误吗 [Auth] => Array

我对会话有问题。用户可以拥有公司或城镇。对于town,我可以使用Auth.town发布有关该城市的信息(town_id of the table users=table towns的id)。对于公司(company_id=id)也是一样,问题是我不能创建会话:Auth.company,有一个连接问题,正如你在我的数组中看到的,我的id值是3,name是test,这对应于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; 

    }

}
奇怪的是,它适用于“城镇”,但不适用于“公司” 对于公司,“关联保存”在我的注册功能中起作用,因此连接是功能性的,我无法恢复值​​对于会话,为什么,我不知道,谢谢你…

首先将用户模型中的从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'
    )
);

谢谢;)它仍然是一样的,我猜它是我的Auth.Company数组中使用的列的第一个id,它必须与Company_id的值相同,所以16。。。这很奇怪:/我也需要改变我的公司模式吗?@user3790914-我相信你的登录代码假设公司和城镇在你使用
字段之前就已经设置好了。尝试将
$this->User->Company->id=$this->Auth->User('Company_id')
作为IF块中的第一条语句。谢谢,你能编辑你的帖子吗?我必须把这个代码放在哪里?我是说在哪种情况下?天哪,它能工作!我在第二份声明中写了这个coe。谢谢!
    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'));

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