9
9
9634682015-11-11 20:00:14
WordPress
963468, 2015-11-11 20:00:14

Why is the variable not getting into the yii2 session?

This is my Category.php model

public static function findOne($id)

      {

        $data = self::find()
              ->where(['id' => $id])
              ->one();

        return $data;

      }

my action in SiteController.php controller
public function actionAdd($id)
    {
      
        $item = Category::findOne($id);
        $session = Yii::$app->session;
        $session->set('item', $item);
        return $this->render('list',['item' => Yii::$app->session->get("item")]);
        }

my views
are index.php
use yii\helpers\Html;
use yii\helpers\Markdown;
<div class="btn-toolbar filters">
      <input type="checkbox" value="Music" >
    <?= Html::a('Add',  ['site/add', 'id'=>$item->'1'], ['class' => 'btn btn-success'])?>
    </label>
    <label class="btn btn-default">
      <input type="checkbox" value="Video" >
    <?= Html::a('Add',  ['site/add', 'id'=>$item->'2'], ['class' => 'btn btn-success'])?>
    </label>
    <label class="btn btn-default">
      <input type="checkbox" value="Pictures" >
        <?=$threecategory->title ?>
    <?= Html::a('Add',  ['site/add', 'id'=>$item->'3'], ['class' => 'btn btn-success'])?>
    </label>
</div>

this is list.php
<?php
use \yii\helpers\Html;
use yii\widgets\LinkPager; 
/* @var $this yii\web\View */
/* @var $products common\models\Product[] */
?
<p>Вы добавили категорию себе <a href="<?= Yii::$app->urlManager->createUrl(['site/add',  'item' => $item]) ?>"><?=$item ?>:</a></p>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Wolf, 2015-11-11
@mannaro

I am glad for you that you have such listings, but here you have 2 options:
a) go to freelance and look for a person who will solve all your problems for money;
b) formulate your question normally and show that you tried to understand the problem, then perhaps they will help you.
I think people have begun to forget that Toaster is a site to help developers; a site in order to understand the problem and learn how to solve it, and not for smart uncles to solve someone's problems, giving ready-made solutions.

M
Maxim Timofeev, 2015-11-12
@webinar

1. Yii has findOne($id) initially, why write a function in the model?
2. $session->set('item', $item); - sets, but does not add. You don't have an 'item' element in your session, so you should probably use $session->add('item', $item);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question