Welcome, Guest
Username: Password: Remember me

TOPIC: Filters : Custom a FK list

Filters : Custom a FK list 20 Nov 2012 14:27 #5502

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 16
In a previous version of JCOOK, not so long ago, we would find references like

$model_deceased_gp = JModel::getInstance('contacts', 'MolefsModel');
$model_deceased_gp->addGroupBy("a.ordering");
$lists = $model_deceased_gp->getItems();

which you could alter using addWhere etc

This seems to have completely gone, and I cant figure out where the equivalent has been moved to. I have built an app using all the latest settings.. beta 2.0, jquery etc

Any assistance greatly received.
Morgan Leecy MCSE

Novell / Linux
PHP. MYSQL, Apache, node.js
Coldfusion, JQuery, HTML5
Joomla
The administrator has disabled public write access.

Filters : Custom a FK list 20 Nov 2012 16:34 #5512

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 16
I suspect its moved to the individual selects on each seperate table.. i.e all my contact selectors have all moved to contacts and been called select33 etc.

I need to narrow these selectors down by a hardcode contactype INT

In the previous j-cook I would just put

addWhere("a.contacttype=5") but this has all gone and everything I knew about jcook has changed.. and I cant change it back. I cant find any documents or forums posts about it

please can anyone assist
Morgan Leecy MCSE

Novell / Linux
PHP. MYSQL, Apache, node.js
Coldfusion, JQuery, HTML5
Joomla
The administrator has disabled public write access.

Filters : Custom a FK list 21 Nov 2012 14:51 #5568

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 16
anyone any ideas on how this functionality now works?
Morgan Leecy MCSE

Novell / Linux
PHP. MYSQL, Apache, node.js
Coldfusion, JQuery, HTML5
Joomla
The administrator has disabled public write access.

Filters : Custom a FK list 21 Nov 2012 16:21 #5580

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
To have a good and clean application, it is better when the SQL query is built in the model.
You will still be able to add SQL statements from the view (public functions), so it shouldn't change anything.

Now in 2.0, I prefer to migrate every single call of SQL, in a predefined query in prepareQuery().

$model->addWhere(), addJoin(), addSelected(), are still presents, and after a long reflexion are used a lot because it is really important.

The current filter rewriting is actually improving prepareQuery() in that way.

Hum... What was the question ?

Are you saying that this line is missing :
$model_deceased_gp = JModel::getInstance('contacts', 'MolefsModel');

One thing absolute : Cook Always catch datas trough the models, so it is ALWAYS possible to play with addWhere() and co, and to play with statesVars to customize and instance standard shared behaviors of the query.

with prepareQuery() and using well the states vars, your SQL query can become sort of 'intelligency' wich automatically create what you need.
The use of the states vars are defining some human understandable orders to the Query. And when you start to code in, it is amazing.
Coding is now a piece of cake
The administrator has disabled public write access.

Filters : Custom a FK list 21 Nov 2012 16:32 #5584

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 16
Ok end result I am seeking is this.. I have several contact_type database records that will be hard coded when the component is installed..

A contact type with id 12 is a Coroner, and I have a select box in my component that is called coroner and currently it is specifically listing ALL contacts, I want it JUST to list all contacts with a contact type of 12

In the previous version I would have added

$model_coroner->addWhere("a.contact_type=12");

and the job would have been done, as it was in a previous JCOOK version, now I have moved to the new version, I can see where to put this


I have been playing around with adding new filters in the contacts model such as

//WHERE - FILTER : Contact Type - Coroner
if($this->getState('filter.contact_type_coroner') != null)
$query->where('a.contact_type = 12');

and in the model that is calling the select list putting

$data->coroner = $jinput->get('filter_coroner', $this->getState('filter.contact_type_coroner'), 'INT');


