How to create own action in Controller to make mysql insert

Hi to all,

i am new to yii framework and this is also my first framework.

i am trying to implement a Intress link/button for an article (not from facebook).

So i have written in my articleController.php an action


protected function actionIntress($article)

 

{

    $intress = new Intress;

    if($article->addIntress())

    {

         $this->refresh();

    }

   return $intress;

}

and created a function in my model Article.php


public function addIntress($intress)

{

     $intress = new Intress;

     $intress->user_id=$user->user_id;

     $intress->article_id=$this->article_id;

 

 return $intress->save();

 

}

and my link in article/view.php

looks like


<?php echo CHtml::link('Intress', array('submit'=>array('Intress')));?>

if i click on this Link page load and comes back to same page again but in database no new inserts

can any one help me?

This method in model class is not following a proper syntax for insertion of record. You can get more idea about a database concepts by reading this link. link





public function addIntress($intress)

{

     $intress = new Intress;

     $intress->user_id=$user->user_id;

     $intress->article_id=$this->article_id;

 

 return $intress->save();

 

}




i think an databse connecteion is allready established because an addcomment method that was created using gii is working and is also inside the same model class. Mabye u can properly tell what i am missing i am trying this more then few hours. It’s frustating

ArticleController.php


protected function actionIntress($article) 

{

    $intress = new Intress;

    if($article->addIntress($intress))

    {

         $this->refresh();

    }

   return $intress;

}

Model : article/Article.php


public function addIntress($intress)

{

     $intress->user_id=$user->user_id;

     $intress->article_id=$this->article_id;

 

 return $intress->save();

 

}





protected function actionIntress($article) 

{

    $intress = new Intress;

    if($article->addIntress())

    {

         $this->refresh();

    }

   return $intress;

}


public function addIntress($intress)

{

     $intress->user_id=$user->user_id;

     $intress->article_id=$this->article_id;

 

 return $intress->save();

 

}




What are you trying to do here exactly.You are passing an instance of intress class in Model method.And there you are assigning an attribute like $intress->user_id=$user->user_id; FROM WHERE YOU are getting this $user->user_id; ????

If you just want to save a record in the on clicking a link then get a param in actionIntress function in Action in controller class then just a few line of code in your controller file will complete your task.




public function actionIntress($article) 

{

    $intress = new Intress;


    $intress->article_id=$article;

    $intress->user_id=$user->user_id; // Change it according to your requirement

	

    if($intress->save())

    {

         $this->refresh();

    }


}




Please replace this code in your article controller class and also remove a method from model class and let me know it works or not.And why you was returning return $intress; from action in Controller class.

Thank for your help,

i did what you said, just for a test propose i gave a the a value for article_id and user_id . But not working

public function actionIntress($article)

{

&#036;intress = new Intress;





&#036;intress-&gt;article_id='2';


&#036;intress-&gt;user_id='1'; // Change it according to your requirement


    


if(&#036;intress-&gt;save())


{


     &#036;this-&gt;refresh();


}

}

Are you Sure you have a a Model class with name Intress in your project.And if you have then please check your model class again.You need to be sure that a attributes you are assigning a value are a present in your table also and you these are also present in your model class’s rules function.