Php 有许多属于唐的东西';不行?
我的链接模型有一种奇怪的行为: 用户型号:Php 有许多属于唐的东西';不行?,php,cakephp,model,associations,Php,Cakephp,Model,Associations,我的链接模型有一种奇怪的行为: 用户型号: public $hasMany = array( 'Place' => array( 'className' => 'Place', 'foreignKey' => 'user_id', 'dependent' => false, ) ); 放置模型: public $belongsTo = array( 'User' => arra
public $hasMany = array(
'Place' => array(
'className' => 'Place',
'foreignKey' => 'user_id',
'dependent' => false,
)
);
放置模型:
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'dependent' => false,
)
);
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;
}
}
当我做$this->Place->find('all')代码>我看到一个空数组结果,但是当我删除$belongsTo关联时,它会找到位置,当然没有任何用户。问题在哪里?我需要添加HasandBelongTomany关联吗
以下是所有两种型号的代码:
AdPlace:
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'dependent' => false,
)
);
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;
}
}
用户:
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'dependent' => false,
)
);
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;
}
}
你能给我们看一下你剩下的模型代码吗?我已经在帖子中添加了代码,外接程序控制器调试($this->AdPlace->find('all');当我做$this->Place->find('all')时,你说的是;我看到一个空数组结果
。是AdPlace
还是Place
?调试模式是否设置为0
?因为它应该抛出一个关联不存在的错误。因为模型本身被称为AdPlace
。