guild/frontend/modules/card/models/UserCard.php

64 lines
1.6 KiB
PHP
Raw Normal View History

2019-11-18 16:14:06 +03:00
<?php
namespace frontend\modules\card\models;
use common\models\CardSkill;
2023-11-08 12:06:22 +03:00
use frontend\modules\api\models\UserCardPortfolioProjects;
2021-07-28 17:08:08 +03:00
use Yii;
2023-11-08 12:06:22 +03:00
use yii\db\ActiveQuery;
2019-11-18 16:14:06 +03:00
use yii\helpers\ArrayHelper;
2023-11-08 12:06:22 +03:00
/**
* @property UserCardPortfolioProjects[] $userCardPortfolioProjects
*/
2019-11-18 16:14:06 +03:00
class UserCard extends \common\models\UserCard
{
public $fields;
public $skill;
public function init()
{
parent::init();
$skill = ArrayHelper::getColumn(
2021-07-28 17:08:08 +03:00
CardSkill::find()->where(['card_id' => Yii::$app->request->get('id')])->all(),
2019-11-18 16:14:06 +03:00
'skill_id'
);
if (!empty($skill)) {
$this->skill = $skill;
}
}
public function afterSave($insert, $changedAttributes)
{
2023-11-22 12:42:36 +03:00
if (Yii::$app->request->post('UserCard')) {
$post = Yii::$app->request->post('UserCard');
2019-11-18 16:14:06 +03:00
2023-11-22 12:42:36 +03:00
if ($post['skill']) {
CardSkill::deleteAll(['card_id' => $this->id]);
2019-11-18 16:14:06 +03:00
2023-11-22 12:42:36 +03:00
foreach ($post['skill'] as $item) {
$skill = new CardSkill();
$skill->skill_id = $item;
$skill->card_id = $this->id;
2019-11-18 16:14:06 +03:00
2023-11-22 12:42:36 +03:00
$skill->save();
}
2019-11-18 16:14:06 +03:00
}
}
parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
}
2021-07-28 17:08:08 +03:00
public static function findByUserId($userId): ?UserCard
{
return self::findOne(['id_user' => $userId]);
}
2023-11-08 12:06:22 +03:00
public function getUserCardPortfolioProjects(): ActiveQuery
{
return $this->hasMany(UserCardPortfolioProjects::class, ['card_id' => 'id']);
}
2019-11-18 16:14:06 +03:00
}