Page 1 of 1
Why Do We Write Queries In View
#1
Posted 05 February 2013 - 11:31 PM
Hi in most examples on yii we write the query code for dropdowns in views. Isn't it against MVC. if so then why do we do it.
#2
Posted 06 February 2013 - 12:21 AM
I think it is demonstrated that way because it is easier to follow the example when you aren't using the controller to run model methods and then pass data to the view.
I confess to using queries in views also but generally only for simple dropdown selects and things.
I confess to using queries in views also but generally only for simple dropdown selects and things.
#3
Posted 06 February 2013 - 02:16 AM
Could you give me an example showing this approach? I don't remember any tutorial using queries in views. However, I almost always create custom methods like this in my models that are then used in my views in order to keep things clean
public function getFancyDropdownOptions()
{
return CHtml::listData(MyModel::model()->findAll(),'id','name');
}
codecrumbs.at
My extensions:
ActiveResource for Yii - the RESTful equivalent to ActiveRecord | Neo4Yii - wrapper for the Neo4j graph database | EPhpThumb - a simple, lightweight wrapper for the phpThumb library
Get social:
Circle me on Google Plus
Follow me on Twitter
My extensions:
ActiveResource for Yii - the RESTful equivalent to ActiveRecord | Neo4Yii - wrapper for the Neo4j graph database | EPhpThumb - a simple, lightweight wrapper for the phpThumb library
Get social:
Circle me on Google Plus
Follow me on Twitter
#4
Posted 06 February 2013 - 07:19 AM
awebdeveloper, on 05 February 2013 - 11:31 PM, said:
Hi in most examples on yii we write the query code for dropdowns in views. Isn't it against MVC. if so then why do we do it.
It can be done, but with moderation.
#5
Posted 06 February 2013 - 08:15 AM
Like Haensel, I generally create a static method in the relevant model to return options for drop down lists, following much the same naming convention. A simple example might be:
public static function getCompanyOptions()
{
return CHtml::listData(self::model()->findAll(), 'Id', 'Name');
}
#6
Posted 06 February 2013 - 01:16 PM
Keith, on 06 February 2013 - 08:15 AM, said:
Like Haensel, I generally create a static method in the relevant model to return options for drop down lists, following much the same naming convention. A simple example might be:
public static function getCompanyOptions()
{
return CHtml::listData(self::model()->findAll(), 'Id', 'Name');
}
i too do the same. call a dropdown method. But why static. what are the advantages
#7
Posted 06 February 2013 - 02:46 PM
Because then I can get the valid options without needing to create an instance of the class. For instance, if I was generating a drop down list to attach a User to a Company, I don't have an existing instance of Company, so a static method is more appropriate.
Also, it makes more sense that the class itself generates a list of all companies rather than a specific Company instance; the individual instance doesn't need to know about all of the other companies, it only cares about its own attributes.
It's down to personal preference I suppose, but I find the static method to be more appropriate from a code perspective.
Also, it makes more sense that the class itself generates a list of all companies rather than a specific Company instance; the individual instance doesn't need to know about all of the other companies, it only cares about its own attributes.
It's down to personal preference I suppose, but I find the static method to be more appropriate from a code perspective.
#9
Posted 06 February 2013 - 05:02 PM
The model() method does return a default instance, but again, I feel that the method belongs with the class rather than the object. Both ways will work, so choose whichever suits you.
Share this topic:
Page 1 of 1

Help












