newRecord = true; } public static function createImage(Reservation $reservation, $type, $path) { assert('!empty($path)'); $image = new self; $image->user_id = $reservation->user_id; $image->image_type = $type; $image->reservation_id = $reservation->id; $image->fullpath = $path; $image->filename = (string)substr(pathinfo($path, PATHINFO_BASENAME), 0, 10); $image->save(); return $image; } public function save() { try { if ($this->newRecord) { if (!isset($this->id)) { $sql = "INSERT INTO reservation_images (user_id, image_type, filename, fullpath, reservation_id, created_at) VALUES ('" . $this->user_id . "', '" . $this->image_type . "', '" . $this->filename . "', '" . $this->fullpath . "', '" . $this->reservation_id . "', NOW())"; } else { $sql = "INSERT INTO reservation_images (id, user_id, image_type, filename, fullpath, reservation_id, created_at) VALUES ('" . $this->id . "', '" . $this->user_id . "', '" . $this->image_type . "', '" . $this->filename . "', '" . $this->fullpath . "', '" . $this->reservation_id . "', NOW())"; } 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("update reservation image not 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 static function className() { return get_called_class(); } public static function findOne($id) { $sql = "SELECT * FROM reservation_images WHERE id = '$id'"; $rez = mysql_query($sql); if (mysql_num_rows($rez)) { $reservationImage = new ReservationImage(); $reservationImage->newRecord = false; $dbObject = mysql_fetch_array($rez); $reservationImage->id = $dbObject['id']; $reservationImage->filename = $dbObject['filename']; $reservationImage->image_type = $dbObject['image_type']; $reservationImage->fullpath = $dbObject['fullpath']; $reservationImage->user_id = $dbObject['user_id']; $reservationImage->reservation_id = $dbObject['reservation_id']; $reservationImage->created_at = $dbObject['created_at']; $reservationImage->updated_at = $dbObject['updated_at']; return $reservationImage; } return null; } /** * @param $reservationId - ИД * @return ReservationImage[] */ public static function findForReservation($reservationId) { $sql = "SELECT * FROM reservation_images WHERE reservation_id = '$reservationId'"; $rez = mysql_query($sql); $reservationImages = array(); if (mysql_num_rows($rez)) { while ($dbObject = mysql_fetch_assoc($rez)) { $reservationImage = new ReservationImage(); $reservationImage->newRecord = false; $reservationImage->id = $dbObject['id']; $reservationImage->filename = $dbObject['filename']; $reservationImage->image_type = $dbObject['image_type']; $reservationImage->fullpath = $dbObject['fullpath']; $reservationImage->user_id = $dbObject['user_id']; $reservationImage->reservation_id = $dbObject['reservation_id']; $reservationImage->created_at = $dbObject['created_at']; $reservationImage->updated_at = $dbObject['updated_at']; $reservationImages[] = $reservationImage; } } return $reservationImages; } }