How To Fill In Object Returned By Factory?

I need to generate a form (witch gathering from some views and models). User fill in this form and data save to DB. I have a few forms in my module, they have some equal fields and some unique. Some data puts once, they a fixed, for example user name, email, phone. And some data fill in dynamically, for example, goods. User can add as many goods, as he wish.

At first time creating form, to make filing easier, some fields fill with default data. For example user name, phone, email, also some dynamically fields, etc.

So, I can’t determine where define default data for forms? Where fill in this data to my object which I pass to form view? It’s would really helpful for me, a detailed explanation of implement. Thanks in advance.

class DocController {

  public function actionCreate( $type ){

    $factory = new DocFactory();

    $doc = $factory->createDoc( $type );

    if ( $doc === null ) {

      throw new CHttpException(404,'Page doesn\'t exists');



    if ( isset( $_POST['somedata'] ) ) {

      // save form data

    } else {

      // something else, for example fill in object with defaut data


    $this->render( $doc->getView('create'), array(

      // form values takes from $doc

      'doc' => $doc




class DocFactory {

    protected $contract_profitable;

    public function createDoc( $type ){

        switch( $type ){

            case 'contract_profitable':

                return $this->getContractProfitable();



        return null;


    public function getContractProfitable(){

        if ( $this->contract_profitable === null ) {

            $this->contract_profitable = new DocContractProfitable();


        return $this->contract_profitable;



interface IDoc {

  public function getView( $view );


class DocContractProfitable implements IDoc {

   public function getView( $view ){

       return $view;



I’m really confused with what your code is doing:

  • In you actionCreate() you want to save data to a DB, but have not made an instance to a model.

  • Does the ‘username’, ‘phone’, ‘email’, etc actually stored in the Goods table or just a user_id to reference that info?

  • I really don’t understand the reason for the DocFactory class.

If you are just saving the user info for the current user, just save Yii::app()->user->id into the user_id of the goods model.

If you are actually saving the info in the goods model, then you need to load the user model given the user’s id, then assign the needed values before $model->save().

Of course, I don’t save directly, I’m using a model. It was a part of question about realization.

That does not matter.

I need dynamicaly load models independent on user choise with preloaded data. So I’m using a simple factory.

P.S. By the way I suggest my problem with scenarios and inheritance of models, using fabric to produce necessary objects.