Yii Framework Forum: Working With Mysql And Mvc Practices - Yii Framework Forum

Jump to content

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

Working With Mysql And Mvc Practices Rate Topic: -----

#1 User is offline   clonevn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 111
  • Joined: 25-March 13

Posted 28 April 2013 - 11:15 PM

Im working with MySQL and form now and have something that I dont understand:
	public function actionCreate()
	{
		$model=new Data;
		if(isset($_POST['Data]))
		{
			$model->attributes=$_POST['Data'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

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

In these codes above, from CRUD generator tool, I have some questions. Can I save to data to database with few attributes? Like the Data model has attributes :a1,a2,a3,a4 and in the form it has input name like: input1, input2, input3, input4.
So can I do like this?:
		if(isset($_POST['Data]))
		{
			$model->a1=$_POST['Data']->input1;
			$model->a2=$_POST['Data']->input2;
                        // dont want to use 2 other input, expect a3 and a4 will be default as NULL
			if($model->save())
				
		}

About the MVC practice, I know that controller used to process data and model and send render data to the view. So if I have a function could be reuse in many actions call analyzeData(), should I put it in the controller class or the model class? Im confused because in the guide it said that model must be very large, store data and process data, controller only retrieve data from GET, POST or somethings like this then process it with model and view.
0

#2 User is offline   vim 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 27-January 11

Posted 29 April 2013 - 12:43 AM

You would want to do it the following way:

$model->a1 = $_POST['Data']['input1'];
$model->a2 = $_POST['Data']['input2'];


As the function name analyzeData() sounds like some business logic you should put it into your model.
0

#3 User is offline   clonevn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 111
  • Joined: 25-March 13

Posted 29 April 2013 - 02:11 AM

Yeah right, this function is about business logic so I will put it into a model class. And thank you for your answer :).
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