Welcome, Guest
Username: Password: Remember me

TOPIC: Specifiying a variable in a dropdown

Specifiying a variable in a dropdown 17 Sep 2012 18:44 #3710

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 16
I have a table of contact types all defined, and a list of contacts each of which is one of the defined types.

Some of the contact types will be hardcoded when I publish my component.. however, I need to use these hardcoded contacts in another view. How can I pass a variable to the model to specify a certain contact type without having to copy the same model several times.

Example

Contact type 1 = Butcher
Contact type 2 = Grocer
Contact type 3 = Baker

Contact 1 (contact type 1) = Mr Brown
Contact 2 (contact type 2) = Mr Smith
Contact 3 (contact type 3) = Mr White


Main table

PLease specify the suppliers for the supermarket

Butcher = Drop down of all contacts of type 1
Baker = Drop down of all contacts of type 2
Grocer = Drop down of all contacts of type 3

as you can see.. ALL three are foreign table types essentially refering to the same data, and therefore the same model, but each will specify a different contact type..

can this be done usingf the existing JDom
Morgan Leecy MCSE

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

Re: Specifiying a variable in a dropdown 17 Sep 2012 18:59 #3712

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
JDom is ONLY displaying and writting javascript. You should never write hard coded lists in JDom.
Once you get your list, then call JDom to show it.

Well :
Use the enumerations. It is exactly for that.
If you are affraid of creating 2 enumerations in 2 differents tables, then once your component is finished, merge the 2 enumerations in one. I mean, you always ask the same.

Open your main helper, see how it works for enumerations. If you have to hard code something common, do it in a helper. In your case, enumerations is what you need.
Coding is now a piece of cake
The administrator has disabled public write access.

Re: Specifiying a variable in a dropdown 17 Sep 2012 19:06 #3713

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 16
I think I may have over complicated things.. the only thing I want to hardcode (which may be the wrong term) is to specifiy a value to pass to the model to restrict the choices.. i think the alteration may need to be made here.. I pull these examples out of a view

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

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

As you can see they both pull data from the same table.. BUT I want to specifiy in the first one.. asset_type=1, and in the second asset_type=2

maybe something like $model_coffin->addWhere("a.asset_type=1");

or something similar
Morgan Leecy MCSE

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

Re: Specifiying a variable in a dropdown 17 Sep 2012 19:09 #3714

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
I read again your topic, I can see you search for something else.

In the view file, find the correct models that are calling the lists.

Create a loop, wich is calling always the same query model, but adding the correct statement (where)
foreach($types as $type)
{
... Model init $myModel
$myModel->addWhere('a.type = ' . $db->quote($type));
$typesItems[$type] = $myModel->getItems();
}

Not tested. Never did that, in facts.

One problem can be to flush the cache datas of the model, because if you do not do that, always the first called list of items will be returned.

Sure you will find the way.
Coding is now a piece of cake
Last Edit: 17 Sep 2012 19:10 by admin.
The administrator has disabled public write access.

Re: Specifiying a variable in a dropdown 17 Sep 2012 19:24 #3715

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
And, maybe even better, it depends the context, you can do a complete SQL query, and then create differents arrays (PHP) depending on the type.

I think it is better.
Coding is now a piece of cake
The administrator has disabled public write access.

Re: Specifiying a variable in a dropdown 17 Sep 2012 20:30 #3719

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 16
yeah I see what you mean.. REALLY annoying

I did

$model_coffin = JModel::getInstance('assets', 'MolefsModel');
$model_coffin->addGroupBy("a.ordering");
$model_coffin->addWhere("a.asset_type=1");
$lists = $model_coffin->getItems();

$model_lining = JModel::getInstance('assets', 'MolefsModel');
$model_lining->addGroupBy("a.ordering");
$model_coffin->addWhere("a.asset_type=2");
$lists = $model_lining->getItems();

$model_furniture = JModel::getInstance('assets', 'MolefsModel');
$model_furniture->addGroupBy("a.ordering");
$model_coffin->addWhere("a.asset_type=3");
$lists = $model_furniture->getItems();

after defining the 3 asset types.. the FIRST model works perfectly and successfully pulls off only asset_type=1 but the other two just list all assets..Its wierd looking at the SQL calls as its almost as though it ignores the other two.. here is the output

SELECT a.*
FROM `f9t0r_molefs_assets` AS a
WHERE a.asset_type=1
ORDER BY a.ordering,a.ordering, a.branch
SELECT a.*
FROM `f9t0r_molefs_assets` AS a
ORDER BY a.ordering,a.ordering, a.branch
SELECT a.*
FROM `f9t0r_molefs_assets` AS a
ORDER BY a.ordering,a.ordering, a.branch

only the first WHERE statement is called.. eesh
Morgan Leecy MCSE

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

Re: Specifiying a variable in a dropdown 17 Sep 2012 20:31 #3720

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 16
LOL fixed.. i read my post back and saw the error

$model_coffin = JModel::getInstance('assets', 'MolefsModel');
$model_coffin->addGroupBy("a.ordering");
$model_coffin->addWhere("a.asset_type=1");
$lists = $model_coffin->getItems();

$model_lining = JModel::getInstance('assets', 'MolefsModel');
$model_lining->addGroupBy("a.ordering");
$model_lining->addWhere("a.asset_type=2");
$lists = $model_lining->getItems();

$model_furniture = JModel::getInstance('assets', 'MolefsModel');
$model_furniture->addGroupBy("a.ordering");
$model_furniture->addWhere("a.asset_type=3");
$lists = $model_furniture->getItems();

i was calling model_coffin in all three instances

Fixed.. simply using the addWhere solves my problem.. BRILLIANT.. full steam ahead..
Morgan Leecy MCSE

Novell / Linux
PHP. MYSQL, Apache, node.js
Coldfusion, JQuery, HTML5
Joomla
Last Edit: 17 Sep 2012 20:32 by MorganL.
The administrator has disabled public write access.

Re: Specifiying a variable in a dropdown 17 Sep 2012 20:59 #3721

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
You can do even better ;-)
1. Create a new context profile and set the State to call it.

-> a.ordering can be handled in the query profile

-> Instead of addWhere, you can also use a new state var (give a namespace) and use it to send the value of 'type'
You can immagine, that if this state var is not null, the filter apply.



addWhere() is specific to Cook Self Service.

Sometimes, it is really nice to code inside the view, when the custom is only a specific query.

In the same hand, I have doubts the way of coding of Joomla to put the SQL query in the field types, combos and lists for example. (models/fields/xxxx.php)
Cook separate the jobs and if the model add a 'publish' or 'access' functionality, for example, there is no need to remember where the table has been asked to add the changes.
Cook always call the lists, and items throuhg the Model, wich makes much more sense.
...And the acl params are always availables, as well as every feature you put in populateParams, or populateObjects(), empty for the moment.

populateObjects(), can be used to fill FK N:1 or N:N links, in order to handle arrays or any other useful object.
Coding is now a piece of cake
The administrator has disabled public write access.
Time to create page: 0.105 seconds

Get Started