Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
QuoteSetup.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Quote\Setup;
7 
9 use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;
14 
21 class QuoteSetup extends EavSetup
22 {
26  protected $_config;
27 
31  protected $_encryptor;
32 
36  private static $connectionName = 'checkout';
37 
45  public function __construct(
47  Context $context,
48  CacheInterface $cache,
49  CollectionFactory $attrGroupCollectionFactory,
51  ) {
52  $this->_config = $config;
53  $this->_encryptor = $context->getEncryptor();
54  parent::__construct($setup, $context, $cache, $attrGroupCollectionFactory);
55  }
56 
62  protected $_flatEntityTables = [
63  'quote' => 'quote',
64  'quote_item' => 'quote_item',
65  'quote_address' => 'quote_address',
66  'quote_address_item' => 'quote_address_item',
67  'quote_address_rate' => 'quote_shipping_rate',
68  'quote_payment' => 'quote_payment',
69  ];
70 
77  protected function _flatTableExist($table)
78  {
79  $tablesList = $this->getConnection()->listTables();
80  return in_array(
81  strtoupper($this->getTable($table)),
82  array_map('strtoupper', $tablesList)
83  );
84  }
85 
94  public function addAttribute($entityTypeId, $code, array $attr)
95  {
96  if (isset(
97  $this->_flatEntityTables[$entityTypeId]
98  ) && $this->_flatTableExist(
99  $this->_flatEntityTables[$entityTypeId]
100  )
101  ) {
102  $this->_addFlatAttribute($this->_flatEntityTables[$entityTypeId], $code, $attr);
103  } else {
104  parent::addAttribute($entityTypeId, $code, $attr);
105  }
106  return $this;
107  }
108 
117  protected function _addFlatAttribute($table, $attribute, $attr)
118  {
119  $tableInfo = $this->getConnection()
120  ->describeTable($this->getTable($table));
121  if (isset($tableInfo[$attribute])) {
122  return $this;
123  }
124  $columnDefinition = $this->_getAttributeColumnDefinition($attribute, $attr);
125  $this->getConnection()->addColumn(
126  $this->getTable($table),
127  $attribute,
128  $columnDefinition
129  );
130  return $this;
131  }
132 
142  {
143  // Convert attribute type to column info
144  $data['type'] = isset($data['type']) ? $data['type'] : 'varchar';
145  $type = null;
146  $length = null;
147  switch ($data['type']) {
148  case 'timestamp':
150  break;
151  case 'datetime':
153  break;
154  case 'decimal':
156  $length = '12,4';
157  break;
158  case 'int':
160  break;
161  case 'text':
163  $length = 65536;
164  break;
165  case 'char':
166  case 'varchar':
168  $length = 255;
169  break;
170  }
171  if ($type !== null) {
172  $data['type'] = $type;
173  $data['length'] = $length;
174  }
175 
176  $data['nullable'] = isset($data['required']) ? !$data['required'] : true;
177  $data['comment'] = isset($data['comment']) ? $data['comment'] : ucwords(str_replace('_', ' ', $code));
178  return $data;
179  }
180 
186  public function getConfigModel()
187  {
188  return $this->_config;
189  }
190 
194  public function getEncryptor()
195  {
196  return $this->_encryptor;
197  }
198 
204  public function getConnection()
205  {
206  return $this->getSetup()->getConnection(self::$connectionName);
207  }
208 
215  public function getTable($table)
216  {
217  return $this->getSetup()->getTable($table, self::$connectionName);
218  }
219 }
_getAttributeColumnDefinition($code, $data)
Definition: QuoteSetup.php:141
$attr
Definition: text.phtml:8
$config
Definition: fraud_order.php:17
__construct(ModuleDataSetupInterface $setup, Context $context, CacheInterface $cache, CollectionFactory $attrGroupCollectionFactory, ScopeConfigInterface $config)
Definition: QuoteSetup.php:45
addAttribute($entityTypeId, $code, array $attr)
Definition: QuoteSetup.php:94
_addFlatAttribute($table, $attribute, $attr)
Definition: QuoteSetup.php:117
$type
Definition: item.phtml:13
$setup
Definition: trigger.php:12
$table
Definition: trigger.php:14
$code
Definition: info.phtml:12