Welcome, Guest
Username: Password: Remember me

TOPIC: New features are coming soon

New features are coming soon 23 Sep 2015 11:34 #13483

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
Great!

I will try to implement in my component. I have it set up already the "old fashioned" way but I will see what this brings.
The administrator has disabled public write access.

New features are coming soon 23 Sep 2015 11:40 #13484

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
And if you want to make infinite recurse, you can do it manually, but sooo easy.
$model->loadRelations('level1.level2.level3');
Then you can easily access the datas in the template file.

The relations have to be declared in every plural model. (constructor)
Simply check if it is already declared. In case, create it manually.
Every field you want to load is given in the last param : selectFields
At the moment, you cannot load a Foreign Key value from a relation context. (Can be manually done)
Coding is now a piece of cake
Last Edit: 23 Sep 2015 11:41 by admin.
The administrator has disabled public write access.

New features are coming soon 01 Oct 2015 21:06 #13552

  • dyvel
  • dyvel's Avatar
  • Offline
  • Elite Member
  • Posts: 200
  • Thank you received: 11
  • Karma: 10
I can create N:1 with Xref but not N:M relations in builder - I use a join table (orders) with FK to Products and FK to Clients
On Products I have a FK to Orders - only when I do that I get Xref, but only N:1

When you have time Chef, I'd appreciate a small guide on how to set N:M up in builder.
Thanks
The administrator has disabled public write access.

New features are coming soon 01 Oct 2015 21:31 #13553

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
Wich field did you dragged ?
Open the 'Orders' XREF node, then open 'Clients' Node, then pick the field value you want to see.
As simple as that.
Coding is now a piece of cake
The administrator has disabled public write access.

New features are coming soon 01 Oct 2015 23:58 #13554

  • dyvel
  • dyvel's Avatar
  • Offline
  • Elite Member
  • Posts: 200
  • Thank you received: 11
  • Karma: 10
Please see attached - my "problem" is, I can only select a "product" once (N:1). Normally I wouldn't have a FK from Products back to orders, but that was the only way I could get the Xref to even show up on my join table (Orders).





The administrator has disabled public write access.

New features are coming soon 02 Oct 2015 07:56 #13556

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
When creating a N:M structure you need 3 tables:
Source1
Source2
Pivot

Source1 and Source have an id, the pivot table should only contain two field FK1 to Source1 and FK2 to Source2

In your example:
- Remove the FK"order_id" and the FK "id" from products and remove the FK "products_id" and the FK "id" from orders
- Create a table productsXorders with to FK's. 1 to orders and 1 to products.

Maybe you should close the tables (tabs) if you don't see the xref fields.

That way you have correctly implemented the N:M relations.

BUT: You still can NOT add just 1 productX to orderY.
You can add productX, productY and such to orderY and also to orderZ.

What is not possible (and also not a good N:M relation) is adding productX multiple times to orderX.
You can add extra options in your pivot table (productsXorders) like quantity or "options" and store the extra info there,
That is how is done in other shops as well
You can not use the out-of-the-box xref functionality from Cook with that, but have to create customized functionality.

A guide on the Xref: www.j-cook.pro/index.php/r/relations
The administrator has disabled public write access.
The following user(s) said Thank You: admin, dyvel

New features are coming soon 02 Oct 2015 09:38 #13558

  • dyvel
  • dyvel's Avatar
  • Offline
  • Elite Member
  • Posts: 200
  • Thank you received: 11
  • Karma: 10
Thanks Romkabouter - your guide was very helpful.

You said: BUT: You still can NOT add just 1 productX to orderY.

The setup I did at first (screenshots) did just that. I could add productX only once to only one order - not what I was looking for, but I could.

I would be very cool if we could use input fields when selecting items on a Xref relation in builder, but this is already a huge help :-)
The administrator has disabled public write access.

New features are coming soon 02 Oct 2015 10:18 #13559

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
My mistake, I meant more then 1
The administrator has disabled public write access.

New features are coming soon 02 Oct 2015 12:31 #13560

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
Thanks Romkabouter,
Dyvel, your diagram makes no sense. I confirm, remove the 'order_id' FK in Products

Continuing on this case study, let's say:
a. If you want to edit products in a Order page : its a N:1
b. If you want to edit products in a Client page : its a N:m, and every link is automatically handled by the pivot. It creates 'Orders'

IMPORTANT (I should write in the doc) :
Other thing interresting : The model behaviors (Wizard automatisms) are kept working because the relations are created and destructed, using your component models.

