Solved

Understanding how Yii PHP Framework works Am I right?

Posted on 2011-09-06
5
1,350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 110

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 110

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 110

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

724 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