0

我的链接模型有一个奇怪的行为:

用户型号:

    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 关联吗?

这是所有两个模型的代码:

广告位:

    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;
    }

}
4

0 回答 0