我的链接模型有一种奇怪的行为:
用户模型:
public $hasMany = array(
'Place' => array(
'className' => 'Place',
'foreignKey' => 'user_id',
'dependent' => false,
)
);
放置模型:
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'dependent' => false,
)
);
当我$this->Place->find('all');
时,我看到一个空的数组结果,但当我删除$ belongsTo关联时,它会找到地点,但当然没有任何用户。问题出在哪儿?我是否需要添加hasAndBelongsToMany关联?
以下是所有两个型号代码:
AdPlace:
App::uses('AppModel', 'Model');
/**
* AdPlace Model
*
*/
class AdPlace extends AppModel {
public $name = 'AdPlace';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'url' => array(
'notempty' => array(
'rule' => array('notempty'),
),
'urlUnique' => array(
'rule' => 'isUnique',
'message' => 'Такой URL существует',
'required' => 'create',
'on' => 'create'
),
'urlValidate' => array(
'rule' => array('url', true),
'message' => 'Не валидный URL',
),
'urlRequestValidate' => array(
'rule' => array('requestUrl', true),
'message' => 'URL не найден'
)
),
'name' => array(
'notempty' => array(
'rule' => array('notempty')
)
)
);
public $belongsTo = array(
'User'
);
public function requestUrl($url) {
Configure::write('debug', 0);
return (boolean)get_headers($url['url']);
}
}
用户:
App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');
/**
* User Model
*
*/
class User extends AppModel {
public $name = 'User';
public $validate = array(
);
public $belongsTo = array(
'Role'
);
public $hasMany = array(
'AdPlace',
'News'
);
public function beforeSave($options = array()) {
if(isset($this->data['User']['password'])) {
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
return true;
}
public function afterFind($results, $primary = false) {
parent::afterFind($results, $primary);
}
public function encryptHash($user = array()) {
$hash = '';
if(!empty($user) && isset($user['User'])) {
$hash = base64_encode(
Security::rijndael(serialize($user['User']), Configure::read('Security.cipherSeed'), 'encrypt')
);
$hash = preg_replace('/\//', '::', $hash);
}
return (string) $hash;
}
public function decryptHash($hash = '') {
if(!empty($hash)) {
$hash = preg_replace('/::/', '/', $hash);
$hash = preg_replace('/\s/', '+', $hash);
$hash = base64_decode($hash);
$hash = @unserialize(Security::rijndael($hash, Configure::read('Security.cipherSeed'), 'decrypt'));
}
return $hash;
}
}