ความสัมพันธ์ที่ไพเราะในตารางเดือยแบบกำหนดเอง

ฉันอาจจะซับซ้อนเกินไป แต่นี่คือความสัมพันธ์ที่ฉันพยายามเขียนเป็นภาษา Eloquent (Laravel 5):

ฉันมีรายการสินค้าและราคารายการ สินค้าแต่ละรายการสามารถมีรายการราคาได้หลายรายการ นี่เป็นเรื่องง่ายที่จะทำ แต่นี่เป็นอีกสิ่งหนึ่ง - สำหรับทุกผลิตภัณฑ์ ‹> การกำหนดรายการราคา ฉันสามารถกำหนดราคาได้หลายราคาตามปริมาณ ดังนั้น:

productID: 1010
priceListId: 1

min quantity: 1 -> price 10.00
min quantity: 5 -> price 9.50
min quantity: 10 -> price 9.00

productID: 1010
priceListId: 2

min quantity: 1 -> price 15.00
min quantity: 40 -> price 14.00
min quantity: 90 -> price 12.00

ฉันคิดว่าฉันรู้วิธีสร้างตารางเดือยแบบกำหนดเอง แม้ว่าฉันจะใช้งานไม่เป็นก็ตาม ฉันติดตามลิงก์นี้ ตอนนี้ฉันไม่แน่ใจว่ารหัสของฉันถูกต้องหรือไม่ และวิธีการใช้งาน

ในขณะนี้ฉันมี:

รุ่นสินค้า:

class Product extends Model {

    public function pricelists()
    {
        return $this->belongsToMany('PriceList');
    }

    public function newPivot(Model $parent, array $attributes, $table, $exists)
    {
        if ($parent instanceof PriceList)
        {
            return new ProductPricePivot($parent, $attributes, $table, $exists);
        }

        return parent::newPivot($parent, $attributes, $table, $exists);
    }

}

รายการราคารุ่น:

class PriceList extends Model {

    public function products()
    {
        return $this->belongsToMany('Product');
    }

    public function newPivot(Model $parent, array $attributes, $table, $exists)
    {
        if ($parent instanceof Product)
        {
            return new ProductPricePivot($parent, $attributes, $table, $exists);
        }

        return parent::newPivot($parent, $attributes, $table, $exists);
    }
}

หมุน:

class ProductPricePivot extends Pivot {

    public function product()
    {
        return $this->belongsTo('Product');
    }

    public function pricelist()
    {
        return $this->belongsTo('PriceList');
    }

    public function prices()
    {
        return $this->hasMany('ProductPrice');
    }

}

ตอนนี้ ProductPrice ขยาย Model อีกครั้ง และเป็นเพียงโมเดลมาตรฐานที่ไม่มีวิธีการเพิ่มเติมใดๆ

สิ่งนี้ถูกต้องหรือไม่? หากเป็นเช่นนั้น ตามตัวอย่างจากด้านบน ฉันจะเพิ่มปริมาณ/ระดับราคาใหม่ไปยังรายการราคา 1 บนผลิตภัณฑ์ 1010 ได้อย่างไร

ในขณะนี้ เมื่อสร้างความสัมพันธ์ ฉันกำลังทำ:

$productID = 1010;
$priceListID = 1;

$priceList = PriceList::find($priceListID);
$priceList->products()->attach($productID);

$productToPriceList = $priceList->products()->find($productID);

และฉันหลงทางอยู่ที่นี่... ฉันพบความสัมพันธ์นี้ แต่ตอนนี้ฉันจะแนบระดับราคาถัดไปของปริมาณ ‹> ได้อย่างไร

ใครช่วยยกตัวอย่างวิธีใช้ความสัมพันธ์ดังกล่าวหรือลิงก์ไปยังหน้าที่ฉันสามารถหาอะไรเพิ่มเติมได้บ้าง ใช่ ฉันตรวจสอบเอกสารของ Laravel และใช่ ฉันก็ค้นหาใน Google เช่นกัน

ขอบคุณ!


person Raff W    schedule 19.03.2015    source แหล่งที่มา


คำตอบ (1)


ดังที่คุณอธิบายกรณีนี้ ฉันเห็นความสัมพันธ์สองประเภทที่นี่:

  1. กลุ่มต่อกลุ่ม ความสัมพันธ์ระหว่าง Product และ PriceList
  2. ความสัมพันธ์ หลายต่อหนึ่ง ระหว่าง PriceList & ProductPrice (ซึ่งเก็บค่า "ปริมาณขั้นต่ำ" และ "ราคา")

ฉันสมมติว่าคุณไม่จำเป็นต้องมีเงื่อนไขราคาเดียวกันสำหรับ "รายการราคา" หลายๆ รายการ ทุกรายการจะมีเงื่อนไขราคาของมัน

ความสัมพันธ์แบบหลายต่อหนึ่งไม่ต้องใช้ตารางสรุปข้อมูล

ตามลิงก์นี้ คุณจะพบว่าระบบ laravel Eloquent ช่วยคุณได้มาก

คุณต้องมี 4 ตาราง: ผลิตภัณฑ์, price_lists, product_prices, products_to_pricelists (เดือย)

ตาราง products_to_pricelists มีลักษณะดังนี้:

*====*============*===============*
| id | product_id | price_list_id |
*====*============*===============*

ตอนนี้โมเดล:

สินค้า:

class Product extends Eloquent {

    protected $table = 'products';

    public function price_lists()
    {
        return $this->belongsToMany('PriceList', 'products_to_pricelists', 'price_list_id');
    }

}

รายการราคา:

class PriceList extends Eloquent {

    protected $table = 'price_lists';

    public function prices()
    {
        return $this->hasMany('ProductPrice');
    }

}

ราคา:

class ProductPrice extends Eloquent {

    protected $table = 'product_prices';

}

ตอนนี้มันง่าย:

$productID = 1010;
$list_id = 1;
$theProduct = Product::find( $productID );
$theProductPriceLists = $theProduct->price_lists; // Don't use parens
$theList = Product::find( $productID )->price_lists()->where('price_lists.id', $list_id)->first(); // Here you use parens
$theListPrices = $theList->prices; // Again no parens

Laravel จัดการการเชื่อมต่อเดือยให้คุณ!

หวังว่าจะช่วยได้

person Nati Mask    schedule 19.03.2015
comment
สวัสดี ขอบคุณสำหรับความคิด! ฉันลงเอยด้วยการทำ: $product = Product::find($productID); $product->pricelists()->attach($priceListID); $prodToPriceList = $product->pricelists()->find($priceListID); $prodToPriceList->prices()->create([... quantity and price array ...]); - person Raff W; 19.03.2015