guild/frontend/modules/card/models/UserCard.php
2023-11-08 12:06:22 +03:00

62 lines
1.5 KiB
PHP
Executable File

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