Yii Framework Forum: save HAS_MANY - Yii Framework Forum

Jump to content

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

save HAS_MANY How to save HAS_MANY when you aren't creating the related items Rate Topic: -----

#1 User is offline   Ferriol 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 28-August 11

Posted 22 September 2011 - 08:37 AM

Hi,

I have trouble saving related models of table, maybe I'm confused about HAS_MANY and BELONGS_TO relations.

I have table Pagaments (A) that is related with FacturesPropies(B), FacturesProveidors( C ) and Segsocial(D) with HAS_MANY

B,C and D are related with A with BELONG_TO, all three tables has got A_id foreign key.

Those relations aren't like invoice, line_invoice. It works like this: In A form the user can choose some B, C or D items to related with A item. But always B,C or D items exist before the user creates A item.

I'm having troubles to find some extension to save this relation between A and B,C and D
esaverelatedbehavior and CSaveRelationsBehavior seems that is thought for has_many relation that when you create A also create B or C or D.

Do you know what extension can help me ?

Maybe I don't understand how use this extensions ?

Maybe I don't understand the relations and this is not a HAS_MANY relation ?

This is the model pagaments the A that I'm talking. B, C and D are FacturesPropies, FacturesProveidors, Segsocial

I have a form with 3 CGridView with a CCheckBoxColumn that sends ids of related items. Then I receive an array of ids

This code is generated with gii and then I've changed parts, I omitted code to make text easier to read

<?php


class Pagaments extends CActiveRecord
{

	public $comissio = 0.1;

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('data, soci', 'required'),
			array('soci', 'numerical', 'integerOnly'=>true),
			array('facturesPropiesImport, facturesProveidorsImport, segsocialsImport, comissioImport', 'numerical'),
			array('descripcio', 'length', 'max'=>255),
			array('data', 'date','format'=>Yii::app()->locale->getDateFormat('medium')),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('pid, data, facturesPropiesImport, facturesProveidorsImport, segsocialsImport, comissioImport, soci, descripcio', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'facturesPropies' => array(self::HAS_MANY, 'FacturesPropies', 'pagament'),
			'facturesProveidors' => array(self::HAS_MANY, 'FacturesProveidors', 'pagament'),
			'soci0' => array(self::BELONGS_TO, 'Socis', 'soci'),
			'segsocials' => array(self::HAS_MANY, 'Segsocial', 'pagament'),
		);
	}


	public function behaviors()
	{
	    return array(
		'datetimeI18NBehavior' => array('class' => 'ext.DateTimeI18NBehavior'),
		'CAdvancedArBehavior' => array('class' => 'ext.CAdvancedArBehavior'),
	    ); 
	}
}


And this the controller

<?php

class PagamentsController extends Controller
{

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

		if(isset($_POST['Pagaments']))
		{
			$model->attributes = $_POST['Pagaments'];
			if (isset($_POST['facturesPropies'])) $model->facturesPropies = $_POST['facturesPropies'];
			if (isset($_POST['facturesProveidors'])) $model->facturesProveidors = $_POST['facturesProveidors'];
			if (isset($_POST['segsocials'])) $model->segsocials = $_POST['segsocials'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->pid));
		} else $model->data = Yii::app()->dateFormatter->format(Yii::app()->locale->getDateFormat('medium'),time());

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

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

		if(isset($_POST['Pagaments']))
		{
			$model->attributes=$_POST['Pagaments'];
			$model->facturesPropies = isset($_POST['facturesPropies']) ? $_POST['facturesPropies'] : array();
			$model->facturesProveidors = isset($_POST['facturesProveidors']) ? $_POST['facturesProveidors'] : array();
			$model->segsocials = isset($_POST['segsocials']) ? $_POST['segsocials'] : array();
			if($model->save())
				$this->redirect(array('view','id'=>$model->pid));
		}

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

}

0

#2 User is offline   hofrob 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 22-July 10
  • Location:Vienna

Posted 22 September 2011 - 11:56 AM

Guidelines for posting this forum

Quote

Include a piece of code snippet reproducing the problem. Try to make the code snippet as simple as possible to isolate the problem. People may simply be scared away at seeing hundreds of lines of code.


Maybe I'm not understanding the question completely but I think you just have to fill A_id in your related models with the id from the model A the user chooses and save B/C/D.
0

#3 User is offline   Ferriol 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 28-August 11

Posted 22 September 2011 - 03:19 PM

View Posthofrob, on 22 September 2011 - 11:56 AM, said:


Sorry, I edited the post and is much clearer

View Posthofrob, on 22 September 2011 - 11:56 AM, said:

Maybe I'm not understanding the question completely but I think you just have to fill A_id in your related models with the id from the model A the user chooses and save B/C/D.


I'm agree with you, but I don't know how to do with this extensions. I can't do executing update statements but I thought that Yii has a more elegant way, isn't it ?
0

#4 User is offline   hofrob 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 22-July 10
  • Location:Vienna

Posted 24 September 2011 - 05:20 AM

View PostFerriol, on 22 September 2011 - 03:19 PM, said:

I'm agree with you, but I don't know how to do with this extensions. I can't do executing update statements but I thought that Yii has a more elegant way, isn't it ?


Do you know how to get data from a database in Yii? And how you change specific attributes of the data? Because that is what you need to do.

Get the data. Change the foreign key (e.g.: A_id) and save it.
0

#5 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 545
  • Joined: 09-April 10

Posted 24 September 2011 - 05:49 AM

Not sure what your DB table looks like but you need to pass the ID field to the HAS_MANY relation. The rest of your code looks ok.

'facturesPropies' => array(self::HAS_MANY, 'FacturesPropies', 'pagament'),

// Maybe should be
'facturesPropies' => array(self::HAS_MANY, 'FacturesPropies', 'pid'),


Cheers,

Matt
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