Yii Framework Forum: Starrating Bei Click In Db Speichern - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Starrating Bei Click In Db Speichern Rate Topic: -----

#1 User is offline   beatcode 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 57
  • Joined: 13-May 11
  • Location:Zürich

Posted 12 December 2012 - 04:40 AM

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 ?????
                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,
        ));
    }
}

Attached File(s)


0

#2 User is offline   julian90 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 28-February 11

Posted 12 December 2012 - 04:58 AM

Mhh hast du FierBug ??

Werden da evt in der Konsole JavaScript Fehler ausgegeben ??
0

#3 User is offline   julian90 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 28-February 11

Posted 12 December 2012 - 05:18 AM

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
           }
         
        }
}

1

#4 User is offline   beatcode 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 57
  • Joined: 13-May 11
  • Location:Zürich

Posted 12 December 2012 - 07:01 AM

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)
               }})
   }'
 )); ?>



0

#5 User is offline   julian90 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 28-February 11

Posted 12 December 2012 - 08:12 AM

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)
					}
				})'
 )); ?>


}

0

#6 User is offline   beatcode 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 57
  • Joined: 13-May 11
  • Location:Zürich

Posted 12 December 2012 - 02:48 PM

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.yiiplaygr...ther/starRating
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users