Yii Framework Forum: How To Insert Data From Web Service/xml/json Into Mysql Db Using Ar Model Class - Yii Framework Forum

Jump to content

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

How To Insert Data From Web Service/xml/json Into Mysql Db Using Ar Model Class How to insert data from web service/xml/json into mysql db using ar Rate Topic: -----

#1 User is offline   Maxx Astral 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 71
  • Joined: 19-September 13
  • Location:Kiev

Posted 23 September 2013 - 11:19 AM

Hi, i'm new to Yii, still learning and loving it a lot. So the thing is that i have to build a product retrieval system, which is based on Amazon web services.

First i have created the necessary tables to hold the information about the products. Then i've created the model class using the awesome Gii. After that i generated the CRUD using Gii again. Now i'm kinda stuck. So Gii provides a form to let the users populate the tables with the necessary info. Now in my system/app i have no need for a form input, for any of the main tables that would hold the product information. The tables should auto populate with data gathered from Amazon API. Only a few tables can have form related to the input fields.

So can anyone please guide me in the right direction, on how would i start implementing the functionality. Should i remove this code from the corresponding view and write the functionality in the Controller class.

<div class="row">
<?php echo $form->labelEx($model,'type_id'); ?>
<?php echo $form->textField($model,'type_id'); ?>
<?php echo $form->error($model,'type_id'); ?>
</div>





Or should i generate a separate view files. Right now i can't seem to find any headway. How to start ? What should be the workflow sequence for a typical application built using Yii. Where would i put the business logic? Of course i know that the business logic should always reside within the Controller class as per the MVC paradigm. But should i write all of the application logic in a single controller class.



I've read about modules and components. But the dilemma i'm facing is that i don't know when is the right time to separate the necessary logic into its respective modules or components.



I'm already following the Web Application Developement with Yii and PHP 2nd Edition, and i admit that its a fantastic book. I've read it two times till now. But i'm getting stuck when i get down to build my projects. Just don't know where to start. My application would not follow a similar flow diagram like the book example.



I would be enormously happy if anyone could provide any sort of guidelines or links that would atleast head me in the right direction.

Thanks,
Maxx
0

#2 User is offline   lagogz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 478
  • Joined: 30-November 12
  • Location:Galiza

Posted 23 September 2013 - 11:56 AM

Hi, Maxx.

In my opinion, you could do this:

- Get Web Service data in your action (into controller).

- Fill your model/s with that data.

- Render appropiate view.

Regards.
-----------------------------------------------------------------------------------------------
------------------------------- VISITEN FOCUCHASART --------------------------------
-----------------------------------------------------------------------------------------------
0

#3 User is offline   Maxx Astral 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 71
  • Joined: 19-September 13
  • Location:Kiev

Posted 23 September 2013 - 12:17 PM

View Postlagogz, on 23 September 2013 - 11:56 AM, said:

Hi, Maxx.

In my opinion, you could do this:

- Get Web Service data in your action (into controller).

- Fill your model/s with that data.

- Render appropiate view.

Regards.

Ok, but should i delete those form input fields which are generated by gii. How will i initiate the data inserting ? Like in normal PHP app i can for example make a separate section in the admin panel where the admin can initiate the data insertion from the web service. Of course i can do this easily using generic PHP functions and HTML/CSS too but what convention should i follow when it comes to Yii. I just want to adhere to the conventions that are set in Yii. I've heard in many places that once you get used to the conventions in Yii your productivity increases hundred folds. So what are the best practices ?

Say::
1) What are the conventions when building an automated/real time system ?
2) How to initialize the specific controller logic sequence
3) How to get the most benefit from the CRUD/Model/Module system already built by Gii ?

And many thanks for your response. I feel really motivated on choosing this wonderful framework.
0

#4 User is offline   lagogz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 478
  • Joined: 30-November 12
  • Location:Galiza

Posted 24 September 2013 - 01:09 AM

Good mornig.

A default actionCreate could be this:

public function actionCreate()
	{
		$model=new ModelName;

		if(isset($_POST['ModelName']))
		{
			$model->attributes=$_POST['ModelName'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

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


Here, model is send to view empty, then fields into view are empty.

A default actionUpdate could be:

public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		if(isset($_POST['ModelName']))
		{
			$model->attributes=$_POST['ModelName'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

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


Here, model is send to view with the data collected by "loadModel", then fields into view are fill with model data.

Then, you could make an action like actionUpdate, but instead of filling data into view with data collected in "loadModel", you could fill model with data collected from your Web Service and send this model to view. So, you'll not have to remove code generated by CRUD.

<div class="row">
<?php echo $form->labelEx($model,'type_id'); ?>
<?php echo $form->textField($model,'type_id'); ?>
<?php echo $form->error($model,'type_id'); ?>
</div>


The above code fills the textField with 'type_id' from the model passed by actionCreate, actionUpdate,...

My english is very poor, I hope to explain properly.

You should read this:

The Definitive Guide to Yii

Conventions

Regards.
-----------------------------------------------------------------------------------------------
------------------------------- VISITEN FOCUCHASART --------------------------------
-----------------------------------------------------------------------------------------------
0

#5 User is offline   Maxx Astral 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 71
  • Joined: 19-September 13
  • Location:Kiev

Posted 24 September 2013 - 09:56 AM

View Postlagogz, on 24 September 2013 - 01:09 AM, said:

Good mornig.

A default actionCreate could be this:

public function actionCreate()
	{
		$model=new ModelName;

		if(isset($_POST['ModelName']))
		{
			$model->attributes=$_POST['ModelName'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

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


Here, model is send to view empty, then fields into view are empty.

A default actionUpdate could be:

public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		if(isset($_POST['ModelName']))
		{
			$model->attributes=$_POST['ModelName'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

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


Here, model is send to view with the data collected by "loadModel", then fields into view are fill with model data.

Then, you could make an action like actionUpdate, but instead of filling data into view with data collected in "loadModel", you could fill model with data collected from your Web Service and send this model to view. So, you'll not have to remove code generated by CRUD.

<div class="row">
<?php echo $form->labelEx($model,'type_id'); ?>
<?php echo $form->textField($model,'type_id'); ?>
<?php echo $form->error($model,'type_id'); ?>
</div>


The above code fills the textField with 'type_id' from the model passed by actionCreate, actionUpdate,...

My english is very poor, I hope to explain properly.

You should read this:

The Definitive Guide to Yii

Conventions

Regards.


Yes that indeed explained a lot of doubts i was having. I now know where to put the necessary functionalities. You explained it properly, no worries. Thanks a lot for this.
0

#6 User is offline   lagogz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 478
  • Joined: 30-November 12
  • Location:Galiza

Posted 24 September 2013 - 03:58 PM

Glad to help.
-----------------------------------------------------------------------------------------------
------------------------------- VISITEN FOCUCHASART --------------------------------
-----------------------------------------------------------------------------------------------
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