cakephp保存没有外键的相关模型

时间:2015-03-11 10:48:32

标签: cakephp

我想使用另一个参数

创建一个新模型
class Beacon extends AppModel {

/**
 * Display field
 *
 * @var string
 */
    public $displayField = 'name';

/**
 * Validation rules
 *
 * @var array
 */
    public $validate = array(
        'UUID' => array(
            'uuid' => array(
                'rule' => array('uuid'),

            ),
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * hasOne associations
 *
 * @var array
 */
    public $hasOne = array(
        'Position' => array(
            'className' => 'Position',
            'foreignKey' => 'beacon_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
}

我想ping这个信标传递参数信标UUID而不是主键。

class Ping extends AppModel {

//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'Beacon' => array(
            'className' => 'Beacon',
            'foreignKey' => 'beacon_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
}

如何在没有beacon primary_key但是UUID?

的情况下创建Ping对象 在ping控制器上我想添加

public function pingBeaconWithUUID($uuid) {

我该如何管理? 谢谢!

1 个答案:

答案 0 :(得分:0)

如果UUID是唯一的,您可以将其用作id字段的值。 id不需要是整数,尽管它更可取。

但我也建议使用AgRizzo的方法并查找id。