but I think I am completely off-track :(

I have seen the XML file with the form fields in, and I dont seem to be able to put in a filter there.. I am guessing that I should be altering select13 in the view.html file
Morgan Leecy MCSE

Novell / Linux
PHP. MYSQL, Apache, node.js
Coldfusion, JQuery, HTML5
Joomla
The administrator has disabled public write access.

Filters : Custom a FK list 21 Nov 2012 16:35 #5586

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 16
I have tried

{
$model_item = JModel::getInstance('contactsitem', 'MolefuneralModel');

$model_item->setState('contactsitem.id', $values[0]); //Ground value
$model_item->addWhere('contact_type=12');
$selectedItem = $model_item->getItem();

//Redefine the ajax chain key values
if ($model_item->getId() > 0)
{

}

}

in the view.html file with no success... it still lists every contact in the database (1400 of em)
Morgan Leecy MCSE

Novell / Linux
PHP. MYSQL, Apache, node.js
Coldfusion, JQuery, HTML5
Joomla
The administrator has disabled public write access.

Filters : Custom a FK list 21 Nov 2012 16:40 #5587

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
You are on the good track

in your view :
$model_xx->setState('filter.contact_type_coroner', 12);
$model_xx-> getItems();

You can also follow this topic to understand a little bit :
www.j-cook.pro/forum/9-coding-inside-you...om-query-and-filters

It is different than your request, but shows the principle of filters and state vars.
$data->coroner = $jinput->get('filter_coroner', $this->getState('filter.contact_type_coroner'), 'INT');

This line is not calling a filter. Nothing to do with filtering. It is to preffill an item, WHEN a filter is setted up in URL. You may found it in an ITEM file.

Filtering always belongs to COLLECTION files.
Coding is now a piece of cake
The administrator has disabled public write access.

Filters : Custom a FK list 21 Nov 2012 16:48 #5589

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Yes I understand !
You want to return ony ONE item row.

It never changed.
addWhere() is not supported in ITEM model. The reason is simple : by default, the only WHERE possible is :
a.id = cid

But ITEM Model is a correct place for that. Continue.

So, when you want to base on another search, you have to create a new CONTEXT query in prepareQuery(). A new case entry in the switch.

Get the state var to filter your query. No need to use addWhere() exceptionalty in this case. You can do :
$query->where('a.contact_type =' . (int)$this->getState('filter.contact_type_coroner'));
Coding is now a piece of cake
The administrator has disabled public write access.

Filters : Custom a FK list 21 Nov 2012 17:37 #5592

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 16
I feel like I am on the brink of grasping this.. with one incredibly confusing issue I cant work through

The SELECT lists.. which lists LOTS of items (in theory) are referring to the item model, not the collection.. is it constructing the list one item at a time?

Back to my problems...

I see in the prepareQuery code for contacts (in the collection model)
//WHERE - FILTER : Contact Type
		if($this->getState('filter.contact_type') != null)
			$query->where('a.contact_type = '. (int)$this->getState('filter.contact_type'));

which is exactly what I need as the where statement is already there waiting to be called, i just, in theory need to add
case 'select13':
				/* Ajax List : Contacts
				 * Called from: view:funeralitem, layout:funeralitem
				 */
				//Init or override the list of joined values for entry point
				if (is_array($values) && isset($values[0]) && $values[0])   //First value available
				{
					$model_item = JModel::getInstance('contactsitem', 'MolefuneralModel');

					$model_item->setState('contactsitem.id', $values[0]);	//Ground value
					[b]$model_item->setState('filter.contact_type', 12);[/b]
					$selectedItem = $model_item->getItem();

					//Redefine the ajax chain key values
					if ($model_item->getId() > 0)
					{

					}

however.. this is what is confusing me.. the above code is in the COLLECTIONS's view.html file.. but is referencing an ITEM for a dropdown list..

I tried changing the model to the collection and also left as the item model, but in BOTH instances I am STILL pulling down every single contact in the database
Morgan Leecy MCSE

Novell / Linux
PHP. MYSQL, Apache, node.js
Coldfusion, JQuery, HTML5
Joomla
The administrator has disabled public write access.

Filters : Custom a FK list 22 Nov 2012 00:07 #5599

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Yes because at this point you do not need any model in theory. You didn't said you was in Ajax call.

Here it is only implementing a wrapper and an url construction. It seems that this item model is initializating something but not used.

The thing is bit complex to understand, but easy at the end.
You do not need this model at the end. It is used only when you have groups to retreive a fk values successions, and auto populate the values in cascad. It is a bit complex.

ex : When a 3 level groups in ajax has a final value at the loading of the page = 3 succesives calls are displaying the 3 initializated combos. these 3 values are retrieved with this ITEM model.

So...
At the end forget it because I see it is empty and a void call. (Source to avoid in the generation, for me)

The most important COLLECTION model you may use is $model.

It is defined in the top of the file.
BUT : getItems() is not in the good place, so one solution is to move $model->getItems() bellow, in every cases of $render (in the switch statement)
Now, before getItems(), in your particular case (select13), you can intervent in the model BEFORE getItems().

Excuse me, but nothing was very clear for me since the begining.
So you are dealing with COLLECTION model, Ok
AddWhere() is available in collection.

But, you was in an ajax call, and the model var is simply $model.
That was my confusion.
Coding is now a piece of cake
The administrator has disabled public write access.

Filters : Custom a FK list 22 Nov 2012 10:25 #5611

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 16
Darn, now I feel really stupid. Yeah, unticked AJAX loading and the addGroupby etc are back in VIEW.HTML.

The reason I made them AJAX loading is because I wanted to add (in the future) the ability to ADD a new contact on the fly from another form if a contact was missing.. and I assumed that making the select an AJAX select box would make it easier to pull the new contact into the select box easier.

I should probable make that a suggestion if its not already on the cards, the ability to create a record on another table that is linked by a FK so you can add options to a select box if that option is not there.
Morgan Leecy MCSE

Novell / Linux
PHP. MYSQL, Apache, node.js
Coldfusion, JQuery, HTML5
Joomla
The administrator has disabled public write access.
Time to create page: 0.126 seconds

Get Started