Question on ActiveRecord when creating new record

I has a question on ActiveRecord when creating new record in Yii.

From the crud generated, in the PostController,




public function actionCreate()

{

    $post=new Post;

    if(isset($_POST['Post']))

    {

        $post->attributes=$_POST['Post'];

        if(isset($_POST['previewPost']))

            $post->validate();

        else if(isset($_POST['submitPost']) && $post->save())

            $this->redirect(array('show','id'=>$post->id));

    }

    $this->render('create',array('post'=>$post));

}



At the very first line of this function, user is just start to create this post, but not yet save. Yii already issue


 $post=new Post; 

From the documentation, this means there already a record added into the "Post" table.

In the case of user cancel to create this post (at the html form page), even though the record is not saved into the database table, but an auto-increment value in the table "Post" has been "wasted" (can check from the MySql table next auto increment value for this table)

Let said I want to allow user to create a new post with 1 comments together in a single page, the above function will be modified to:




public function actionCreate()

{

    $post=new Post;

    $comment=new Comment;


    if(isset($_POST['Post']))

    ....

    ....

}



Which mean if user suddenly decided do not create this post, and just leave/back to previous page, then there will be total 2 auto-increment values inside table "Post" and "Comment" wasted. Is it the way to use the ActiveRecord?

Can some Yii expert advice on this?

Thanks!

This is false, new Post does not make any modifications to the database unless you make it so with customized code in Post::init() or some other place. It is only on methods such as CActiveRecord::save(), CActiveRecord::update() etc that causes the active record to be saved/updated.

The reason you want to set $post=new Post even before the form is submitted, is that the $post object will contain information the form needs such as labels, etc.

If I would like to let user submit a new Post with Comment (an example) together in a single POST, this is the way?




    $post=new Post;

    $comment=new Comment;



and pass both models to render() inside the controller actionCreate.

This is the usual way? Sorry for my look stupid question, I just want to confirm, and try to understand Yii’s behavior to utilize it as much as possible…

Thanks!

It’s a valid question. Your probably best off reading this:

http://www.yiiframework.com/doc/cookbook/19/

Thanks! I am very clear now!

According to the cook book, what if I want to pass an array of model B, let said I allow user to add multiple comments inside a single new post (which is not so logic, but it is require in my apps).

Just want to ask you an extended question, which is the problem I facing now… I passed array of model B to view, and the view has been generate all the model of B, but I view html source, they are all the same form item name

[html]

<input name="B[FIELD_A]" id="B_FIELD_A" type="text" value="0" />


<input name="B[FIELD_A]" id="B_FIELD_A" type="text" value="0" />


<input name="B[FIELD_A]" id="B_FIELD_A" type="text" value="0" />


..


..

[/html]

in my view.php source




    foreach($arrB as $<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' /> {

        echo CHtml::activeTextField($B, 'FIELD_A', array('size'=>8,'maxlength'=><img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' />);

    }



can you please advice where I my problem?

Thanks!

This is tabular input. Read this:

http://www.yiiframework.com/doc/guide/form.table

But the above page refers to updates. It’s similar anyways. Let me know if you need more help

Thanks! This is the exactly what my problem is. Sorry for not reading through enough the guide.

Thank you for your kind smart reply, it just help!