本文整理汇总了PHP中app\modules\shop\models\Product::linkToCategory方法的典型用法代码示例。如果您正苦于以下问题:PHP Product::linkToCategory方法的具体用法?PHP Product::linkToCategory怎么用?PHP Product::linkToCategory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\modules\shop\models\Product
的用法示例。
在下文中一共展示了Product::linkToCategory方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: linkProduct
/**
* @param int|Product|null $product
* @param bool $asMainCategory
* @return bool
*/
public function linkProduct($product = null, $asMainCategory = false)
{
if ($product instanceof Product) {
return $product->linkToCategory($this->id, $asMainCategory);
} elseif (is_int($product) || is_string($product)) {
$product = intval($product);
if (null !== ($product = Product::findById($product, null))) {
return $product->linkToCategory($this->id, $asMainCategory);
}
}
return false;
}
示例2: createProduct
protected function createProduct($item = [], $createNotExists = true)
{
if (empty($item) || !isset($item[static::ELEMENT_ID]) || !isset($item[static::ELEMENT_NAIMENOVANIE])) {
return false;
}
$guid = CommercemlGuid::findOne(['guid' => $item[static::ELEMENT_ID], 'type' => 'PRODUCT']);
if (empty($guid) && $createNotExists) {
$category = !empty($item['categories']) ? array_shift($item['categories']) : null;
$product = new Product();
$product->name = $product->title = $product->h1 = $item[static::ELEMENT_NAIMENOVANIE];
$product->slug = Helper::createSlug($product->name);
$product->main_category_id = isset($this->categoryCache[$category]) ? $this->categoryCache[$category] : $this->rootCategoryCache;
$product->content = empty($item[static::ELEMENT_OPISANIE]) ? '' : $item[static::ELEMENT_OPISANIE];
if ($product->validate() && $product->save()) {
$product->refresh();
$product->linkToCategory($this->rootCategoryCache);
$guid = new CommercemlGuid();
$guid->guid = $item[static::ELEMENT_ID];
$guid->name = $item[static::ELEMENT_NAIMENOVANIE];
$guid->model_id = $product->id;
$guid->type = 'PRODUCT';
$guid->save();
return true;
}
}
if (!empty($guid)) {
/** @var Product $product */
$product = isset($product) ? $product : $guid->product;
if (!empty($product)) {
$product->price = empty($item['price']) ?: array_shift($item['price']);
$product->content = empty($item[static::ELEMENT_OPISANIE]) ?: $item[static::ELEMENT_OPISANIE];
$product->name = empty($item[static::ELEMENT_NAIMENOVANIE]) ?: $item[static::ELEMENT_NAIMENOVANIE];
if (!empty($item['properties'])) {
AbstractPropertyEavModel::setTableName($this->objectProduct->eav_table_name);
$product_eav = array_reduce(AbstractPropertyEavModel::findByModelId($product->id), function ($result, $item) {
$key = $item->property_group_id . ':' . $item->key;
$result[$key] = $item;
return $result;
}, []);
$product_groups = array_reduce(ObjectPropertyGroup::getForModel($product), function ($result, $item) {
$result[] = $item->property_group_id;
return $result;
}, []);
$product_osv = array_reduce(ObjectStaticValues::findAll(['object_id' => $this->objectProduct->id, 'object_model_id' => $product->id]), function ($result, $item) {
$result[] = $item->property_static_value_id;
return $result;
}, []);
foreach ($item['properties'] as $key => $value) {
if (isset(static::$propertiesCache[$key])) {
/** @var Property $prop */
$prop = static::$propertiesCache[$key];
if (!in_array($prop->property_group_id, $product_groups)) {
$objectGroup = new ObjectPropertyGroup();
$objectGroup->object_id = $this->objectProduct->id;
$objectGroup->object_model_id = $product->id;
$objectGroup->property_group_id = $prop->property_group_id;
$objectGroup->save();
}
if ($prop->has_static_values) {
$psv = PropertyStaticValues::findOne(['value' => $value]);
if (empty($psv)) {
$psv = new PropertyStaticValues();
$psv->name = $psv->value = $value;
$psv->property_id = $prop->id;
$psv->slug = Helper::createSlug($value);
if ($psv->validate() && $psv->save()) {
$psv->refresh();
} else {
$psv = null;
}
}
if (!empty($psv) && !in_array($psv->id, $product_osv)) {
$osv = new ObjectStaticValues();
$osv->object_id = $this->objectProduct->id;
$osv->object_model_id = $product->id;
$osv->property_static_value_id = $psv->id;
$osv->save();
}
} elseif ($prop->is_eav) {
$_key = $prop->property_group_id . ':' . $prop->key;
if (isset($product_eav[$_key])) {
/** @var AbstractPropertyEavModel $eav */
$eav = $product_eav[$_key];
$eav->value = $value;
$eav->save();
} else {
$eav = new AbstractPropertyEavModel();
$eav->object_model_id = $product->id;
$eav->property_group_id = $prop->property_group_id;
$eav->key = $prop->key;
$eav->value = $value;
$eav->save();
}
}
}
}
}
$product->save();
return true;
}
//.........这里部分代码省略.........