简体   繁体   中英

Can anyone help me with saving multiple array to the database in Yii2 framework?

I want to save the array to the database which the array is take from the DOMDocument I have created. I keep trying to save the array but it doesn't work. The database didn't show any data. Below is the function I have create.

public function actionGetLme()
{
    $htmlContent = file_get_contents("https://www.lme.com/"); 

    $DOM = new \DOMDocument();
    @$DOM->loadHTML($htmlContent);

    $headerDOM = $DOM->getElementsByTagName('th'); //Getting the header of the table
    //#Get header name of the table
    foreach($headerDOM as $nodeHeader) 
    {
        $aDataTableHeaderHTML[] = trim($nodeHeader->textContent);
    }
    // print_r($aDataTableHeaderHTML);
    $shifted = array_shift($aDataTableHeaderHTML);
    //print_r($aDataTableHeaderHTML);

    #Get row data/detail table without header name as key
    $detailDOM = $DOM->getElementsByTagName('td');
    foreach($detailDOM as $sNodeDetail)  //Gettting the table data
    {
        $dataCell[] = trim($sNodeDetail->textContent);
    }
    // print_r($dataCell);
    $mapping[$shifted]=array_combine($aDataTableHeaderHTML,$dataCell);
    //print_r($mapping);
    $model = new Lme();
    $model->load(Yii::$app->request->post()); 
    $model->lme_title = $shifted;              // The data that I want to save to the db
    $model->lme_name = $aDataTableHeaderHTML;  // The data that I want to save to the db
    $model->lme_price = $dataCell;             // The data that I want to save to the db
    $model->save();

The $shifted value is the top table header. Can refer to the link inside the code for more clearly understanding.

I already change the data to the array form. Below is the output of the DOMDocument which I get.

 Array ( [US$: 24 February 2020] => Array ( [LME Aluminium] => 1,672.50 [LME Copper] => 5,657.50 [LME Zinc] => 2,039.00 [LME Nickel] => 12,360.00 [LME Lead] => 1,864.00 [LME Tin] => 16,510.00 [LME Aluminium Alloy] => 1,360.00 [LME NASAAC] => 1,260.00 [LME Cobalt] => 33,500.00 [LME Gold*] => 1,674.30 [LME Silver*] => 18.900 [LME Steel Scrap**] => 290.00 [LME Steel Rebar**] => 442.00 ) )

I have create the database table with the lme_id, lme_title, lme_name and lme_price. I want to save the $shifted to the lme_title, $aDataTableHeaderHtml to lme_name, and $dataCell to lme_price.

Example : |lme_id|lme_title|lme_name|lme_price|
          |   1  |  Title  | Copper | 1660.00 |

Hope someone can teach me how to save the array into the database by using Yii2 framework. Thanks.

I think you are missing something important like which database manager you are using, for example, MySQL, mssql, RDBMS .

But just to notice not many DBMS allows you to save an array directly into the database, cause normal data types to be stored in databases are practical primitive values .

So, If this is the case, you could apply to methods around this:


The first one is to use the PHP implode method to generate a string value which can then be saved into the database as you would do whit normal VARCHARS fields.

This method has some concerns which are by which I would not recommend this.

For example, 1- The complex string being formed is difficult to read. 2- You would end up needing to use a flag value to determinate the break and another value added to the array could cause problems with the flag you are using.


The second method , which is what I would recommend is to store as a JSON value.

Many DBMS support this type of data nowadays, and also it is a standardized format so what you could do in PHP is first to make a json_encode(YOUR_ARRAY) and then stored in the database.

json_encode docs json_decode docs

I'm not sure about the Yii part cause I haven't used it for quite some time. But I hope this information can guide you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM