Yii Framework Forum: Why Do We Write Queries In View - Yii Framework Forum

Jump to content

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

Why Do We Write Queries In View Rate Topic: -----

#1 User is offline   awebdeveloper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 55
  • Joined: 29-September 12

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.
0

#2 User is offline   waitforit 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 401
  • Joined: 09-February 11

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.
0

#3 User is offline   Haensel 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 444
  • Joined: 14-January 11
  • Location:Vienna (Austria)

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');
}

0

#4 User is offline   fouss 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 392
  • Joined: 05-October 10
  • Location:Bamako Mali

Posted 06 February 2013 - 07:19 AM

View Postawebdeveloper, 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.
Posted Image
0

#5 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,631
  • Joined: 04-March 10
  • Location:UK

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');
    }

0

#6 User is offline   awebdeveloper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 55
  • Joined: 29-September 12

Posted 06 February 2013 - 01:16 PM

View PostKeith, 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
0

#7 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,631
  • Joined: 04-March 10
  • Location:UK

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.
0

#8 User is offline   awebdeveloper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 55
  • Joined: 29-September 12

Posted 06 February 2013 - 03:30 PM

View PostKeith, on 06 February 2013 - 02:46 PM, said:

Because then I can get the valid options without needing to create an instance of the class.


B) cool that's a good suggestion. But i currently use it as Product::model()->dropdown() and i don't create a instance of it. Is it the same
0

#9 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,631
  • Joined: 04-March 10
  • Location:UK

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. :)
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