50 lines
1.2 KiB
PHP
50 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace humhub\modules\animal_management\models;
|
|
|
|
use humhub\components\ActiveRecord;
|
|
use humhub\modules\rescue_foundation\models\RescueFieldDefinition;
|
|
|
|
class AnimalFieldValue extends ActiveRecord
|
|
{
|
|
public static function tableName()
|
|
{
|
|
return 'rescue_animal_field_value';
|
|
}
|
|
|
|
public function rules()
|
|
{
|
|
return [
|
|
[['animal_id', 'field_definition_id'], 'required'],
|
|
[['animal_id', 'field_definition_id'], 'integer'],
|
|
[['value_text', 'value_json'], 'string'],
|
|
[['animal_id', 'field_definition_id'], 'unique', 'targetAttribute' => ['animal_id', 'field_definition_id']],
|
|
];
|
|
}
|
|
|
|
public function beforeSave($insert)
|
|
{
|
|
if (!parent::beforeSave($insert)) {
|
|
return false;
|
|
}
|
|
|
|
$now = date('Y-m-d H:i:s');
|
|
if ($insert && empty($this->created_at)) {
|
|
$this->created_at = $now;
|
|
}
|
|
|
|
$this->updated_at = $now;
|
|
return true;
|
|
}
|
|
|
|
public function getAnimal()
|
|
{
|
|
return $this->hasOne(Animal::class, ['id' => 'animal_id']);
|
|
}
|
|
|
|
public function getFieldDefinition()
|
|
{
|
|
return $this->hasOne(RescueFieldDefinition::class, ['id' => 'field_definition_id']);
|
|
}
|
|
}
|