PrestaShop:内部联接以在列表中显示客户名称
PrestaShop: inner join to show customer name in a list
我正在制作一个 PrestaShop 1.6 插件,我可以在其中配置客户折扣,我的 table 下面:
create table ps_discountbycustomer
(
id_discountbycustomer int not null auto_increment
primary key,
id_customer int not null,
date_start datetime not null,
date_end datetime not null,
use_percentage bit not null,
percentage_order float not null,
percentage_shipping float not null,
value_order float not null,
value_shipping float not null
);
我的模块列表:
我是如何制作我的控制器的class:
class AdminDiscountbycustomerController extends AdminController
{
public function __construct()
{
$this->bootstrap = true;
$this->table = 'discountbycustomer';
$this->className = 'DiscountbycustomerModel';
$this->identifier = 'id_discountbycustomer';
parent::__construct();
$this->lang = false;
$this->_join = 'INNER JOIN '._DB_PREFIX_.'customer c ON (a.id_customer = c.id_customer)';
// Building the list of records stored within the "test" table
$this->fields_list = array(
// 'id_discountbycustomer' => [
// 'title' => $this->l('ID'),
// 'align' => 'center',
// 'width' => 25
// ],
'id_customer' => [
'title' => $this->l('Customer'),
'width' => 'auto'
],
'date_start' => [
'title' => $this->l('Starts'),
'width' => 'auto',
'type' => 'date'
],
'date_end' => [
'title' => $this->l('Ends'),
'width' => 'auto',
'type' => 'date'
],
'use_percentage' => [
'title' => $this->l('Use Percentage'),
'active' => 'use_percentage',
'align' => 'text-center',
'class' => 'fixed-width-sm'
],
'percentage_order' => [
'title' => $this->l('Order %'),
'width' => 'auto',
'type' => 'float'
],
'percentage_shipping' => [
'title' => $this->l('Shipping %'),
'width' => 'auto',
'type' => 'float'
],
'value_order' => [
'title' => $this->l('Order %'),
'width' => 'auto',
'type' => 'float'
],
'value_shipping' => [
'title' => $this->l('Shipping %'),
'width' => 'auto',
'type' => 'float'
],
);
// This adds a multiple deletion button
$this->bulk_actions = array(
'delete' => array(
'text' => $this->l('Delete selected'),
'confirm' => $this->l('Delete selected items?')
)
);
parent::__construct();
}
}
我的问题是我不知道如何在 $this->fields_list
数组中添加客户列,以便在列表中显示客户的姓名。
我该怎么做?我正在使用 PrestaShop 1.6.1.12。
感谢您的帮助
您需要添加 select 来连接名称
$this->_select = 'a.*, CONCAT(c.`firstname`, \' \', c.`lastname`) AS `customer`';
然后,使用 customer:
而不是字段 id_customer
'customer' => [
'title' => $this->l('Customer'),
'width' => 'auto'
],
这很简单,您只需像这样向您的列添加一个回调
...
'id_customer' => [
'title' => $this->l('Customer'),
'width' => 'auto',
'callback' => 'getCustomerName'
],
...
// Then add this function in the same controller
public function getCustomerName($id) {
if (!empty($id)) {
$customer = new Customer($id);
if(Validate::isLoadedObject($customer)) {
return $customer->firstname.' '.$customer->lastname;
}
}
return '';
}
我正在制作一个 PrestaShop 1.6 插件,我可以在其中配置客户折扣,我的 table 下面:
create table ps_discountbycustomer
(
id_discountbycustomer int not null auto_increment
primary key,
id_customer int not null,
date_start datetime not null,
date_end datetime not null,
use_percentage bit not null,
percentage_order float not null,
percentage_shipping float not null,
value_order float not null,
value_shipping float not null
);
我的模块列表:
我是如何制作我的控制器的class:
class AdminDiscountbycustomerController extends AdminController
{
public function __construct()
{
$this->bootstrap = true;
$this->table = 'discountbycustomer';
$this->className = 'DiscountbycustomerModel';
$this->identifier = 'id_discountbycustomer';
parent::__construct();
$this->lang = false;
$this->_join = 'INNER JOIN '._DB_PREFIX_.'customer c ON (a.id_customer = c.id_customer)';
// Building the list of records stored within the "test" table
$this->fields_list = array(
// 'id_discountbycustomer' => [
// 'title' => $this->l('ID'),
// 'align' => 'center',
// 'width' => 25
// ],
'id_customer' => [
'title' => $this->l('Customer'),
'width' => 'auto'
],
'date_start' => [
'title' => $this->l('Starts'),
'width' => 'auto',
'type' => 'date'
],
'date_end' => [
'title' => $this->l('Ends'),
'width' => 'auto',
'type' => 'date'
],
'use_percentage' => [
'title' => $this->l('Use Percentage'),
'active' => 'use_percentage',
'align' => 'text-center',
'class' => 'fixed-width-sm'
],
'percentage_order' => [
'title' => $this->l('Order %'),
'width' => 'auto',
'type' => 'float'
],
'percentage_shipping' => [
'title' => $this->l('Shipping %'),
'width' => 'auto',
'type' => 'float'
],
'value_order' => [
'title' => $this->l('Order %'),
'width' => 'auto',
'type' => 'float'
],
'value_shipping' => [
'title' => $this->l('Shipping %'),
'width' => 'auto',
'type' => 'float'
],
);
// This adds a multiple deletion button
$this->bulk_actions = array(
'delete' => array(
'text' => $this->l('Delete selected'),
'confirm' => $this->l('Delete selected items?')
)
);
parent::__construct();
}
}
我的问题是我不知道如何在 $this->fields_list
数组中添加客户列,以便在列表中显示客户的姓名。
我该怎么做?我正在使用 PrestaShop 1.6.1.12。 感谢您的帮助
您需要添加 select 来连接名称
$this->_select = 'a.*, CONCAT(c.`firstname`, \' \', c.`lastname`) AS `customer`';
然后,使用 customer:
而不是字段 id_customer'customer' => [
'title' => $this->l('Customer'),
'width' => 'auto'
],
这很简单,您只需像这样向您的列添加一个回调
...
'id_customer' => [
'title' => $this->l('Customer'),
'width' => 'auto',
'callback' => 'getCustomerName'
],
...
// Then add this function in the same controller
public function getCustomerName($id) {
if (!empty($id)) {
$customer = new Customer($id);
if(Validate::isLoadedObject($customer)) {
return $customer->firstname.' '.$customer->lastname;
}
}
return '';
}