Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Region.php
Go to the documentation of this file.
1 <?php
8 
16 {
22  protected $_regionNameTable;
23 
27  protected $_localeResolver;
28 
34  public function __construct(
35  \Magento\Framework\Model\ResourceModel\Db\Context $context,
36  \Magento\Framework\Locale\ResolverInterface $localeResolver,
37  $connectionName = null
38  ) {
39  parent::__construct($context, $connectionName);
40  $this->_localeResolver = $localeResolver;
41  }
42 
48  protected function _construct()
49  {
50  $this->_init('directory_country_region', 'region_id');
51  $this->_regionNameTable = $this->getTable('directory_country_region_name');
52  }
53 
62  protected function _getLoadSelect($field, $value, $object)
63  {
64  $select = parent::_getLoadSelect($field, $value, $object);
65  $connection = $this->getConnection();
66 
67  $locale = $this->_localeResolver->getLocale();
69 
70  $regionField = $connection->quoteIdentifier($this->getMainTable() . '.' . $this->getIdFieldName());
71 
72  $condition = $connection->quoteInto('lrn.locale = ?', $locale);
73  $select->joinLeft(
74  ['lrn' => $this->_regionNameTable],
75  "{$regionField} = lrn.region_id AND {$condition}",
76  []
77  );
78 
79  if ($locale != $systemLocale) {
80  $nameExpr = $connection->getCheckSql('lrn.region_id is null', 'srn.name', 'lrn.name');
81  $condition = $connection->quoteInto('srn.locale = ?', $systemLocale);
82  $select->joinLeft(
83  ['srn' => $this->_regionNameTable],
84  "{$regionField} = srn.region_id AND {$condition}",
85  ['name' => $nameExpr]
86  );
87  } else {
88  $select->columns(['name'], 'lrn');
89  }
90 
91  return $select;
92  }
93 
103  protected function _loadByCountry($object, $countryId, $value, $field)
104  {
105  $connection = $this->getConnection();
106  $locale = $this->_localeResolver->getLocale();
107  $joinCondition = $connection->quoteInto('rname.region_id = region.region_id AND rname.locale = ?', $locale);
108  $select = $connection->select()->from(
109  ['region' => $this->getMainTable()]
110  )->joinLeft(
111  ['rname' => $this->_regionNameTable],
112  $joinCondition,
113  ['name']
114  )->where(
115  'region.country_id = ?',
116  $countryId
117  )->where(
118  "region.{$field} = ?",
119  $value
120  );
121 
122  $data = $connection->fetchRow($select);
123  if ($data) {
124  $object->setData($data);
125  }
126 
127  $this->_afterLoad($object);
128 
129  return $this;
130  }
131 
141  public function loadByCode(\Magento\Directory\Model\Region $region, $regionCode, $countryId)
142  {
143  return $this->_loadByCountry($region, $countryId, (string)$regionCode, 'code');
144  }
145 
154  public function loadByName(\Magento\Directory\Model\Region $region, $regionName, $countryId)
155  {
156  return $this->_loadByCountry($region, $countryId, (string)$regionName, 'default_name');
157  }
158 }
_getLoadSelect($field, $value, $object)
Definition: Region.php:62
loadByCode(\Magento\Directory\Model\Region $region, $regionCode, $countryId)
Definition: Region.php:141
$value
Definition: gender.phtml:16
loadByName(\Magento\Directory\Model\Region $region, $regionName, $countryId)
Definition: Region.php:154
_loadByCountry($object, $countryId, $value, $field)
Definition: Region.php:103
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Locale\ResolverInterface $localeResolver, $connectionName=null)
Definition: Region.php:34
$connection
Definition: bulk.php:13
_afterLoad(\Magento\Framework\Model\AbstractModel $object)
Definition: AbstractDb.php:641