5959 * @property string $meta_keywords
6060 * @property string $seo_h1
6161 * @property integer|null $cms_site_id
62- *
62+ * @property integer|null $cms_content_model_id
63+ *
6364 * @property string|null $external_id
6465 * @property integer|null $main_cce_id
6566 * @property integer|null $main_cce_at
115116 * @property string $seoName
116117 *
117118 * @property CmsContentElement $mainCmsContentElement
119+ * @property CmsContentModel $cmsContentModel
118120 *
119121 */
120122class CmsContentElement extends RelatedElementModel
@@ -342,6 +344,7 @@ public function rules()
342344 'main_cce_by ' ,
343345 'is_adult ' ,
344346 'sx_id ' ,
347+ 'cms_content_model_id ' ,
345348 ],
346349 'integer ' ,
347350 ],
@@ -355,6 +358,7 @@ public function rules()
355358 [['external_id ' ], 'trim ' ],
356359 [['external_id ' ], 'string ' , 'max ' => 255 ],
357360 [['external_id ' ], 'default ' , 'value ' => null ],
361+ [['cms_content_model_id ' ], 'default ' , 'value ' => null ],
358362 /*[
359363 ['content_id', 'code'],
360364 'unique',
@@ -521,6 +525,16 @@ function ($attribute) {
521525
522526 ]);
523527 }
528+
529+ /**
530+ * @return CmsContentModel
531+ */
532+ public function getCmsContentModel ()
533+ {
534+ return $ this ->hasOne (CmsContentModel::class, ['id ' => 'cms_content_model_id ' ])->from (['cmsContentModel ' => CmsContentModel::tableName ()]);
535+
536+ }
537+
524538 /**
525539 * Валидация родительского элемента
526540 *
@@ -1119,6 +1133,48 @@ public static function find()
11191133 return new CmsContentElementActiveQuery (get_called_class (), ['is_active ' => false ]);
11201134 }
11211135
1136+
1137+
1138+ public function joinToModel (array $ element_ids = [])
1139+ {
1140+ if ($ element_ids ) {
1141+
1142+ $ t = \Yii::$ app ->db ->beginTransaction ();
1143+
1144+ try {
1145+ if (!$ this ->cms_content_model_id ) {
1146+ $ spModel = new CmsContentModel ();
1147+ if (!$ spModel ->save ()) {
1148+ throw new Exception (print_r ($ spModel ->errors , true ));
1149+ }
1150+
1151+ $ this ->cms_content_model_id = $ spModel ->id ;
1152+ $ this ->update (false , ['cms_content_model_id ' ]);
1153+ } else {
1154+ $ spModel = $ this ->cmsContentModel ;
1155+ }
1156+
1157+
1158+ foreach ($ element_ids as $ product_id ) {
1159+ if ($ product_id ) {
1160+
1161+ if ($ sp = CmsContentElement::findOne ($ product_id )) {
1162+ $ sp ->cms_content_model_id = $ spModel ->id ;
1163+ $ sp ->update (false , ['cms_content_model_id ' ]);
1164+ }
1165+
1166+ }
1167+
1168+ }
1169+
1170+ $ t ->commit ();
1171+ } catch (\Exception $ exception ) {
1172+ $ t ->rollBack ();
1173+ throw $ exception ;
1174+ }
1175+
1176+ }
1177+ }
11221178}
11231179
11241180
0 commit comments