chiliec / yii2-vote

Provides voting for any model :+1: :-1:

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Relation model?

Pablossoo opened this issue · comments

Hi, how should look relation model? e.g I have table post. What I need that it working?

Hi @Pablossoo and thank you for your interest in this module!

You need a mysql database (not tested in other databases). Table should have a primary key (PK). And model for this table, for example Post.

In module settings you can to set like this:

'models' => [
    123 => \common\models\Post::className()
],

where key 123 - id for link model in module auxiliary tables, value - path to class name definition (this needs for reduce table size - we stored only integer key).

If you have problems with integration module, feel free to ask any questions!

Ok, let me explain my problem more precisely.

My entity model

class Adresssite extends \yii\db\ActiveRecord
{

public static function tableName()
 {
   return 'adresssite';
 }

  public function rules()
   {
      return [
         [['adress'], 'string', 'max' => 255],
         [['adress'], 'unique'],
      ];
 }


  public function attributeLabels()
    {
       return [
           'id_adress' => 'Id Adress',
           'adress' => 'Adress',
       ];
      }
 }

Now if I set all config like as tutorial, but I use a basic app yii2- it's any problem?

 'bootstrap' => ['log',
      'chiliec\vote\components\VoteBootstrap',
  ],
 'vote' => [
        'class' => 'chiliec\vote\Module',
        // show messages in popover
        'popOverEnabled' => true,
        // global values for all models
        // 'allowGuests' => true,
        // 'allowChangeVote' => true,
        'models' => [
            // example declaration of models
                 //   app\models\Adresssite::className(),   if unncoment  gets Uncaught Error: Class 'app\models\Adresssite' not found in... 
            // 'backend\models\Post',
            // 2 => 'frontend\models\Story',
            // 3 => [
            //     'modelName' => \backend\models\Mail::className(),
            //     you can rewrite global values for specific model
            //     'allowGuests' => false,
            //     'allowChangeVote' => false,
            // ],
        ],

        ],

in next step I put widget to view

 echo \chiliec\vote\widgets\Vote::widget([
    'model' => $data,
    // optional fields
    // 'showAggregateRating' => true,
 ]);

but if I try click voteup or vote down I get error

 SyntaxError: expected expression, got ','

What is differance beetwen model agregate and agreagate_rating?

I use a basic app yii2- it's any problem?

It's not a problem. I think your model not in app\models\. Before class declaration you watch line like namespace app\models; ?

but if I try click voteup or vote down I get error
SyntaxError: expected expression, got ','

uncomment this line too:

screenshot 2016-12-01 17 01 14

What is differance beetwen model agregate and agreagate_rating?

aggregate contains all votes, aggregate_rating contains only aggregate data for every id (this needs for improving performance).

Still not working but...
I can;t use app\models\Adresssite::className(), only class, now i doesn't get an error (class not found). My entity has namespace app\models.
But still show error SyntaxError: expected expression, got ','

my new config

        'vote' => [
        'class' => 'chiliec\vote\Module',
        // show messages in popover
        'popOverEnabled' => true,
        // global values for all models
        // 'allowGuests' => true,
        // 'allowChangeVote' => true,
        'models' => [
            app\models\Adresssite::class,
        ],
    ],

Ok I think find error. In console at onclick function
onclick="vote(0, , 'like');
onclick="vote(0, , 'dislike');

How it fix?

::class not the same to ::className(). Hmm, try to change config like this:

'models' => [
    ['modelName' => 'app\models\Adresssite'],
],

Ok, it's resolve this problem. Now what is second problem with onclick?

Are you sure that your model have Primary key? Because you didn't have target id (which is primaryKey()[0])

var_dump($this->primaryKey());

Can you exec this code in model and write result here?

Ya, I created new method with this code and I called in view.

$model->myAction();

result;

array(1) { [0]=> string(9) "id_adress" }

Fine! Now try exec:

var_dump($this::className());

string(21) "app\models\Adresssite"

Great! In module config written this string?

'modules' => [

    'vote' => [
        'class' => 'chiliec\vote\Module',
        // show messages in popover
        'popOverEnabled' => true,
         'allowGuests' => true,
         'allowChangeVote' => true,
        'models' => [

            ['modelName' => 'app\models\Adresssite'],
        ],
    ],

....
]

className() still not working.

Very strange, it should work :(
Is your project located in somewhere in open source?

OK.
so it's application;
https://github.com/Pablossoo/basic
Controller it siteController , action actionShowCommentSite($id) (she render view)
view/site/site.php

config you have in config folder.

if you want I can sent my database sql file and maybe you try reproduce my issue.

Sure, put your sql file in this repository.

Closed because no response for a long time.