| 211 | | <?php |
| 212 | | |
| 213 | | /** |
| 214 | | * エンティティクラスの例 |
| 215 | | */ |
| 216 | | class SC_Entity_OrderDetail { |
| 217 | | |
| 218 | | private $order_detail_id; |
| 219 | | private $order_id; |
| 220 | | private $product_id; |
| 221 | | private $product_class_id; |
| 222 | | private $product_name; |
| 223 | | private $product_code; |
| 224 | | private $classcategory_name1; |
| 225 | | private $classcategory_name2; |
| 226 | | private $price; |
| 227 | | private $quantity; |
| 228 | | private $point_rate; |
| 229 | | |
| 230 | | /** |
| 231 | | * コンストラクタ. |
| 232 | | * |
| 233 | | * 引数の連想配列で, 本オブジェクトのプロパティを設定可能. |
| 234 | | * |
| 235 | | * @param array $arrOrderDetail |
| 236 | | */ |
| 237 | | public function __construct($arrOrderDetail = array()) { |
| 238 | | if (is_array($arrOrderDetail)) { |
| 239 | | $this->setPropertiesFromArray($arrOrderDetail); |
| 240 | | } |
| 241 | | } |
| 242 | | |
| 243 | | /** |
| 244 | | * 税込単価を返します. |
| 245 | | * |
| 246 | | * @return integer 税込単価 |
| 247 | | */ |
| 248 | | public function getTotalInTax() { |
| 249 | | return SC_Helper_DB_Ex::sfTax($this->getPrice()) * |
| 250 | | $this->getQuantity(); |
| 251 | | } |
| 252 | | |
| 253 | | /** |
| 254 | | * 該当商品のカテゴリIDを配列で返します. |
| 255 | | * |
| 256 | | * @return array カテゴリ一覧の配列 |
| 257 | | */ |
| 258 | | public function getCategoryIds() { |
| 259 | | // エンティティクラスの内部での DBアクセスは極力避けるべきですが, |
| 260 | | // 内部でDBアクセスした方が効率が良い場合は, その旨のコメントを入れて使用します. |
| 261 | | // この場合は, product_id に紐づいた category_id を取得します. |
| 262 | | // 以下のロジックを外出しするより簡潔なコードになります. |
| 263 | | $objQuery = SC_Query_Ex::getSingletonInstance(); |
| 264 | | $col = 'category_id'; |
| 265 | | $from = 'dtb_product_categories'; |
| 266 | | $where = 'product_id = ?'; |
| 267 | | $arrResults = $objQuery->select($col, $from, $where, |
| 268 | | array($this->getProductId())); |
| 269 | | $arrCategoryIds = array(); |
| 270 | | foreach ($arrResults as $val) { |
| 271 | | $arrCategoryIds[] = $val; |
| 272 | | } |
| 273 | | return $arrCategoryIds; |
| 274 | | } |
| 275 | | |
| 276 | | /* |
| 277 | | * 読み取り専用にしたいプロパティは, getter のみ作成します. |
| 278 | | * 更新可能なプロパティは setter を作成します. |
| 279 | | */ |
| 280 | | public function getOrderId() { |
| 281 | | return $this->order_id; |
| 282 | | } |
| 283 | | public function getProductId() { |
| 284 | | return $this->product_id; |
| 285 | | } |
| 286 | | public function getProductClassId() { |
| 287 | | return $this->product_class_id; |
| 288 | | } |
| 289 | | public function getProductCode() { |
| 290 | | return $this->product_code; |
| 291 | | } |
| 292 | | public function getClassCategoryName1() { |
| 293 | | return $this->classcategory_name1; |
| 294 | | } |
| 295 | | public function getClassCategoryName2() { |
| 296 | | return $this->classcategory_name2; |
| 297 | | } |
| 298 | | public function getPrice() { |
| 299 | | return $this->price; |
| 300 | | } |
| 301 | | public function setQuantity($quantity) { |
| 302 | | $this->quantity = $quantity; |
| 303 | | } |
| 304 | | public function getQuantity() { |
| 305 | | return $this->quantity; |
| 306 | | } |
| 307 | | protected function getPointRate() { |
| 308 | | return $this->point_rate; |
| 309 | | } |
| 310 | | |
| 311 | | /** |
| 312 | | * 引数の連想配列を元にプロパティを設定します. |
| 313 | | * DBから取り出した連想配列を, プロパティへ設定する際に使用します. |
| 314 | | * |
| 315 | | * @param array プロパティの情報を格納した連想配列 |
| 316 | | * @param ReflectionClass $parentClass 親のクラス. 本メソッドの内部的に使用します. |
| 317 | | */ |
| 318 | | public function setPropertiesFromArray($arrProps, ReflectionClass $parentClass = null) { |
| 319 | | $objReflect = null; |
| 320 | | if (is_object($parentClass)) { |
| 321 | | $objReflect = $parentClass; |
| 322 | | } else { |
| 323 | | $objReflect = new ReflectionClass($this); |
| 324 | | } |
| 325 | | |
| 326 | | $arrProperties = $objReflect->getProperties(); |
| 327 | | foreach ($arrProperties as $objProperty) { |
| 328 | | $objProperty->setAccessible(true); |
| 329 | | $name = $objProperty->getName(); |
| 330 | | $objProperty->setValue($this, $arrProps[$name]); |
| 331 | | } |
| 332 | | |
| 333 | | // 親クラスがある場合は再帰的にプロパティを取得 |
| 334 | | $parentClass = $objReflect->getParentClass(); |
| 335 | | if (is_object($parentClass)) { |
| 336 | | self::setPropertiesFromArray($arrProps, $parentClass); |
| 337 | | } |
| 338 | | } |
| 339 | | |
| 340 | | /** |
| 341 | | * プロパティの値を連想配列で返します. |
| 342 | | * DBを更新する場合などで, 連想配列の値を取得したい場合に使用します. |
| 343 | | * |
| 344 | | * @return array 連想配列のプロパティの値 |
| 345 | | */ |
| | 211 | <?php |
| | 212 | |
| | 213 | /** |
| | 214 | * エンティティクラスの例 |
| | 215 | */ |
| | 216 | class SC_Entity_OrderDetail { |
| | 217 | |
| | 218 | private $order_detail_id; |
| | 219 | private $order_id; |
| | 220 | private $product_id; |
| | 221 | private $product_class_id; |
| | 222 | private $product_name; |
| | 223 | private $product_code; |
| | 224 | private $classcategory_name1; |
| | 225 | private $classcategory_name2; |
| | 226 | private $price; |
| | 227 | private $quantity; |
| | 228 | private $point_rate; |
| | 229 | |
| | 230 | /** |
| | 231 | * コンストラクタ. |
| | 232 | * |
| | 233 | * 引数の連想配列で, 本オブジェクトのプロパティを設定可能. |
| | 234 | * |
| | 235 | * @param array $arrOrderDetail |
| | 236 | */ |
| | 237 | public function __construct($arrOrderDetail = array()) { |
| | 238 | if (is_array($arrOrderDetail)) { |
| | 239 | $this->setPropertiesFromArray($arrOrderDetail); |
| | 240 | } |
| | 241 | } |
| | 242 | |
| | 243 | /** |
| | 244 | * 税込単価を返します. |
| | 245 | * |
| | 246 | * @return integer 税込単価 |
| | 247 | */ |
| | 248 | public function getTotalInTax() { |
| | 249 | return SC_Helper_DB_Ex::sfTax($this->getPrice()) * $this->getQuantity(); |
| | 250 | } |
| | 251 | |
| | 252 | /** |
| | 253 | * 該当商品のカテゴリIDを配列で返します. |
| | 254 | * |
| | 255 | * @return array カテゴリ一覧の配列 |
| | 256 | */ |
| | 257 | public function getCategoryIds() { |
| | 258 | // エンティティクラスの内部での DBアクセスは極力避けるべきですが, |
| | 259 | // 内部でDBアクセスした方が効率が良い場合は, その旨のコメントを入れて使用します. |
| | 260 | // この場合は, product_id に紐づいた category_id を取得します. |
| | 261 | // 以下のロジックを外出しするより簡潔なコードになります. |
| | 262 | $objQuery = SC_Query_Ex::getSingletonInstance(); |
| | 263 | $col = 'category_id'; |
| | 264 | $from = 'dtb_product_categories'; |
| | 265 | $where = 'product_id = ?'; |
| | 266 | $arrResults = $objQuery->select($col, $from, $where, |
| | 267 | array($this->getProductId())); |
| | 268 | $arrCategoryIds = array(); |
| | 269 | foreach ($arrResults as $val) { |
| | 270 | $arrCategoryIds[] = $val; |
| | 271 | } |
| | 272 | return $arrCategoryIds; |
| | 273 | } |
| | 274 | |
| | 275 | /* |
| | 276 | * 読み取り専用にしたいプロパティは, getter のみ作成します. |
| | 277 | * 更新可能なプロパティは setter を作成します. |
| | 278 | */ |
| | 279 | public function getOrderId() { |
| | 280 | return $this->order_id; |
| | 281 | } |
| | 282 | public function getProductId() { |
| | 283 | return $this->product_id; |
| | 284 | } |
| | 285 | public function getProductClassId() { |
| | 286 | return $this->product_class_id; |
| | 287 | } |
| | 288 | public function getProductCode() { |
| | 289 | return $this->product_code; |
| | 290 | } |
| | 291 | public function getClassCategoryName1() { |
| | 292 | return $this->classcategory_name1; |
| | 293 | } |
| | 294 | public function getClassCategoryName2() { |
| | 295 | return $this->classcategory_name2; |
| | 296 | } |
| | 297 | public function getPrice() { |
| | 298 | return $this->price; |
| | 299 | } |
| | 300 | public function setQuantity($quantity) { |
| | 301 | $this->quantity = $quantity; |
| | 302 | } |
| | 303 | public function getQuantity() { |
| | 304 | return $this->quantity; |
| | 305 | } |
| | 306 | protected function getPointRate() { |
| | 307 | return $this->point_rate; |
| | 308 | } |
| | 309 | |
| | 310 | /** |
| | 311 | * 引数の連想配列を元にプロパティを設定します. |
| | 312 | * DBから取り出した連想配列を, プロパティへ設定する際に使用します. |
| | 313 | * |
| | 314 | * @param array プロパティの情報を格納した連想配列 |
| | 315 | * @param ReflectionClass $parentClass 親のクラス. 本メソッドの内部的に使用します. |
| | 316 | */ |
| | 317 | public function setPropertiesFromArray($arrProps, ReflectionClass $parentClass = null) { |
| | 318 | $objReflect = null; |
| | 319 | if (is_object($parentClass)) { |
| | 320 | $objReflect = $parentClass; |
| | 321 | } else { |
| | 322 | $objReflect = new ReflectionClass($this); |
| | 323 | } |
| | 324 | |
| | 325 | $arrProperties = $objReflect->getProperties(); |
| | 326 | foreach ($arrProperties as $objProperty) { |
| | 327 | $objProperty->setAccessible(true); |
| | 328 | $name = $objProperty->getName(); |
| | 329 | $objProperty->setValue($this, $arrProps[$name]); |
| | 330 | } |
| | 331 | |
| | 332 | // 親クラスがある場合は再帰的にプロパティを取得 |
| | 333 | $parentClass = $objReflect->getParentClass(); |
| | 334 | if (is_object($parentClass)) { |
| | 335 | self::setPropertiesFromArray($arrProps, $parentClass); |
| | 336 | } |
| | 337 | } |
| | 338 | |
| | 339 | /** |
| | 340 | * プロパティの値を連想配列で返します. |
| | 341 | * DBを更新する場合などで, 連想配列の値を取得したい場合に使用します. |
| | 342 | * |
| | 343 | * @return array 連想配列のプロパティの値 |
| | 344 | */ |