CakePHP具有一种归属关系,如何从用户输入中查找并插入外键? [英] CakePHP hasOne-belongsTo relationship, how to look up and insert foreign key from user input?

查看:231
本文介绍了CakePHP具有一种归属关系,如何从用户输入中查找并插入外键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张表:
$ b $ pre $ code $ property $($,$ post $ _ $,$)
Postcode ID,邮编,纬度,经度)

关系:

  class属性extends AppModel {
public $ belongsTo = array(
'Postcode'=> array(
'className'=> ;'邮编'

);

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

);

我想要发生什么:


  1. 在属性添加表单中输入一个邮政编码。
  2. 检查输入的邮政编码是在邮政编码表中。
  3. 如果是这样,那么在Properties.postcode_id(外键)中记录Postcode.id。 li>

我无法弄清楚用蛋糕做这件事的最好方法。一个自定义属性验证功能,执行检查并将其添加到要添加的数据?还是在验证之前?或者Cake是否处理这个问题?

谢谢!

编辑



基于这个答案,我认为这是最好的解决方案...



我认为这样做是更好的方法,因为它也验证。感谢Anubhav把我放在正确的轨道上!

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

<$ find('first',array(
'conditions'=> $'$'$'$'$> $ $ $ $) array('postcode'=> $ postcode),
'fields'=> array('id')
));
if(!empty($ record)){
return $ record ['Postcode'] ['id'];
} else {
return false;
//寻找并添加新的邮政编码的逻辑


code $


<但是在Property模型中:

$ p $ public $ validate = array(
'postcode'=> array (
'exists'=>数组(
'rule'=>'postcodeExists',
'message'=>'邮编不存在'


);

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

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



$ b $ p
$ b

通过修改保存在这里的值,控制器保持不变,而不是确保邮政编码是真实的,然后找到id,两者都立即完成,所以只需要一个查询。

解决方案

步骤1:在Postcode.php中创建一个动作

 <$ c $($'$'$'$'$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$'$'$'$'$'$'$'$' $ postcodeValue),
'fields'=> array('id'))
);
if(!empty($ returnData)){
return $ returnData ['Postcode'] ['id'];
} else {
$ saveData ['Postcode'] ['postcode'] = $ postcodeValue;
$ this-> save($ saveData);
return $ this-> getLastInsertID();






$ b

上面的函数将检查邮政编码模式的邮政编码并插入postcode如果不存在,并返回相应行的postcode表的id。

第二步:调用这个函数控制器动作为

pre $ function someFuntion(){
$ postcode_id = $ this-> Postcode-> checkPostcode($ userInputPostcodeHere);
//编写代码在这里保存属性
}

你对逻辑的一些想法...

I have two tables:

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

The relationship:

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

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

What I'd like to happen:

  1. Enter a postcode in the Property add form.
  2. Check entered postcode is in the Postcode table.
  3. If not, so some logic (possibly add it, just return validation error for now).
  4. If so, then record the Postcode.id in Properties.postcode_id (the foreign key).

I can't figure out the best way to do this with cake. A custom Property validation function that does the check and adds it to data to be added? Or in beforeValidation? Or does Cake deal with this?

Thanks!

EDIT

Based on the answer, I think this is the best solution...

I think this is the better way to do it, as it validates too. Thanks Anubhav for the putting me on the right track!

As per Anubhav's answer, in the Postcode model:

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

But and in the Property model:

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']);
}

By modifying the values to save here, the controller is kept skinny, and, rather than making sure postcode is real then finding the id, both are done at once, so only one query is needed.

解决方案

Follow the steps:

Step 1: Create one action in 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();
  } 
 }

The above function will check postcode from postcode model and insert postcode if not exist and returns the id of postcode table for corresponding row.

Step 2: Call this function controller action as

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

Hope this will give you some idea about the logic...

这篇关于CakePHP具有一种归属关系,如何从用户输入中查找并插入外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