在构建WordPress插件时,在我的激活钩子期间,我调用以下方法来在我的新数据库表中构建测试数据。我正在尝试遍历值以添加数据:
public static function installData()
{
global $wpdb;
$authorID = (get_current_user_id()) ? get_current_user_id() : '1';
$demoData = [
0 => [
'id' => '122',
'title' => 'Lorem ipsum dolor sit amet',
'url' => 'https://www.efsnetworks.com',
'image' => 'http://www.news.ucsb.edu/sites/www.news.ucsb.edu/files/styles/article_horizontal/public/images/2018/Cubic%20salt%20crystal%20aggregate.jpg',
'type' => 'small-block',
'authorID' => $authorID,
'dateCreated' => '2016-09-23 21:57:34',
'lastUpdate' => null,
'publishStatus' => '0'
],
1 => [
'id' => '123',
'title' => 'Ut enim ad minim veniam, quis nostrud',
'url' => 'https://www.google.com',
'image' => 'https://cdn.britannica.com/s:500x350/66/143166-049-4900844D.jpg',
'type' => 'small-block',
'authorID' => $authorID,
'dateCreated' => '2017-07-23 22:02:34',
'lastUpdate' => null,
'publishStatus' => '0'
],
2 => [
'id' => '124',
'title' => 'Duis aute irure dolor in reprehenderit in voluptate',
'url' => 'https://www.disruptivetechnology.com',
'image' => 'https://vignette.wikia.nocookie.net/forgottenrealms/images/1/13/Rock_crystal.jpg',
'type' => 'small-block',
'authorID' => $authorID,
'dateCreated' => '2018-12-23 22:03:34',
'lastUpdate' => null,
'publishStatus' => '0'
]
];
$table_name = $wpdb->wpAdvertisementAds;
foreach ($demoData as $index=>$values) {
foreach ($values as $key=>$value) {
$wpdb->insert(
$table_name,
[ $key => $value]
);
}
}
}
当它运行时,我看到由于嵌套foreach
内的 $wpdb-insert
子句,数据正在添加一个新行。然而,当我将其移到第一个foreach
中时,数据没有被添加。我最终必须将其重写为以下有效:
public static function installData()
{
global $wpdb;
$authorID = (get_current_user_id()) ? get_current_user_id() : '1';
$table_name = $wpdb->wpAdvertisementAds;
$wpdb->insert(
$table_name,
[
'id' => '122',
'title' => 'Lorem ipsum dolor sit amet',
'url' => 'https://www.efsnetworks.com',
'image' => 'http://www.news.ucsb.edu/sites/www.news.ucsb.edu/files/styles/article_horizontal/public/images/2018/Cubic%20salt%20crystal%20aggregate.jpg',
'type' => 'small-block',
'authorID' => $authorID,
'dateCreated' => '2016-09-23 21:57:34',
'lastUpdate' => null,
'publishStatus' => '0'
]
);
$wpdb->insert(
$table_name,
[
'id' => '123',
'title' => 'Ut enim ad minim veniam, quis nostrud',
'url' => 'https://www.google.com',
'image' => 'https://cdn.britannica.com/s:500x350/66/143166-049-4900844D.jpg',
'type' => 'small-block',
'authorID' => $authorID,
'dateCreated' => '2017-07-23 22:02:34',
'lastUpdate' => null,
'publishStatus' => '0'
]
);
$wpdb->insert(
$table_name,
[
'id' => '124',
'title' => 'Duis aute irure dolor in reprehenderit in voluptate',
'url' => 'https://www.disruptivetechnology.com',
'image' => 'https://vignette.wikia.nocookie.net/forgottenrealms/images/1/13/Rock_crystal.jpg',
'type' => 'small-block',
'authorID' => $authorID,
'dateCreated' => '2018-12-23 22:03:34',
'lastUpdate' => null,
'publishStatus' => '0'
]
);
}
您是否有任何技巧可以使此方法与我的数据的简单foreach
命令一起使用。这样,我可以将数据集移动到 php 之外并放入 json 文件或任何文件源(不相关但可用于将来使用)。
我认为你不需要内部foreach
循环。您的代码应该是:
foreach ($demoData as $value) {
$wpdb->insert(
$table_name,
$value
);
}