public function actionAddSampleAttr() { if (isset($_POST['sample_id']) && isset($_POST['attr_id']) && isset($_POST['attr_value']) && isset($_POST['attr_unit'])) { if (strlen($_POST['attr_id']) < 3) { Util::returnJSON(array("success" => false, "message" => Yii::t("app", "Please enter an Attribute name with more than 3 characters."))); } $lastSa = SampleAttribute::model()->find(array('order' => 'id desc')); $sa = new SampleAttribute(); if ($lastSa) { $sa->id = $lastSa->id + 1; } // try to find attribute, if not found, create a new one $attr = Attribute::model()->findByAttributes(array('attribute_name' => $_POST['attr_id'])); if (!$attr) { #create new attribute $attr = new Attribute(); $attr->attribute_name = $_POST['attr_id']; $attr->save(false); } $sa->sample_id = $_POST['sample_id']; $sa->attribute_id = $attr->id; $sa->value = $_POST['attr_value']; if (isset($_POST['attr_unit']) && $_POST['attr_unit'] != "") { $sa->unit_id = $_POST['attr_unit']; } if ($sa->save()) { Util::returnJSON(array("success" => true)); } Util::returnJSON(array("success" => false, "message" => Yii::t("app", "Cannot add sample attr."))); } }
private function storeSpps($samples, $value, $sppAttr) { $lastSA = SampleAttribute::model()->findByAttributes(array(), array('order' => 'id desc')); $lastId = $lastSA->id; foreach ($samples as $sample) { $spp = SampleAttribute::model()->findByAttributes(array('attribute_id' => $sppAttr->id, 'sample_id' => $sample->id)); if (!$spp) { $lastId = $lastId + 1; $spp = new SampleAttribute(); $spp->id = $lastId; $spp->attribute_id = $sppAttr->id; $spp->sample_id = $sample->id; } #store spp value $spp->value = $value; if (!$spp->save()) { return false; } } return true; }