我有一个用户模型,它通过与公司模型的关系链接。 它完美地运作。问题是因为我添加了关系,当我想要被隐藏时,查询会与公司建立一个查询。
我不想使用公司模型进行登录操作。
当我点击提交按钮时,这就是我得到的:
数据库错误
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Company.user_id' in 'on clause'
SQL Query: SELECT `User`.`id`, `User`.`username`, `User`.`password`, `User`.`role`, `User`.`town_id`, `User`.`company_id`, `User`.`test_id`, `Company`.`id`, `Company`.`name` FROM `users` AS `User` LEFT JOIN `companies` AS `Company` ON (`Company`.`user_id` = `User`.`id`) WHERE `User`.`username` = 'CREAZ' AND `User`.`password` = '04fb76559109f9bbb69fc6805fccedb6a3b79982' ORDER BY `User`.`id` DESC LIMIT 1
company_id对于用户表并不存在,它仅适用于公司表,这就是它无法运作的原因。
我是否需要更改appController中的Auth部分或更改UsersController的操作登录中的内容?
UserController(登录操作):
public function login(){
if($this->request->is('post')){
if($this->Auth->login()){
return $this->redirect($this->Auth->redirect());
}else{
$this->Session->setFlash("Votre login ou votre mot de passe ne correspond pas","notif",array('type'=>'error'));
}
}
}
登录视图
<div class="page-header">
<h1>Se connecter</h1>
</div>
<?php echo $this->Form->create('User'); ?>
<?php echo $this->Form->input('username',array('label'=>"Login")); ?>
<?php echo $this->Form->input('password',array('label'=>"Mot de passe")); ?>
<?php echo $this->Form->end('Se connecter'); ?>
AppController:
<?php
class AppController extends Controller{
public $helpers = array('Text','Form','Html','Session','Cache');
public $components = array('Session','Auth');
function beforeFilter(){
$this->Auth->loginAction = array('controller'=>'users','action'=>'login','admin'=>false);
$this->Auth->authorize = array('Controller');
if(!isset($this->request->params['prefix'])){
$this->Auth->allow();
}
if(isset($this->request->params['prefix']) && $this->request->params['prefix'] == 'admin'){
$this->layout = 'admin';
}
}
function isAuthorized($user){
if(!isset($this->request->params['prefix'])){
return true;
}
$roles = array(
'admin' => 10,
'user' => 5
);
if(isset($roles[$this->request->params['prefix']])){
$lvlAction = $roles[$this->request->params['prefix']];
$lvlUser = $roles[$user['role']];
if($lvlUser >= $lvlAction){
return true;
}else{
return false;
}
}
return false;
}
}
用户模型:
<?php
class User extends AppModel{
public $actsAs = array('Containable');
public $hasOne = array(
'Company' => array(
'className' => 'Company',
'dependent' => true
)
);
public $recursive = -1;
public $order = 'User.id DESC';
public $validate = array(
'username' => array(
'rule' => 'isUnique',
'allowEmpty' => false,
'message' => "Ce nom d'utilisateur est déja pris"
),
'password' => array(
'rule' => 'notEmpty',
'message' => "Vous ne pouvez pas entrer de mot de pase"
)
);
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 $hasMany = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'company_id',
'dependent' => false
));
}
?>
答案 0 :(得分:1)
尝试更改用户模型以使用$ belongsTo,而不是$ hasOne
public $belongsTo = array(
'Company' => array(
'className' => 'Company',
'foreignKey' => 'company_id'
);