Yii Framework Forum: Active Record and Forms? - Yii Framework Forum

Jump to content

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

Active Record and Forms? Rate Topic: -----

#1 User is offline   Ryan Zec 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 15-January 10

Posted 16 January 2010 - 08:56 AM

As I am going through my evaluation of this framework, I have a question about active records and forms. Now I am just trying to build a simple blog and I have an active record object called BlogPost. Now it seems like you have 2 different types of models, Active Record and Form. I assume that it would make more sense to use the Form model when I need to process a form, correct? Now for my main question, is it possible to somehow automatically link a form model to an active record model so I could create the form model, populate its data, validate it, and then if it passes validation, save that form model to a database table that is linked my active record model?

something like this:
$model = new BlogPostForm();
if(isset($_POST['BlogPostForm']))
{
    $model->attributes=$_POST['BlogPostForm'];
    if($model->validate())
    {
        BlogPost::saveFromFormModel($model);
    }
}

0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 16 January 2010 - 09:23 AM

You can either use a FormModel or an ActiveRecord for your form. Former is used in situations where there's no DB involved (see the contact form from the base webapp). So in your case: Simply use your ActiveRecord as model for the form.
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