Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

asdasg345

<?php

class Design
{
    public $id = null;
    protected $attributes = [
        'id' => '',
        'name' => '',
    ];

    /**
     * Set fields and return design instance.
     * @param $design_data
     * @return $this
     */
    public function saveDesignData($design_data)
    {
        $this->attributes['name'] = $design_data['name'];
        // Make other save stuff.
        return $this;
    }

    /**
     * Save shipping into database (default Eloquent Model save).
     * @return $this
     */
    public function save()
    {
        // Assume design saved to database and we have id.
        // But now we set id just here for example.
        $this->attributes['id'] = rand(0, 10000);
        return $this;
    }
	
	public function getAttributes()
	{
		return $this->attributes;	
	}
	
    public function getId()
    {
        return $this->attributes['id'];
    }
}


class Shipping
{

    protected $attributes = [
        'id' => '',
        'type_of_delivery' => '',
        'address' => '',
        'design_id' => '',
    ];

    public function getId()
    {
        return $this->attributes['id'];
    }
	
	public function getAttributes()
	{
		return $this->attributes;	
	}

    /**
     * Set fields and return shipping instance.
     * @param $shipping_data
     * @return $this
     */
    public function saveShippingData($shipping_data)
    {
        $this->attributes['type_of_delivery'] = $shipping_data['type_of_delivery'];
        $this->attributes['address'] = $shipping_data['address'];
        return $this;
    }

    /**
     * Save shipping into database (default Eloquent Model save).
     * @return mixed
     */
    public function save()
    {
        // Assume shipping saved to database and we have id.
        // But now we set id just here for example.
        $this->attributes['id'] = rand(0, 10000);
        return $this;
    }

    /**
     * @param $id
     * @return $this
     */
    public function associateDesign($id)
    {
        $this->attributes['design_id'] = $id;
        return $this;
    }
}

class UniqueKeyIdPairsTempStoreHelper
{
    protected $uniqueKeyIdPairs = [];

    /**
     * Add key/id pair.
     * @param $uniqueKey
     * @param $id
     */
    public function addUniqueKeyIdPair($uniqueKey, $id)
    {
        $this->uniqueKeyIdPairs[$uniqueKey] = $id;
    }

    /**
     * Get id by unique key.
     * @param $uniqueKey
     * @return mixed|null
     */
    public function getIdByUniqueKey($uniqueKey)
    {
        return !empty($this->uniqueKeyIdPairs[$uniqueKey]) ? $this->uniqueKeyIdPairs[$uniqueKey] : null;
    }
}
// Init database.
$database = [];
/**
 * Assume we create Order and its Designs in OrderController.
 */
$design_post = [
    'design1' => [
        'name' => 'Design #1',
        'designUniqueKey' => 'Iasfuhh2323lk4n1',
        // Other design data from frontend.
    ],
    'design2' => [
        'name' => 'Design #2',
        'designUniqueKey' => 'asf54fuhh2323lk4n2',
        // Other design data from frontend.
    ]
];

// Before loop design creation make a new instance of temp unique key and ids store.
$tempDesignIdsStore = new UniqueKeyIdPairsTempStoreHelper();

// Loop design post and save each design into database.
foreach ($design_post as $design_data) {
    $design = new Design(); // Create design instance

    /*
     * Bunch of code here. Set design fields, create relations(finishings, garments, etc.)
     */
    $design->saveDesignData($design_data);

    $design->save(); // Call save method and design will be stored in database.
    
    
    
    $database['designs'][$design->getId()] = $design->getAttributes();
    /*
     *  Now we can access design id and save it to temp store.
     *  $design_data will store uniqueKey for new designs on estimate form. (ask frontend develop to do it).
     */
    $tempDesignIdsStore->addUniqueKeyIdPair($design_data['designUniqueKey'], $design->getId()); //
}

/*
 * Now you can use $tempDesignIdsStore below for other part of Order saving proccess.
 * See example.
 */

$shippings_post = [
    'shipping1' => [
        'address' => 'Ukraine, Kharkiv, XTZ',
        'type_of_delivery' => 'shipping',
        // More fields for shipping.
        // ...
        'designUniqueKey' => 'Iasfuhh2323lk4n1'
    ],
    'shipping2' => [
        'address' => 'Ukraine, Kharkiv, Moskov ave.',
        'type_of_delivery' => 'shipping',
        // More fields for shipping.
        // ...
        'designUniqueKey' => 'asf54fuhh2323lk4n2'
    ],
    'shipping3' => [
        'address' => 'United States, Florida, Some Street',
        'type_of_delivery' => 'pickup',
        // More fields for shipping.
        // ...
        'designUniqueKey' => 'Iasfuhh2323lk4n1'
    ]
];

foreach ($shippings_post as $shipping_data) {
    // Get design from temp store by unique key attached on estimate form on shipping step.
    $design_id = $tempDesignIdsStore->getIdByUniqueKey($shipping_data['designUniqueKey']);


    /*
     * Create shipping address,
     * Create shipping,
     * Use design id from temp store to associate shipping and design
     */
    $shipping = new Shipping();
    $shipping->saveShippingData($shipping_data);
    $shipping->save();
    
    $shipping->associateDesign($design_id);

    $database['shippings'][$shipping->getId()] = $shipping->getAttributes();
}

print_r($database);

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.