本文整理汇总了PHP中Magento\Eav\Model\Entity\Attribute\AbstractAttribute类的典型用法代码示例。如果您正苦于以下问题:PHP AbstractAttribute类的具体用法?PHP AbstractAttribute怎么用?PHP AbstractAttribute使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AbstractAttribute类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getScopeValue
/**
* @param ScopeInterface $scope
* @param AbstractAttribute $attribute
* @param bool $useDefault
* @return string
*/
protected function getScopeValue(ScopeInterface $scope, AbstractAttribute $attribute, $useDefault = false)
{
if ($attribute instanceof CatalogEavAttribute) {
$useDefault = $useDefault || $attribute->isScopeGlobal();
}
return parent::getScopeValue($scope, $attribute, $useDefault);
}
示例2: getMediaEntriesDataCollection
/**
* @param \Magento\Catalog\Model\Product $product
* @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
* @return array
*/
protected function getMediaEntriesDataCollection(\Magento\Catalog\Model\Product $product, \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute)
{
$attributeCode = $attribute->getAttributeCode();
$mediaData = $product->getData($attributeCode);
if (!empty($mediaData['images']) && is_array($mediaData['images'])) {
return $mediaData['images'];
}
return [];
}
示例3: deleteProductData
/**
* Delete product data
*
* @param \Magento\Catalog\Model\Product $product
* @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
* @return $this
*/
public function deleteProductData($product, $attribute)
{
$where = array('entity_id = ?' => (int) $product->getId(), 'attribute_id = ?' => (int) $attribute->getId());
$adapter = $this->_getWriteAdapter();
if (!$attribute->isScopeGlobal()) {
$storeId = $product->getStoreId();
if ($storeId) {
$where['website_id IN(?)'] = array(0, $this->_storeManager->getStore($storeId)->getWebsiteId());
}
}
$adapter->delete($this->getMainTable(), $where);
return $this;
}
示例4: deleteProductData
/**
* Delete product data
*
* @param \Magento\Catalog\Model\Product $product
* @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
* @return $this
*/
public function deleteProductData($product, $attribute)
{
$where = ['entity_id = ?' => (int) $product->getId(), 'attribute_id = ?' => (int) $attribute->getId()];
$connection = $this->getConnection();
if (!$attribute->isScopeGlobal()) {
$storeId = $product->getStoreId();
if ($storeId) {
$where['website_id IN(?)'] = [0, $this->_storeManager->getStore($storeId)->getWebsiteId()];
}
}
$connection->delete($this->getMainTable(), $where);
return $this;
}
示例5: _saveAttributeValue
/**
* Insert or Update attribute data
*
* @param \Magento\Catalog\Model\AbstractModel $object
* @param AbstractAttribute $attribute
* @param mixed $value
* @return $this
*/
protected function _saveAttributeValue($object, $attribute, $value)
{
$connection = $this->getConnection();
$storeId = (int) $this->_storeManager->getStore($object->getStoreId())->getId();
$table = $attribute->getBackend()->getTable();
$entityId = $this->resolveEntityId($object->getId(), $table);
/**
* If we work in single store mode all values should be saved just
* for default store id
* In this case we clear all not default values
*/
if ($this->_storeManager->hasSingleStore()) {
$storeId = $this->getDefaultStoreId();
$connection->delete($table, ['attribute_id = ?' => $attribute->getAttributeId(), $this->getLinkField() . ' = ?' => $entityId, 'store_id <> ?' => $storeId]);
}
$data = new \Magento\Framework\DataObject(['attribute_id' => $attribute->getAttributeId(), 'store_id' => $storeId, $this->getLinkField() => $entityId, 'value' => $this->_prepareValueForSave($value, $attribute)]);
$bind = $this->_prepareDataForTable($data, $table);
if ($attribute->isScopeStore()) {
/**
* Update attribute value for store
*/
$this->_attributeValuesToSave[$table][] = $bind;
} elseif ($attribute->isScopeWebsite() && $storeId != $this->getDefaultStoreId()) {
/**
* Update attribute value for website
*/
$storeIds = $this->_storeManager->getStore($storeId)->getWebsite()->getStoreIds(true);
foreach ($storeIds as $storeId) {
$bind['store_id'] = (int) $storeId;
$this->_attributeValuesToSave[$table][] = $bind;
}
} else {
/**
* Update global attribute value
*/
$bind['store_id'] = $this->getDefaultStoreId();
$this->_attributeValuesToSave[$table][] = $bind;
}
return $this;
}
示例6: build
/**
* Build validation rules
*
* @param AbstractAttribute $attribute
* @param array $data
* @return array
*/
public function build(AbstractAttribute $attribute, array $data)
{
$validation = [];
if (isset($data['required']) && $data['required'] == 1) {
$validation = array_merge($validation, ['required-entry' => true]);
}
if ($attribute->getFrontendInput() === 'price') {
$validation = array_merge($validation, ['validate-zero-or-greater' => true]);
}
if ($attribute->getValidateRules()) {
$validation = array_merge($validation, $attribute->getValidateRules());
}
$rules = [];
foreach ($validation as $type => $ruleName) {
$rule = [$type => $ruleName];
if ($type === 'input_validation') {
$rule = isset($this->validationRules[$ruleName]) ? $this->validationRules[$ruleName] : [];
}
$rules = array_merge($rules, $rule);
}
return $rules;
}
示例7: build
/**
* Build validation rules
*
* @param AbstractAttribute $attribute
* @param array $data
* @return array
*/
public function build(AbstractAttribute $attribute, array $data)
{
$rules = [];
if (isset($data['required']) && $data['required'] == 1) {
$rules['required-entry'] = true;
}
$validation = $attribute->getValidateRules();
if (!empty($validation)) {
foreach ($validation as $type => $ruleName) {
switch ($type) {
case 'input_validation':
if (isset($this->validationRul[$type][$ruleName])) {
$rules = array_merge($rules, $this->validationRul[$type][$ruleName]);
}
break;
case 'min_text_length':
case 'max_text_length':
$rules = array_merge($rules, [$type => $ruleName]);
break;
}
}
}
return $rules;
}
示例8: setUp
/**
* Set up test
*
* @return void
*/
protected function setUp()
{
$this->resource = $this->getMock('Magento\\Framework\\App\\Resource', ['getConnection', 'getTableName'], [], '', false);
$this->ruleCollectionFactory = $this->getMock('Magento\\CatalogRule\\Model\\Resource\\Rule\\CollectionFactory', ['create', 'addFieldToFilter'], [], '', false);
$this->backend = $this->getMock('Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\AbstractBackend', [], [], '', false);
$this->select = $this->getMock('Magento\\Framework\\DB\\Select', [], [], '', false);
$this->connection = $this->getMock('Magento\\Framework\\DB\\Adapter\\AdapterInterface');
$this->db = $this->getMock('Zend_Db_Statement_Interface', [], [], '', false);
$this->website = $this->getMock('Magento\\Store\\Model\\Website', [], [], '', false);
$this->storeManager = $this->getMock('Magento\\Store\\Model\\StoreManagerInterface', [], [], '', false);
$this->combine = $this->getMock('Magento\\Rule\\Model\\Condition\\Combine', [], [], '', false);
$this->rules = $this->getMock('Magento\\CatalogRule\\Model\\Rule', [], [], '', false);
$this->logger = $this->getMock('Psr\\Log\\LoggerInterface', [], [], '', false);
$this->attribute = $this->getMock('Magento\\Eav\\Model\\Entity\\Attribute\\AbstractAttribute', [], [], '', false);
$this->priceCurrency = $this->getMock('Magento\\Framework\\Pricing\\PriceCurrencyInterface');
$this->dateFormat = $this->getMock('Magento\\Framework\\Stdlib\\DateTime', [], [], '', false);
$this->dateTime = $this->getMock('Magento\\Framework\\Stdlib\\DateTime\\DateTime', [], [], '', false);
$this->eavConfig = $this->getMock('Magento\\Eav\\Model\\Config', [], [], '', false);
$this->product = $this->getMock('Magento\\Catalog\\Model\\Product', [], [], '', false);
$this->productFactory = $this->getMock('Magento\\Catalog\\Model\\ProductFactory', ['create'], [], '', false);
$this->connection->expects($this->any())->method('select')->will($this->returnValue($this->select));
$this->connection->expects($this->any())->method('query')->will($this->returnValue($this->db));
$this->select->expects($this->any())->method('distinct')->will($this->returnSelf());
$this->select->expects($this->any())->method('where')->will($this->returnSelf());
$this->select->expects($this->any())->method('from')->will($this->returnSelf());
$this->select->expects($this->any())->method('order')->will($this->returnSelf());
$this->resource->expects($this->any())->method('getConnection')->will($this->returnValue($this->connection));
$this->resource->expects($this->any())->method('getTableName')->will($this->returnArgument(0));
$this->storeManager->expects($this->any())->method('getWebsites')->will($this->returnValue([$this->website]));
$this->storeManager->expects($this->any())->method('getWebsite')->will($this->returnValue($this->website));
$this->rules->expects($this->any())->method('getId')->will($this->returnValue(1));
$this->rules->expects($this->any())->method('getWebsiteIds')->will($this->returnValue([1]));
$this->rules->expects($this->any())->method('getConditions')->will($this->returnValue($this->combine));
$this->rules->expects($this->any())->method('getCustomerGroupIds')->will($this->returnValue([1]));
$this->ruleCollectionFactory->expects($this->any())->method('create')->will($this->returnSelf());
$this->ruleCollectionFactory->expects($this->any())->method('addFieldToFilter')->will($this->returnValue([$this->rules]));
$this->product->expects($this->any())->method('load')->will($this->returnSelf());
$this->product->expects($this->any())->method('getId')->will($this->returnValue(1));
$this->product->expects($this->any())->method('getWebsiteIds')->will($this->returnValue([1]));
$this->combine->expects($this->any())->method('validate')->will($this->returnValue(true));
$this->attribute->expects($this->any())->method('getBackend')->will($this->returnValue($this->backend));
$this->eavConfig->expects($this->any())->method('getAttribute')->will($this->returnValue($this->attribute));
$this->productFactory->expects($this->any())->method('create')->will($this->returnValue($this->product));
$this->indexBuilder = new \Magento\CatalogRule\Model\Indexer\IndexBuilder($this->ruleCollectionFactory, $this->priceCurrency, $this->resource, $this->storeManager, $this->logger, $this->eavConfig, $this->dateFormat, $this->dateTime, $this->productFactory);
}
示例9: getFlatUpdateSelect
/**
* Retrieve Select for update Flat data
*
* @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
* @param int $store
* @param bool $hasValueField flag which require option value
* @return \Magento\Framework\DB\Select
*/
public function getFlatUpdateSelect(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute, $store, $hasValueField = true)
{
$adapter = $this->_getReadAdapter();
$attributeTable = $attribute->getBackend()->getTable();
$attributeCode = $attribute->getAttributeCode();
$joinConditionTemplate = "%s.entity_id = %s.entity_id" . " AND %s.entity_type_id = " . $attribute->getEntityTypeId() . " AND %s.attribute_id = " . $attribute->getId() . " AND %s.store_id = %d";
$joinCondition = sprintf($joinConditionTemplate, 'e', 't1', 't1', 't1', 't1', \Magento\Store\Model\Store::DEFAULT_STORE_ID);
if ($attribute->getFlatAddChildData()) {
$joinCondition .= ' AND e.child_id = t1.entity_id';
}
$valueExpr = $adapter->getCheckSql('t2.value_id > 0', 't2.value', 't1.value');
/** @var $select \Magento\Framework\DB\Select */
$select = $adapter->select()->joinLeft(['t1' => $attributeTable], $joinCondition, [])->joinLeft(['t2' => $attributeTable], sprintf($joinConditionTemplate, 't1', 't2', 't2', 't2', 't2', $store), [$attributeCode => $valueExpr]);
if ($attribute->getFrontend()->getInputType() != 'multiselect' && $hasValueField) {
$valueIdExpr = $adapter->getCheckSql('to2.value_id > 0', 'to2.value', 'to1.value');
$select->joinLeft(['to1' => $this->getTable('eav_attribute_option_value')], "to1.option_id = {$valueExpr} AND to1.store_id = 0", [])->joinLeft(['to2' => $this->getTable('eav_attribute_option_value')], $adapter->quoteInto("to2.option_id = {$valueExpr} AND to2.store_id = ?", $store), [$attributeCode . '_value' => $valueIdExpr]);
}
if ($attribute->getFlatAddChildData()) {
$select->where('e.is_child = 0');
}
return $select;
}
示例10: setUp
/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @return void
*/
protected function setUp()
{
$this->zendDbMock = $this->getMockBuilder('Zend_Db_Statement_Interface')->getMock();
$this->zendDbMock->expects($this->any())->method('fetchColumn')->willReturn([]);
$this->selectMock = $this->getMockBuilder('Magento\\Framework\\DB\\Select')->disableOriginalConstructor()->setMethods(['from', 'where', 'joinInner', 'joinLeft', 'having', 'useStraightJoin', 'insertFromSelect', '__toString'])->getMock();
$this->selectMock->expects($this->any())->method('from')->willReturnSelf();
$this->selectMock->expects($this->any())->method('where')->willReturnSelf();
$this->selectMock->expects($this->any())->method('joinInner')->willReturnSelf();
$this->selectMock->expects($this->any())->method('joinLeft')->willReturnSelf();
$this->selectMock->expects($this->any())->method('having')->willReturnSelf();
$this->selectMock->expects($this->any())->method('useStraightJoin')->willReturnSelf();
$this->selectMock->expects($this->any())->method('insertFromSelect')->willReturnSelf();
$this->selectMock->expects($this->any())->method('__toString')->willReturn('string');
$this->connectionMock = $this->getMockBuilder('Magento\\Framework\\DB\\Adapter\\AdapterInterface')->getMock();
$this->connectionMock->expects($this->any())->method('select')->willReturn($this->selectMock);
$this->connectionMock->expects($this->any())->method('query')->willReturn($this->zendDbMock);
$this->resourceMock = $this->getMockBuilder('Magento\\Framework\\App\\Resource')->disableOriginalConstructor()->getMock();
$this->resourceMock->expects($this->any())->method('getConnection')->willReturn($this->connectionMock);
$this->resourceMock->expects($this->any())->method('getTableName')->will($this->returnCallback(function ($arg) {
return $arg;
}));
$this->contextMock = $this->getMockBuilder('Magento\\Framework\\Model\\Resource\\Db\\Context')->disableOriginalConstructor()->getMock();
$this->contextMock->expects($this->any())->method('getResources')->willReturn($this->resourceMock);
$this->loggerMock = $this->getMockBuilder('Psr\\Log\\LoggerInterface')->getMock();
$dateTime = $this->getMockBuilder('DateTime')->getMock();
$this->timezoneMock = $this->getMockBuilder('Magento\\Framework\\Stdlib\\DateTime\\TimezoneInterface')->getMock();
$this->timezoneMock->expects($this->any())->method('scopeDate')->willReturn($dateTime);
$this->dateTimeMock = $this->getMockBuilder('Magento\\Framework\\Stdlib\\DateTime')->getMock();
$this->flagMock = $this->getMockBuilder('Magento\\Reports\\Model\\Flag')->disableOriginalConstructor()->setMethods(['setReportFlagCode', 'unsetData', 'loadSelf', 'setFlagData', 'setLastUpdate', 'save'])->getMock();
$this->flagFactoryMock = $this->getMockBuilder('Magento\\Reports\\Model\\FlagFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock();
$this->flagFactoryMock->expects($this->any())->method('create')->willReturn($this->flagMock);
$this->validatorMock = $this->getMockBuilder('Magento\\Framework\\Stdlib\\DateTime\\Timezone\\Validator')->disableOriginalConstructor()->getMock();
$this->backendMock = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\AbstractBackend')->disableOriginalConstructor()->getMock();
$this->attributeMock = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute\\AbstractAttribute')->disableOriginalConstructor()->getMock();
$this->attributeMock->expects($this->any())->method('getBackend')->willReturn($this->backendMock);
$this->productMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Resource\\Product')->disableOriginalConstructor()->getMock();
$this->productMock->expects($this->any())->method('getAttribute')->willReturn($this->attributeMock);
$this->helperMock = $this->getMockBuilder('Magento\\Reports\\Model\\Resource\\Helper')->disableOriginalConstructor()->getMock();
$this->viewed = new Viewed($this->contextMock, $this->loggerMock, $this->timezoneMock, $this->flagFactoryMock, $this->dateTimeMock, $this->validatorMock, $this->productMock, $this->helperMock);
}
示例11: getAttributeType
/**
* Get attribute type for upcoming validation.
*
* @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute|\Magento\Eav\Model\Entity\Attribute $attribute
* @return string
*/
public static function getAttributeType(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute)
{
$frontendInput = $attribute->getFrontendInput();
if ($attribute->usesSource() && in_array($frontendInput, ['select', 'multiselect', 'boolean'])) {
return $frontendInput;
} elseif ($attribute->isStatic()) {
return $frontendInput == 'date' ? 'datetime' : 'varchar';
} else {
return $attribute->getBackendType();
}
}
示例12: getAttributeOptions
/**
* Returns attributes all values in label-value or value-value pairs form. Labels are lower-cased.
*
* @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
* @return array
*/
public function getAttributeOptions(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute)
{
$options = [];
if ($attribute->usesSource()) {
// should attribute has index (option value) instead of a label?
$index = in_array($attribute->getAttributeCode(), $this->_indexValueAttributes) ? 'value' : 'label';
// only default (admin) store values used
$attribute->setStoreId(\Magento\Store\Model\Store::DEFAULT_STORE_ID);
try {
foreach ($attribute->getSource()->getAllOptions(false) as $option) {
foreach (is_array($option['value']) ? $option['value'] : [$option] as $innerOption) {
if (strlen($innerOption['value'])) {
// skip ' -- Please Select -- ' option
$options[$innerOption['value']] = $innerOption[$index];
}
}
}
} catch (\Exception $e) {
// ignore exceptions connected with source models
}
}
return $options;
}
示例13: _isAttributeValueEmpty
/**
* Check is attribute value empty
*
* @param AbstractAttribute $attribute
* @param mixed $value
* @return bool
*/
protected function _isAttributeValueEmpty(AbstractAttribute $attribute, $value)
{
return $attribute->isValueEmpty($value);
}
示例14: testBeforeSave
public function testBeforeSave()
{
$this->attributeMock->expects($this->any())->method('getName')->willReturn('attribute_name');
$object = new DataObject(['use_config_attribute_name' => true]);
$this->model->beforeSave($object);
$this->assertEquals(BooleanSource::VALUE_USE_CONFIG, $object->getData('attribute_name'));
}
示例15: testBuild
/**
* @param array $data
* @param array $expected
* @dataProvider buildDataProvider
*/
public function testBuild($attributeInputType, $validateRules, $data, $expected)
{
$this->attributeMock->expects($this->once())->method('getFrontendInput')->willReturn($attributeInputType);
$this->attributeMock->expects($this->any())->method('getValidateRules')->willReturn($validateRules);
$validationRules = $this->subject->build($this->attributeMock, $data);
$this->assertEquals($expected, $validationRules);
}