In your case, you have 'order_date' field in the pivot (Orders). Well, that's correct. Let's imagine you have auto creation date with the wizard.
In the second case (b.), when the 'orders' are automatically managed by the N:m relation, then you 'creation_date' field will be set with correct value.
Amazing, no ?
Of course, you can choose 'raw', and optimize the queries, but then, wizards will not work for the pivot.

$raw is the last param recieved in the relation contructor.
Coding is now a piece of cake
Last Edit: 02 Oct 2015 12:36 by admin.
The administrator has disabled public write access.
The following user(s) said Thank You: dyvel

New features are coming soon 04 Oct 2015 20:03 #13563

  • dyvel
  • dyvel's Avatar
  • Offline
  • Elite Member
  • Posts: 200
  • Thank you received: 11
  • Karma: 10
Hi chef

I knew my diagram didn't make sense - was trying "everything" to get Xref to show :-) But thanks to Romkabouter's guide, I got N:m working.

If my pivot table contains related fields like quantity etc., what would be the correct way to show those fields in my order view form? Builder can create the relation, but in my testcase - creating an order requires more than that.
The administrator has disabled public write access.

New features are coming soon 04 Oct 2015 20:48 #13564

  • dyvel
  • dyvel's Avatar
  • Offline
  • Elite Member
  • Posts: 200
  • Thank you received: 11
  • Karma: 10
Or perhaps more importantly, how would I go about saving the data I input?

Simple form test input:
$fieldSets = $this->form->getFieldsets();

?>
<?php $fieldSet = $this->form->getFieldset('order.form');?>
<fieldset class="fieldsform form-horizontal">

	<?php	
	
	// JForms dynamic initialization (Cook Self Service proposal)
	$fieldSet['jform_client_id']->jdomOptions = array(
			'list' => $this->lists['fk']['client_id']
		);
	echo $this->renderFieldset($fieldSet);
	
	$products = $this->item->products;
	$params = $this->item->productsxorders;
	
	foreach ( $products as $key => $product ) {

		echo '<div class="control-group field-id">';
			echo '<div class="control-label"><label>'.$product->title.'</label></div>';
			echo '<div class="controls">';
				echo '<input type="text" name="quantity" id="quantity" value="'.$params[$key]->quantity.'" />';
			echo '</div>';
		echo '</div>';
		
	}
		
	?>
</fieldset>

I need to modify my model order.php save function?
The administrator has disabled public write access.

New features are coming soon 05 Oct 2015 10:01 #13568

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
Interresting....

That's the next step.
Well, you can fork the syncRelation() function in the item model.

At the moment Cook do not propose it, because there is no input for that. It must be custom design as you did.
It is a good idea to implement. (At least in the model class)

Please do it as I tell you now : (I will implement it in future)

$values parameter is a simple array of values at the moment.
So, let's make $array recieving sub arrays as follow :

Ex:
$values = array(
	'23' => array(
		'quantity' => '2',
	),

	'52' => array(
		'quantity' => '4',
	),

);

First, the function extract the ids from array_keys($values) (when recognized necessary)
And then , for every link, you get the datas to populate in the Pivot. ($model->save())
It is really simple to achieve.

For you, you do not need to change code in the $raw mode if you don't use it. I will do it.
It is a good exercice. Once you have put your hands in syncRelations(), then you will understand pretty well the process.
Not a big deal in facts.

By the way, excellent hint. I love to break the limits when it is usefull.
Coding is now a piece of cake
The administrator has disabled public write access.

New features are coming soon 05 Oct 2015 10:10 #13569

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
And then you also have to fork the save() function of the item model.

You have to map the recieved datas from the post, to the new array structure.
There is a little problem I think because if you fork, you will call parent::save(), and it will call the original function.

So the forking will be tricky, maybe not possible. Just hack your component.

Oh, yes, I have an idea.
When you call parent::save(), just give to the parent a restricted array, without the relation (remove data form the array)
But because you have kept a copy of this array, just map, and syncRelation() in the fork.

I am not sure to be clear enough.
Coding is now a piece of cake
The administrator has disabled public write access.

New features are coming soon 06 Oct 2015 18:17 #13575

  • dyvel
  • dyvel's Avatar
  • Offline
  • Elite Member
  • Posts: 200
  • Thank you received: 11
  • Karma: 10
I'm feeling stupid :-D
Tried to follow your guide, but I don't get it...
The administrator has disabled public write access.
Time to create page: 0.178 seconds

Get Started