Yii Framework Forum: Getposted Function - Yii Framework Forum

Jump to content

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

Getposted Function

#1 User is offline   gbasto 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 70
  • Joined: 28-September 10
  • Location:Portugal

Posted 08 March 2013 - 10:45 AM

Hi,

Today we have the following code:

$model=new ContactForm;
if(isset($_POST['ContactForm']))
{
    $model->attributes=$_POST['ContactForm'];
    (...)
}



I suggest add the getPosted() function to CModel, so you can turn the previous code into this:

$model=new ContactForm;
if($model->posted)
{
    $model->attributes=$model->posted;
    (...)
}



Advantages:
- More code reusing
- We can change the class name and the controllers still work without any other change

I've been using this in my projects, and it is a tiny feature that actually helps. Here it is my implementation:

public function getPosted() {
    $name = get_called_class();
    if (isset($_POST[$name])) {
        return $_POST[$name];
    } else {
        return false;
    }
}

1

#2 User is offline   sensorario 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,987
  • Joined: 07-September 10
  • Location:Cesena (Italy)

Posted 08 March 2013 - 12:23 PM

Or ...

$model=new MyModel;
if($this->isPostRequest())
{
    $model->bindData();
    (...)
}

0

#3 User is offline   Ben 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 276
  • Joined: 15-March 09

Posted 08 March 2013 - 06:50 PM

I don't think models should contain request related stuff.
Don't like ads in my sig...
3

#4 User is offline   yJeroen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 06-September 11
  • Location:The Netherlands

Posted 11 March 2013 - 01:25 PM

This would fit better:
public function getRequest{
    return Yii::app()->getRequest();
}
public function actionIndex()
    $model=new MyModel;
    if($this->request->isPostRequest) {
        $model->attributes = $this->request->getPost($model); //change getPost() so it accepts CModel as a parameter as well
        (...)
    }
}


;)
0

#5 User is offline   gbasto 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 70
  • Joined: 28-September 10
  • Location:Portugal

Posted 11 March 2013 - 10:30 PM

View PostyJeroen, on 11 March 2013 - 01:25 PM, said:

This would fit better:
public function getRequest{
    return Yii::app()->getRequest();
}
public function actionIndex()
    $model=new MyModel;
    if($this->request->isPostRequest) {
        $model->attributes = $this->request->getPost($model); //change getPost() so it accepts CModel as a parameter as well
        (...)
    }
}


;)


Seems fine to me too :)
0

#6 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,005
  • Joined: 17-January 09
  • Location:Russia

Posted 12 March 2013 - 05:00 AM

That's how it's now ;)
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#7 User is offline   Jaggi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 05-September 11

Posted 14 March 2013 - 10:45 AM

I prefer using CForm Builder and doing:

if ( ( $form->submitted('submit') )
{

}


this automatically sets the post attributes to the model too.
See my development site @ www.CodeTheInter.net (BETA)

Posted Image Posted Image

Quote

If you make it idiot proof, they'll build a better idiot
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