Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

Editing multiple tables in one view 28 Mar 2012 15:12 #1761

How to retrieve and update data from multiple database tables in one page of the view?

Please Log in or Create an account to join the conversation.

Re: Editing multiple tables in one view 28 Mar 2012 17:16 #1771

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 986
Joomla is designed to update datas only one table form at once.

If you want to do this, the best is maybe AJAX.
Coding is now a piece of cake

Please Log in or Create an account to join the conversation.

Re: Editing multiple tables in one view 05 Apr 2012 06:05 #1882

It is possible, just prepare your form and controller.

In the view.html.php i created several instances of the needed models. Of course you need to set the correct queries to collect the data, i only submitted the code for the models:
$model	= $this->getModel();

//get the subpropertyitem
$subpropertyitem	= $this->get('data');

$model_booking = JModel::getInstance('bookingitem', 'Mw1bookingModel');
$bookingitem = $model_booking->get('data');		

$model_address = JModel::getInstance('bookingitem', 'Mw1bookingModel');
$addressitem = $model_address->get('data');	

$this->assignRef('subpropertyitem',		$subpropertyitem);
$this->assignRef('bookingitem',		$bookingitem);
$this->assignRef('addressitem',		$addressitem);

Next in the form template you need to use the correct dataObject for the correct fields, examples:
<?php echo JDom::_('html.form.input.calendar', array(
	'dataKey' => 'dt_till',
	'dataObject' => $this->bookingitem,  // object for the booking
	'dateFormat' => $this->config->get('date_format'),
	'required' => true
	));

<?php echo JDom::_('html.form.input.text', array(
	'dataKey' => 'last_name',
	'dataObject' => $this->addressitem,  // object for the address
	'size' => "32",
	'required' => true,
	'validatorMsgRequired' => "MW1BOOKING_VALIDATOR_LAST_NAME_IS_REQUIRED",
	'validatorHandler' => "length_100",
	'validatorRegex' => "/^.{2,100}$/",
	'validatorMsgInfo' => "MW1BOOKING_VALIDATOR_LAST_NAME",
	'validatorMsgIncorrect' => "MW1BOOKING_VALIDATOR_INCORRECT_LAST_NAME_AT_LEAST_2_CHARACTERS_REQUIRED"
       ));

In the controller i created a new function, in my case savebooking. Of course in the view.html is refer to this function:
$bar->appendButton( 'Standard', "save", "MW1BOOKING_JTOOLBAR_SAVE", "savebooking", false);

In the save_booking function i save the data in different tables.
$model_addressitem = JModel::getInstance('addressitem', 'Mw1bookingModel');
$model_bookingitem = JModel::getInstance('bookingitem', 'Mw1bookingModel');
        
$model_addressitem_save = $model_addressitem->save(JArrayHelper::toObject($post));
		
if ($model_addressitem_save) {
   $post['address_id'] = $model_addressitem->_id; 
   $model_bookingitem_save = $model_bookingitem->save(JArrayHelper::toObject($post));
  }

Of course this is only the basic code. For example i first save an address record, after that i save the booking record. If saving the booking record fails you maybe want to delete the address record.

Another possibility is to save the data in a transaction. Then there is the function rollback all queries in a transaction when one of the queries failes. But then you need to do some more programming.
Molenwal1 Webdesign
www.molenwal1.nl
The following user(s) said Thank You: admin, VeCrea, abdullah, d.stragapede

Please Log in or Create an account to join the conversation.

Last edit: by molenwal1.

Re: Editing multiple tables in one view 28 Apr 2012 20:38 #2175

  • VeCrea
  • VeCrea's Avatar
  • Offline
  • Platinum Member
  • Platinum Member
  • Absolute JCook fan
  • Posts: 473
  • Thank you received: 100
Hi,
My request would be much more simplier. I only would like to update a field if another one is filled in.
To explain it further, i would like a enum field to be switched to "shipping sent" when the "shipping date" is selected by the shippings responsible.
Any idea to do this very simply ?

Please Log in or Create an account to join the conversation.

Re: Editing multiple tables in one view 30 Apr 2012 10:04 #2184

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 986
Thanks, molenwal1


@VeCrea

TODO (If from the same table)
You must only make a test in the check() method of your table file,
so if shipping date is setted, you set up you property shipping = sent


TODO (If from another table)
In you controller that receive shipping date, if the save call worked properly, launch a second call to update to data of your choice, dealing with models


Hope I am clear
Coding is now a piece of cake
The following user(s) said Thank You: VeCrea, edwardcox

Please Log in or Create an account to join the conversation.

Re: Editing multiple tables in one view 30 Apr 2012 10:23 #2186

  • VeCrea
  • VeCrea's Avatar
  • Offline
  • Platinum Member
  • Platinum Member
  • Absolute JCook fan
  • Posts: 473
  • Thank you received: 100
You're the world greatest

Please Log in or Create an account to join the conversation.

Re: Editing multiple tables in one view 01 Sep 2012 17:56 #3351

  • VeCrea
  • VeCrea's Avatar
  • Offline
  • Platinum Member
  • Platinum Member
  • Absolute JCook fan
  • Posts: 473
  • Thank you received: 100
I'm coming back on this one because it seems it doesn't work with different users... With me having admin rights, it works flawlessly. With other users, i get a blank page on save...
if (!empty($this->livr_reel) && ($this->my_date != '0000-00-00'))
		{
			$livr_reel = xxxHelper::getSqlDate($this->livr_reel, array('%d-%m-%Y', '%Y-%m-%d'));
			if ($livr_reel === null){
				/*JError::raiseWarning(2001, JText::sprintf("xxx_VALIDATOR_WRONG_DATETIME_FORMAT_FOR_PLEASE_RETRY", JText::_("xxx_FIELD_DATE_REELLE_DE_LIVRAISON")));
				$valid = false;*/
			}
			else
				$this->statut = "livraison_effectuee";
				$this->livr_reel = $livr_reel->toMySQL();
		}
This is the code i use with
$this->statut = "livraison_effectuee";
updating the other field.
Thanks for your help

Please Log in or Create an account to join the conversation.

Re: Editing multiple tables in one view 03 Sep 2012 15:08 #3363

Hello,

I am trying to do a similar work, in my case I show a one-to-many reltaionship this way: at the item view, I want to add a grid with the list of related items from the second table.

I have managed to compose the view, using two forms in the same page (one _fom and one _grid), and calling two models (the second with a where clause) and everything seems to work, BUT I am having trouble with the edit/delete bottoms of the grid: it seems that JDom depends on the form to be called "adminForm" (hardcoded), and hence, I have a collision with the two forms.

Is there a quick way to fix this?

Thanks a lot!

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
Time to create page: 0.106 seconds

Although I found this tool some time ago I just started learning experimenting and building this first component on Monday and now on Thursday afternoon it's already online. Although I already had a working version done with another tool this is much easier to build and at the end you have the freedom to own your own component. Once you know how to use Cook you will boost your productivity as the learning curve is really moderate compared to many other tools.
Giori (Forum)

Get Started