如何从另一个模型中检索在localhost上输入的值


how to retrieve a value that is entered on localhost from another model

首先,让我澄清一下自己。我在cakepp上烘焙了一些东西,并在该应用程序的localhost上输入了值。基本上有locationtemp_readings两种模型。

location模型中,存在被称为high_templow_temp的温度极限。以及位置的名称。我输入这些值,(例如name= fridge, low_temp= 3, high_temp= 7(

temp_readings模型中,我输入特定位置的温度值(例如temperature= 10, location= frigde(。

如果临时值超出限制,我的应用程序会向我发送警告电子邮件。

为此,我编写了temp_readings.php脚本,并且需要设置发送电子邮件的条件。

我想,我可以通过写作来获得温度的值;

 $temperature = $this->data['temp_readings']['temperature']; 

因为我在temp_readings.php工作。

但是,我无法获得保存在位置模型下的low_temphigh_temp的值。

如何在temp_readings.php中获得low_temp和high_temp值

这是temp_readings.php的一部分(我不知道它是否有帮助(

class temp_readings extends AppModel {
function afterSave($created, $options = array()) {
//pr($this);
//$temperature = $this->data['TemperatureReading']['temperature'];
//$low_temp=
//$high_temp=
    if ($temperature > $high_temp && $temperature < $low_temp) {
    $Email = new CakeEmail('gmail');
    $Email->from(array('****@gmail.com' => 'My Site'));
    $Email->to('***@gmail.com');
    $Email->subject('Temperature Warning!');
    $Email->send('Temperature is at critical value of');
    }
}

temp_readingslocation之间应该有一个belongsTo关系,如下所示:

class temp_readings extends AppModel {
    public $belongsTo = [
        'location' // I don't know your data structure, but you'll probably need to add some details here
    ];
    /* ... */
}

然后,保存后:

function afterSave($created, $options = array()) {
    $temperature = $this->data['TemperatureReading']['temperature'] ;
    $location    = $this->data['Location'] ;
    /* ... */
}

当然,这段代码需要更改,因为我不知道你的数据库结构,但它应该给你一个应该做什么的提示