Yii Framework Forum: Need Help Understanding Model Relations To Chtml::listdata Correlation - Yii Framework Forum

Jump to content

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

Need Help Understanding Model Relations To Chtml::listdata Correlation Rate Topic: -----

#1 User is offline   Whit 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 24-November 12
  • Location:WI USA

Posted 04 December 2012 - 09:44 PM

First off, I'm new to Yii and walking myself through Jeffrey's book, Web Application Development with Yii and PHP.

I'm working to understand Models relations() and Drop box boxes. Questions are at the end.

Database structure:
  • User table with security_question_id (FK to SecurityQuestion id) and security_question_answer in it
  • SecurityQuestion with question column


User Model
class User extends TrackStarActiveRecord
{
...
    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(
                'issues' => array(self::HAS_MANY, 'Issue', 'requester_id'),
                'issues1' => array(self::HAS_MANY, 'Issue', 'owner_id'),
                'tblProjects' => array(self::MANY_MANY, 'Project', 'tbl_project_user_assignment(user_id, project_id)'),
                'securityQuestions' => array(self::BELONGS_TO, 'SecurityQuestion', 'security_question_id'),
            );
    }

    public function getSecurityQuestionOptions() {
        // WHY DO I NEED THIS? - This works.
        $questions = SecurityQuestion::model()->findAll();
        return is_null( $questions ) ? array() : CHtml::listData( $questions, 'id', 'question' );
        /**
         * Instead of this solution (which doesn't work)
         */
        // return CHtml::listData( $this->securityQuestions, 'id', 'question' );

    }
}


Here is the SecurityQuestion Model
class SecurityQuestion extends CActiveRecord
{
...
	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(
			'users' => array(self::HAS_MANY, 'User', 'security_question_id'),
		);
	}
}


Users Form _form.php:
...
<?php echo $form->dropDownList( $model,'security_question_id', $model->getSecurityQuestionOptions() ); ?>
...


Questions:
  • Why can't I access $this->securityQuestions to get the database rows vs. using model()->findAll()?
    return CHtml::listData( $this->securityQuestions, 'id', 'question' );
    

  • Doesn't $this->securityQuestions reference to the SecurityQuestion model?


Thanks for your help,
Whit
0

#2 User is offline   Whit 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 24-November 12
  • Location:WI USA

Posted 05 December 2012 - 04:57 AM

I get it now.

        $questions = SecurityQuestion::model()->findAll();
        return CHtml::listData( $questions, 'id', 'question' );


is the same as
        return CHtml::listData( $this->securityQuestions->findAll(), 'id', 'question' );


where SecurityQuestion::model() is the same as $this->securityQuestions. I just failed to put findAll() at the end of the second one. Ahha.
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