首先,让我澄清一下自己。我在cakepp上烘焙了一些东西,并在该应用程序的localhost上输入了值。基本上有location
和temp_readings
两种模型。
在location
模型中,存在被称为high_temp
和low_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_temp
和high_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_readings
和location
之间应该有一个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'] ;
/* ... */
}
当然,这段代码需要更改,因为我不知道你的数据库结构,但它应该给你一个应该做什么的提示