简体   繁体   English

任何人都可以帮助我将多个数组保存到 Yii2 框架中的数据库中吗?

[英]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.我想将数组保存到数据库中,该数组是从我创建的 DOMDocument 中获取的。 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. $shifted值是顶部表头。 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.下面是我得到的 DOMDocument 的输出。

 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.我已经用 lme_id、lme_title、lme_name 和 lme_price 创建了数据库表。 I want to save the $shifted to the lme_title, $aDataTableHeaderHtml to lme_name, and $dataCell to lme_price.我想将$shifted保存到 lme_title,将$aDataTableHeaderHtml 保存到 lme_name,将$dataCell 保存到 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.希望有人可以教我如何使用Yii2框架将数组保存到数据库中。 Thanks.谢谢。

I think you are missing something important like which database manager you are using, for example, MySQL, mssql, RDBMS .我认为您缺少一些重要的东西,例如您使用的数据库管理器,例如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 .但是要注意并没有多少DBMS允许您将数组直接保存到数据库中,导致数据库中存储的普通数据类型是实用的原始值

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.第一个是使用 PHP 内方法生成一个字符串值,然后可以将其保存到数据库中,就像您对普通 VARCHARS 字段所做的一样。

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.例如,1- 正在形成的复杂字符串难以阅读。 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. 2-您最终需要使用标志值来确定中断,而添加到数组中的另一个值可能会导致您正在使用的标志出现问题。


The second method , which is what I would recommend is to store as a JSON value.我推荐的第二种方法是存储为JSON值。

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.现在很多 DBMS 都支持这种类型的数据,而且它是一种标准化的格式,所以你可以在 PHP 中做的是首先制作一个json_encode(YOUR_ARRAY)然后存储在数据库中。

json_encode docs json_decode docs json_encode 文档json_decode 文档

I'm not sure about the Yii part cause I haven't used it for quite some time.我不确定 Yii 部分,因为我已经有一段时间没有使用它了。 But I hope this information can guide you.但我希望这些信息可以为您提供指导。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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