【问题标题】:CakePHP hasOne-belongsTo relationship, how to look up and insert foreign key from user input?CakePHP hasOne-belongsTo 关系,如何从用户输入中查找和插入外键?
【发布时间】:2014-01-28 20:54:00
【问题描述】:

我有两张桌子:

Property (..., postcode_id, ...)
Postcode (id, postcode, latitude, longitude)

关系:

class Property extends AppModel {
    public $belongsTo = array(
        'Postcode' => array(
            'className' => 'Postcode'
        )
    );

class Postcode extends AppModel {
    public $hasMany = array(
        'Property' => array(
            'className' => 'Property'
       )
    );

我希望发生的事情:

  1. 在属性添加表单中输入邮政编码。
  2. 检查输入的邮政编码是否在邮政编码表中。
  3. 如果没有,那么一些逻辑(可能添加它,现在只返回验证错误)。
  4. 如果是,则在 Properties.postcode_id(外键)中记录 Postcode.id。

我想不出用蛋糕做这个的最佳方法。执行检查并将其添加到要添加的数据的自定义属性验证函数?还是在 beforeValidation 中?还是 Cake 会处理这个问题?

谢谢!

编辑

根据答案,我认为这是最好的解决方案...

我认为这是更好的方法,因为它也可以验证。感谢 Anubhav 让我走上正轨!

根据 Anubhav 的回答,在 Postcode 模型中:

public function checkPostcode($postcode = null){
    $record = $this->find('first', array(
            'conditions'=>array('postcode' => $postcode),
            'fields' => array('id')
        ));
    if (!empty($record)){
        return $record['Postcode']['id'];
    } else {
        return false;
        // Logic for finding and adding new postcode
    } 
}

但是在属性模型中:

public $validate = array(
    'postcode' => array(
        'exists' => array(
            'rule' => 'postcodeExists',
            'message' => 'Postcode does not exist'
        )
    )
);

public function postcodeExists($check) {
    $id = $this->Postcode->checkPostcode($check);
    if ($id) {
        $this->data['Property']['postcode_id'] = $id;
        return true;
    } 
    return false;
}

public function beforeSave($options = array()) {
    unset($this->data['Property']['postcode']);
}

通过修改要保存在这里的值,控制器保持精简,并且,与其确保邮政编码是真实的然后找到 id,两者都是同时完成的,因此只需要一个查询。

【问题讨论】:

  • 你有什么问题?
  • 如何在 Postcode 表中的 Propery add 表单中找到用户输入的邮政编码记录,如果存在,将该记录的 ID 放在 postcode_id 外键字段中属性表?

标签: cakephp model foreign-keys relationship model-associations


【解决方案1】:

按照以下步骤操作:

第 1 步:在 Postcode.php 中创建一个操作

function checkPostcode($postcodeValue = null){
  $returnData = $this->find('first',
                array('conditions'=>array('postcode'=>$postcodeValue),
                      'fields'=>array('id'))
                      );
  if(!empty($returnData)){
   return $returnData['Postcode']['id'];
  }else{
   $saveData['Postcode']['postcode'] = $postcodeValue;
   $this->save($saveData);
   return $this->getLastInsertID();
  } 
 }

上述函数会从邮政编码模型中检查邮政编码,如果不存在则插入邮政编码,并返回对应行的邮政编码表的id。

第 2 步:将此函数控制器操作称为

function someFuntion(){
        $postcode_id = $this->Postcode->checkPostcode($userInputPostcodeHere);
        // Write code to save property here 
    }

希望这能让您对逻辑有所了解...

【讨论】:

  • checkPostcode($postcodeValue = null) 而不是 checkPostcode($postcodeValue) 背后的原因是什么?此外,调用该函数必须在邮政编码字段被验证为邮政编码格式之后进行,所以它会在属性模型中调用吗?如果是这样,作为自定义验证函数,还是在 afterValidation 中?谢谢!
  • 点号 1 checkPostcode($postcodeValue = null) 这样如果不带参数调用不会给出致命错误,点号 2 它将在控制器操作验证后调用
  • 要调用它,它必须是 $this->Property->Postcode->function,除此之外,效果很好,谢谢!你知道如何适应这个进行编辑吗?有一个“Postcode.postcode”字段会显示当前字段,但它必须是 Property.postcode 才能找到 Postcode.id,如上所述。有没有这样做的最佳实践方法?可能是 Property.postcode 字段,填充 Postcode.postcode 值?
  • @StringsOnFire 我明白你要编辑的意思了吗?请用外行的语言告诉我。
  • 我的意思是请详细说明您的问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-29
  • 1970-01-01
  • 2020-11-26
相关资源
最近更新 更多