newRecord = true; } public function save() { try { if ($this->newRecord) { if (!isset($this->id)) { $sql = "INSERT INTO block_images (model_id, image, block_images.type, img_date, video_id) VALUES ('" . $this->model_id . "', '" . $this->image . "', " . $this->type . ", '" . $this->img_date . "', '".$this->video_id."')"; } else { $sql = "INSERT INTO block_images (id, model_id, image) VALUES ('" . $this->id . "', '" . $this->model_id . "', '" . $this->image . "')"; } mysql_query($sql); $err = mysql_errno(); if ($err > 0) { echo mysql_error(); return false; } if (!isset($this->id)) { $this->id = mysql_insert_id(); } } else { throw new Exception("no block image update implemented"); } return true; } catch (Exception $e) { echo $e->getMessage(); return false; } } public function getIsNewRecord() { return $this->newRecord; } public static function deleteAll($condition = '') { throw new Exception("deleteALL not implemented"); } public function delete() { $sql = "DELETE from block_images WHERE id='" . $this->id . "'"; mysql_query($sql); unlink($_SERVER['DOCUMENT_ROOT'] . $this->image); } public static function className() { return get_called_class(); } public static function findOne($id) { $sql = "SELECT * FROM block_images WHERE id='$id'"; $rez = mysql_query($sql); if (mysql_num_rows($rez)) { $blockImage = new BlockImage(); $blockImage->newRecord = false; $dbObject = mysql_fetch_array($rez); $blockImage->id = $dbObject['id']; $blockImage->model_id = $dbObject['model_id']; $blockImage->image = $dbObject['image']; $blockImage->video_id = $dbObject['video_id']; return $blockImage; } return null; } public static function createImage($blockId, $path, $type = 'NULL', $img_date = 'NULL', $video_id = 'NULL') { assert('(int)$blockId > 0'); assert('!empty($path)'); $image = new self; $image->model_id = $blockId; $image->image = $path; $image->type = $type; $image->img_date = $img_date; $image->video_id = $video_id; $image->save(); return $image; } }