Starrating Bei Click In Db Speichern

Hallo Zusammen

Ich möchte ein StarRating erstellen welches beim klicken das Rating in die Tabelle Speichert.

Bin absoluter Anfänger und bin mich am einarbeiten. Kann mir jemand einen Tipp geben wieso es mir bei der markierten Codestelle die Action nicht ausführt?

views/MyController/starrating.php


  

 <? $Mrating = New StarRating; ?>


<? $this->widget('CStarRating',array(

        'name'=>'rating',

           'model'=>$Mrating,

           'attribute'=>'rating',

                'callback'=>'

            function(){

                

            $.ajax({

                type: "GET",

                url: "'.Yii::app()->createUrl('MyController/Rating').'"   // <---  PROBLEM <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />??

                success: function(msg){

                    alert("Sucess")

                    },

                error: function(xhr){

                alert("failure"+xhr.readyState+this.url)

                }

            })}'

    )); ?>

Controller/ControllerMyController.php


<?php


class MyControllerController extends Controller

{


public function actionRating()

{

        $model->rating = $_GET['StarRating'];           // <----   Ich geben hier den Formularname an oder?

        $model->save();           

}

}

models/StarRating.php (zur vervollständigung)


<?php


/**

 * This is the model class for table "CStarRating".

 *

 * The followings are the available columns in table 'CStarRating':

 * @property integer $id

 * @property integer $rating

 * @property string $user

 */

class StarRating extends CActiveRecord

{

    /**

     * Returns the static model of the specified AR class.

     * @param string $className active record class name.

     * @return StarRating the static model class

     */

    public static function model($className=__CLASS__)

    {

        return parent::model($className);

    }


    /**

     * @return string the associated database table name

     */

    public function tableName()

    {

        return 'CStarRating';

    }


    /**

     * @return array validation rules for model attributes.

     */

    public function rules()

    {

        // NOTE: you should only define rules for those attributes that

        // will receive user inputs.

        return array(

            array('id, user', 'required'),

            array('id, rating', 'numerical', 'integerOnly'=>true),

            // The following rule is used by search().

            // Please remove those attributes that should not be searched.

            array('id, rating, user', 'safe', 'on'=>'search'),

        );

    }


    /**

     * @return array relational rules.

     */

    public function relations()

    {

        // NOTE: you may need to adjust the relation name and the related

        // class name for the relations automatically generated below.

        return array(

        );

    }


    /**

     * @return array customized attribute labels (name=>label)

     */

    public function attributeLabels()

    {

        return array(

            'id' => 'ID',

            'rating' => 'Rating',

            'user' => 'User',

        );

    }


    /**

     * Retrieves a list of models based on the current search/filter conditions.

     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

     */

    public function search()

    {

        // Warning: Please modify the following code to remove attributes that

        // should not be searched.


        $criteria=new CDbCriteria;


        $criteria->compare('id',$this->id);

        $criteria->compare('rating',$this->rating);

        $criteria->compare('user',$this->user,true);


        return new CActiveDataProvider($this, array(

            'criteria'=>$criteria,

        ));

    }

}

Mhh hast du FierBug ??

Werden da evt in der Konsole JavaScript Fehler ausgegeben ??

Mir ist eben noch etwas auf gefallen.

Undzwar deine Action sollte so aussehen.




public function actionRating()

{

        $model = new StarRating(); 


        if(isset($_GET['StarRating'])) {


           $model->attributes = $_GET['StarRating']; 

           

           if($model->save()) {

           // success

           }else{

           // error

           }

         

        }

}



Ich habe den Controller angepasst. Nun wird die korrekte Javascript Meldung ausgegeben aber der wechsel auf die angegebene URL scheint immer noch nicht zu funktionieren.

Meine Javascript Testmeldung wird auch ausgegeben einfach die Umleitung auf die Action nicht :(




 <? $this->widget('CStarRating',array(

           'name'=>'rating',

           'model'=>$Mrating,

           'attribute'=>'rating',

           'callback'=>'

           function(){

                Alert("test");  //<--- wird ausgegeben

                $.ajax({

                      type: "GET",

                      url: "'.Yii::app()->createUrl('MyController/Rating').'",

                     success: function(msg){

                    alert("Sucess")

                },

               error: function(xhr){

               alert("failure"+xhr.readyState+this.url)

               }})

   }'

 )); ?>






Versuch mal so evt geht das ja.




 <? $this->widget('CStarRating',array(

           'name'=>'rating',

           'model'=>$Mrating,

           'attribute'=>'rating',

           'callback'=>'

           function (){

				$.ajax({

					type: "GET",

					url: "'.Yii::app()->createUrl('MyController/Rating').'",

					success: function(data){

						alert("Sucess")

					},

					error: function(xhr){

						alert("failure"+xhr.readyState+this.url)

					}

				})'

 )); ?>




}



Mit folgendem Data: Statement und dem Insert in dem Controller funktioniert es bestens!

Dank an alle.

View


<? $Mrating = New StarRating; ?>


<?	$this->widget('CStarRating',array(


    'name'=>'ratingAjax',

    'callback'=>'

        function(){

                $.ajax({

                    type: "POST",

                    url: "'.Yii::app()->createUrl('MyController/Rating').'",

                    data: "&rate=" + $(this).val(),

                    success: function(msg){

                                $("#result").html(msg);

                        }})}'

  ));

echo "<br/>";echo "<div id='result'>No Result</div>";

Controller




public function actionRating() {


  $model = new StarRating();

  $model->rating = isset($_POST['rate']) ? $_POST['rate'] : 0;

  echo "You are voting $model->rating through AJAX!";

  $model->insert();


}



Quelle: http://www.yiiplayground.com/index.php?r=UiModule/ui_other/starRating