Solved

Understanding how Yii PHP Framework works Am I right?

Posted on 2011-09-06
5
1,310 Views
Last Modified: 2013-11-10
I recently have been trying to learn Yii and PHP together.
I have hit a road block in my understanding. I see it works on the Trackstar example code from
"Agile Web Application Development with Yii 1.1 and PHP5"

I can not understand how an action like
<form id="project-form" action="/trackstar/index.php?r=project/create" method="post">
      <p class="note">Fields with <span class="required">*</span> are required.</p>
 as the HTML rendered looks like the above

"/trackstar/index.php?r=project/create
triggers a record creation. Could someone explain the program flow?
project is the projectController.php  and create is the view involved but how does the actual
SQL command  Insert   .. $model data ... get executed?
Is the create view essential for the name? Do I have to override whatever other view name I use? Someone help  me out here????
Does this happen via get and Post?? e.g after post then the data is saved
via the if(isset($_POST['Project']))
May be I am right here has it been there the whole time.
 
1. call up url from menu or type into browser:  to render the form as a get
e.g http://localhost/trackstar/index.php?r=project/create
press the submit button and go to the same URL but as a  post -
call save funcion code within the parent of the model, CActivateRecord

Here is the actionCreate function:
      public function actionCreate()
      {
            $model=new Project;

            // Uncomment the following line if AJAX validation is needed
            // $this->performAjaxValidation($model);

            $fp0=fopen("c:\\tmp\\debug1.txt","a");
            fwrite($fp0," Got 2 start of actionCreate");
            fclose($fp0);

            if(isset($_POST['Project']))
            {
                                          $fp1=fopen("c:\\tmp\\debug1.txt","a");
            fwrite($fp1," We Got POST SO WE ARE INSERTING DATA");
            fclose($fp1);
                  $model->attributes=$_POST['Project'];
                  if($model->save())
                        $this->redirect(array('view','id'=>$model->id));
            }

            $this->render('create',array( 'model'=>$model,));
            $fp=fopen("c:\\tmp\\debug1.txt","a");
            fwrite($fp," Got here to end");
            fclose($fp);
      }
      

In fact it makes sense that the parent class CActiveRecord of the model here contains the
save function which actually saves the data base record.
Can anyone comment on this finding? Maybe this will help others like myself
0
Comment
Question by:Robert Silver
  • 3
  • 2
5 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36495516
Not sure exactly where to start, and you might want to break this line of inquiry down into several questions, since it seems to be all over the Yii and PHP map.  Perhaps the best response is to provide you some learning resources.

You can make a Google search for "learn Yii Framework" -- I did and found many apparently good learning resources.
http://www.yiiframework.com/doc/blog/1.1/en/start.overview
http://www.yiiframework.com/tutorials/
http://www.yiiframework.com/doc/guide/

If you're not rock-solid in your PHP skills, you might want to learn the basics of PHP through the well-explained examples in these two books.
http://www.sitepoint.com/books/phpmysql4/
http://www.amazon.com/PHP-5-Practice-Elliott-White/dp/0672328887

Frameworks are typically object-oriented, and Yii is no different, so you will need to understand object-oriented PHP programming.  This page and all the linked pages will give you something of a flavor for that.
http://php.net/manual/en/language.oop5.php

And this book will help add some foundation to your understanding of OOP.
http://www.amazon.com/PHP-5-Objects-Patterns-Practice/dp/1590593804

Some additional interesting information is available here:
http://en.wikipedia.org/wiki/Comparison_of_Web_application_frameworks#PHP
http://www.php-developer.org/most-used-php-framework-the-popular-top-7-list-in-year-2011/


0
 
LVL 2

Author Comment

by:Robert Silver
ID: 36501981
While your reply is somewhat helpful I was looking for confirmation of my understanding whcih recent progress has vindicated.
 Today I  have more questions. Since no else has come forward and actually
validated statements I know to be true I will sum it up as this.
My understanding was dead on.  You get the  view or form and post the results typically in the yii framework standard.   You pass a model and run your view from the controller like most MVC frameworks.  The post of the form data back to the controller is handled through the action url of the form  View used to populate typically a database.

Proof of work helps! For all those other people interested.'
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36503276
Sorry, I did not understand that the question was about post of the form data back to the controller is handled through the action url of the form.  Please let me add this link to the collected learning resources above.
http://us3.php.net/manual/en/tutorial.forms.php

HTH, ~Ray
0
 
LVL 2

Author Comment

by:Robert Silver
ID: 36506919
As I said before. I assumption was in fact correct The demo TrackStar application in fact uses the
get method displaying a form or view page and  Post to process the results of said view page.
I think it will be helpful for others struggling to understand Yii Framework and how it gets implemented in a classic fashion e.g Trackstar demo.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 36509978
get method displaying a form or view page and post to process the results of said view page

That is, indeed, "a classic fashion."  In fact it is a little bit more than classic.  If you make a Google search for "get vs post" you will find many results, and among them this very good little article.
http://www.cs.tut.fi/~jkorpela/forms/methods.html

In a nutshell, GET method requests use the URL to carry the request arguments.  You can send a link to a friend if the page can be retrieved with a GET request (very handy) like this:
http://lmgtfy.com?q=GET+vs+POST

GET requests must not modify the underlying data model -- they are just for displaying information.  As the article points out, GET should be used if and only if there are no side effects to the retrieval of data.

POST requests use the request body (a raw post string of arguments) to carry the content of the request.  You can use POST requests when you want to add, update or remove information from the underlying data model.

It's all part of the HTTP specs.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now