1

I am using yii2 advanced template improved

Not sure if relevant to problem/a solution but to get to the category I have a form with some javascript which on change redirects the user to the category selected.

I have the following code which allows me to access the posts within that category id I go to localhost:8888/advanced/article/category?id=1

The problem at the minute is that if I call getCategoryName function in my model from my category view the id parameter isn't being passed to the model function therefore when using getCategoryName defaults to sport.

 public function actionCategory($id)
{

    $model = new Article();

    $searchModel = new ArticleSearch();

    $query = Article::find()
    ->where(['category' => $id]);

     $dataProvider = new ActiveDataProvider([
                    'query' => $query,
                    ]);

    return $this->render('category', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
        'model'=>$model,
    ]);
}

Then within my view I use the following which works to an extent in terms of executing the model function, however I am unsure on how to pass the parameter/current category id to the model function. The below code work for the _index & in the single article view.

<?= $model->CategoryName ?>

This is my model function

public function getCategoryName($category = null)
{
    $category = (empty($category)) ? $this->category : $category ;

    if ($category === self::CATEGORY_ECONOMY)
    {
        return Yii::t('app', 'Economy');
    }
    elseif ($category === self::CATEGORY_SOCIETY)
    {
        return Yii::t('app', 'Society');
    }
    else
    {
        return Yii::t('app', 'Sport');
    }
}
con322
  • 1,119
  • 7
  • 16
  • 29
  • 1
    Don't get it. What's the problem / error? – arogachev Feb 13 '15 at 10:40
  • No id/parameter is being pass to the getCategoryName function in my model when I use in my view therefore CategoryName always defaults to Sport. – con322 Feb 13 '15 at 10:51
  • Because you are not passing anything and calling it through getter here: `= $model->CategoryName ?>`. – arogachev Feb 13 '15 at 10:54
  • I thought that was the case but was unsure on how to pass the id to the model function, can that be done in view or do I need to do it in my controller. – con322 Feb 13 '15 at 10:55

2 Answers2

0

Use something like this, in your view

$request = Yii::$app->request;
$id = $request->get('id');//get id from request or change according to your requirement
echo  $model->getCategoryName($id);//full method name here
RN Kushwaha
  • 2,081
  • 3
  • 29
  • 40
  • I faced the same problem while creating with basic yii2 application today. Let me know if it not works for you. – RN Kushwaha Feb 21 '15 at 15:11
  • I get the id and it changes when changing category and I can see the id changing in my view, however when using the getCategoryName function it always displays the category name sport no matter what. – con322 Feb 21 '15 at 16:58
  • @con322 Then something is wrong in your logic. Just try to print there in getCategoryName() what value its getting. `public function getCategoryName($category = null) { $category = ($category>0) ? $category: $this->category; //echo $category;exit;//try to see what is coming here if ($category === self::CATEGORY_ECONOMY) { return Yii::t('app', 'Economy'); } elseif ($category === self::CATEGORY_SOCIETY) { return Yii::t('app', 'Society'); } else { return Yii::t('app', 'Sport'); } }` – RN Kushwaha Feb 21 '15 at 18:24
0

Try to use this. change === to ==:

public function getCategoryName($category = null)
{
    $category = (empty($category)) ? $this->category : $category ;

    if ($category == self::CATEGORY_ECONOMY)
    {
        return Yii::t('app', 'Economy');
    }
    elseif ($category == self::CATEGORY_SOCIETY)
    {
        return Yii::t('app', 'Society');
    }
    else
    {
        return Yii::t('app', 'Sport');
    }
}
Serghei Leonenco
  • 3,478
  • 2
  • 8
  • 16