Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
josh

Returning Value Name

Recommended Posts

Greetings,

 

Thank you in advance for your help. In the blog regarding handling related models in Yii it is stated:

 

"And that’s all you need to do to get this to work. The dropDownList() method creates a SELECT menu. It’s associated with the departmentId attribute of this Model. The drop-down menu’s data has to come from CHtml::listData(). That part of the code says to fetch every Department, and to use the department’s id value for the menu value and its name value for the visible label."

 

This works perfectly! Thank you. My question is when viewing, managing or updating the record I would like the value to show the name and not number associated with the entry.

 

pic.png

 

I have tried modifying my my model regarding the

public function relations()

{

return array(

);

}

 

But to no avail. Please help. Thank you.

Share this post


Link to post
Share on other sites

Thanks for the nice words. First, you do need to have the proper relations identified in the Model. But then, it's just a matter of referring to $model->relation_name->property, which is probably $model->department->name in this case (this reference is in the View).

Share this post


Link to post
Share on other sites

Thank you Mr Ullman for your quick response. This should be simple and I apologize for my ignorance in this situation but I am simply not seeing my relation.

 

MediaArchive is my main form. BibleDetails is a lookup table that is populating the dropdown list for the Bible field on the MediaArchive form.

 

So in my MediaArchive form I established the following relation

public function relations()

{

return array(

'Bible' => array(self::HAS_ONE, 'BibleDetails','BibleEnglish')

);

}

 

 

In BibleDetails I added this to my relations

public function relations()

{

return array(

'BibleEnglish' => array(self::BELONGS_TO, 'MediaArchive','Bible')

);

}

 

and then in my _view.php for my MediaArchive form I inserted

<?php echo CHtml::encode($model->Bible->BibleEnglish); ?>

 

 

OK, after all the chuckles :) could you please advise. I am not understanding relations. Thank you!

Share this post


Link to post
Share on other sites

You're welcome. I'm not sure I understand what the actual relationship is between MediaArchive (is that a Model, by the way?) and BibleDetails. Also, I suspect your View file should have $model->Bible->someBibleModelProperty.

Share this post


Link to post
Share on other sites

You're welcome. I'm not sure I understand what the actual relationship is between MediaArchive (is that a Model, by the way?) and BibleDetails. Also, I suspect your View file should have $model->Bible->someBibleModelProperty.

 

Thank you! I might not be the fastest but my perseverance paid off. I took another look at my database and fixed my FKs and now everything works. Thank you.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...