RAR problem

Hello everyone!

(Sorry for bad english)

I’m coding a poll-system now, and I have a question.

There are three tables which have relations.

Here they are:

The table contains variants of answers to the poll.


CREATE TABLE IF NOT EXISTS `tbl_poll_answers` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `vote_main_id` int(11) NOT NULL,

  `title` varchar(150) NOT NULL,

  `position` int(3) NOT NULL DEFAULT '0',

  `chosen` int(6) NOT NULL DEFAULT '0',

  `points` int(10) NOT NULL DEFAULT '0',

  PRIMARY KEY (`id`)

) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;

The table contains the polls itselfes, e.g. title means the question of the poll




CREATE TABLE IF NOT EXISTS `tbl_poll_main` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `title` varchar(300) NOT NULL,

  `starts` datetime NOT NULL,

  `expires` datetime DEFAULT NULL,

  `bypoints` tinyint(1) NOT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;

And this table contains the votes for answers of a poll.




CREATE TABLE IF NOT EXISTS `tbl_poll_votes` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `answer_id` int(11) NOT NULL,

  `user_id` int(11) NOT NULL,

  `time` datetime NOT NULL,

  `vote_points` int(2) unsigned DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;



The relations are simple:

PollVotes model:


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		return array(

			'pollAnswer'=>array(self::BELONGS_TO, 'PollAnswers', 'answer_id'),

		);

	}

PollMain model:


	public function relations()

	{

		return array(

			'answers'=>  array(self::HAS_MANY, 'PollAnswers', 'poll_main_id', 'order'=>'answers.position DESC'),

			'answersCount' => array(self::STAT, 'PollAnswers', 'poll_main_id'),


		);

	}

PollAnswers model:


	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(

			'pollMain'=>array(self::BELONGS_TO, 'PollMain', 'poll_main_id'),

			'votes'=>array(self::HAS_MANY, 'PollAnswers', 'answer_id'),

		);

	}

So, at View we get such form, which sends over AJAX the answer id to the same controller and the same action (PollMain Controller and "view" action).

That’s it:

Now the time to controller:




	public function actionView($id)

	{

		$model = $this->loadModel($id);

		

		 if(Yii::app()->request->isAjaxRequest){

                     if($answer_id = $_POST['answer_id']) {


                               // HERE SHOULD BE THE NEEDED CODE (SEE BELOW)

                               

                               // show vote results via AJAX

			       $this->renderPartial('_pollResult', array('model'=>$model));

			     

		      }

                      Yii::app()->end();

		} else {

			$this->render('view',array(

				'model'=>$model,

			));

		}

		

		

	}

So, when we’ve voted - we should see the poll results which are loaded over AJAX instead of voting form.

But we should check that user haven’t voted for this poll yet.

"HERE SHOULD BE THE NEEDED CODE" should be something like


$has_voted = $model->answers(array('condition'=>'id='.$answer_id))[0]->votes(array('condition'=>'user_id=HERE IS CURRENT USERID'));

Please, help me to build this query!




$record = PollVotes::model()->find('answer_id = :answer_id and user_id =:user_id',array(':answer_id'=>$answer_id,':user_id'=>$user_id));

$has_vote = ($record!=null)?true:false;