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

TOPIC: [SOLVED] Getting the current query in a helper v2.0

[SOLVED] Getting the current query in a helper v2.0 09 Nov 2012 12:48 #5159

  • BTB300
  • BTB300's Avatar
  • Online
  • Moderator
  • Posts: 414
  • Thank you received: 130
  • Karma: 46
Hi All,
Had a few weeks off now getting back into it and i have a mental blank hoping someone can help please

I need to get the current query ie. "SELECT a.a_field, a.b_field FROM..." from the current model and parse it to an additional helper file,

the structure and functionality of the additional helper is defined and working but i am getting an empty sql string in the model

In the model i need to return the current query such as return $this->helperquery=$query;
In the helper i need to set the sql query as $sql=$this->helperquery

In version 1.5 i just called the function in the model but in version 2.0 its protected and parse by reference seems to be causing some errors what am i missing

Thanks in advance
The administrator has disabled public write access.

Re: Getting the current query in a helper v2.0 09 Nov 2012 13:56 #5164

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
I'm sure that you've probably thought of this but, is the query an array or a string at the point of passing it to your helper function?

What about $this->helperquery($query); ?

probably a stupid question but, do you have a public function in your model called helperquery() ?

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: Getting the current query in a helper v2.0 09 Nov 2012 15:39 #5173

  • BTB300
  • BTB300's Avatar
  • Online
  • Moderator
  • Posts: 414
  • Thank you received: 130
  • Karma: 46
audibleid wrote:
I'm sure that you've probably thought of this but, is the query an array or a string at the point of passing it to your helper function?

What about $this->helperquery($query); ?

probably a stupid question but, do you have a public function in your model called helperquery() ?

Gez

Thanks audibleid,
I think i know what it is... i think its my mistake

at present
the helper is called from the layout
the helper is trying to get the query such as "SELECT * From ..." from the model
function _myhelperfunction()
	{
		$db =& JFactory::getDBO();
		$model   =& $this->getModel();
		$sql="";
		$sql= $model->gethelperSql();
		.....
the $query is returned from the switch statement in the model
and the function gethelperSql calls the private function prepareQuery in the model but complains with the following Call to a member function from() on a non-object in

Thanks for helping
Last Edit: 09 Nov 2012 15:39 by BTB300.
The administrator has disabled public write access.
The following user(s) said Thank You: JoomGuy

Re: Getting the current query in a helper v2.0 09 Nov 2012 15:40 #5174

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Glad you got it sorted!

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: Getting the current query in a helper v2.0 09 Nov 2012 16:02 #5179

  • BTB300
  • BTB300's Avatar
  • Online
  • Moderator
  • Posts: 414
  • Thank you received: 130
  • Karma: 46
Hi Gez
Not there yet
the public function gethelperSql calls the private function prepareQuery in the model

but Errors out with the following Call to a member function from() on a non-object in.....
The administrator has disabled public write access.

Re: Getting the current query in a helper v2.0 09 Nov 2012 19:14 #5191

  • admin
  • admin's Avatar
  • Online
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Be sure you do dot alternative than sending to a helper. When you can do everyting in the model.
I am quite sure you know about this, but only for visitors, I precise that the use of helper must be limited.

Helpers are called in static (my opinion)
Classes are instancied.

I read again. Are you sending to a helper ? Or only a helper function in the model ?
Coding is now a piece of cake
The administrator has disabled public write access.

Re: Getting the current query in a helper v2.0 09 Nov 2012 19:20 #5193

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Is the non object part of the error because the query you're passing to the function is in string format?

Also, RE: the from() method: should it be $query->from() ?

Just a couple of thoughts, not sure if they are relevant! :unsure:
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: Getting the current query in a helper v2.0 11 Nov 2012 11:32 #5264

  • BTB300
  • BTB300's Avatar
  • Online
  • Moderator
  • Posts: 414
  • Thank you received: 130
  • Karma: 46
Thanks Audibleid and Admin,
Sorry for slow response as I had an unexpected trip out of town for the weekend
Came back to it tonight and everything fell into place

@admin agreed the Helper should be static and technically it is the current query simply overrides some i guess you can say "default" options of the helper function

and thinking about my current helper method am I really calling the same query twice???

as for the idea of using a helper
- i need to access a specific model function whenever this helper is called
- it may not necessarily be directly related to the current model / view
- it may need to have more data / fields than is available in the current model / view
- the current query overrides "default" values of the "normal" function performed by the Helper
- the helper also manipulates specific data such as fks / strings / creates unique values
- performs some i guess you can say n:n table updates

I guess perhaps i could achieve the same by implementing it as as an extension of the jmodel.item class but i am thinking its own separate class would be best


@ Audibleid Thanks I dont know what i was doing wrong but i think...
I was trying to call the helper from the view then determine the model in the helper then call the protected model prepareQuery() function

As i said earlier it all fell into place tonight... all i had to do was call the helper from the model and pass $helpersqlquery by replacing break; in the relevant case of the switch function of the model


In the model
protected function prepareQuery(&$query, $pk)
{

	//FROM : Main table
	$query->from('#__somecom_sometable AS a');

	switch($this->getState('context'))
	{
		case: 'somecase'
		....
		// break; <=removed from the case and replaced with below
		$helpersqlquery=$query;
		MyAdditionalHelper::getMyHelperFunction($helpersqlquery);
		return $helpersqlquery=$query;
	
		case: 'nextcase'
		.....
		.....

and in the helper i have
function getMyHelperFunction($helpersqlquery)
	{	
		$db = JFactory::getDBO();
		$sql= $helpersqlquery;
		
		$db->setQuery($sql);
		$result=$db->loadAssoc();
Last Edit: 11 Nov 2012 11:57 by BTB300.
The administrator has disabled public write access.
The following user(s) said Thank You: JoomGuy

Re: Getting the current query in a helper v2.0 11 Nov 2012 11:59 #5267

  • admin
  • admin's Avatar
  • Online
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
I do not understand your source.

Can you reexplain why are you using such helper ? What are you trying to do ?
Coding is now a piece of cake
The administrator has disabled public write access.

Re: Getting the current query in a helper v2.0 11 Nov 2012 18:57 #5298

  • BTB300
  • BTB300's Avatar
  • Online
  • Moderator
  • Posts: 414
  • Thank you received: 130
  • Karma: 46
admin wrote:
I do not understand your source.

Can you reexplain why are you using such helper ? What are you trying to do ?

Hi Admin,
I am not surprised your confused its complicated :( i will try to make it a little clearer

For example a tax component that has a number of tables
[TAX EVENTS LIST] [USER TAX DETAILS] [EXTENDED USERS] [TASKS TODO] [TAX CORRESPONDENCE] [CORRESPONDENCE DEFAULTS]

The Helper sends a generic email using the values from the table [CORRESPONDENCE DEFAULTS]
- such as "Thank you your tax has been submitted"
- if an alternate email override is defined in the table [TAX EVENTS LIST] or [USER TAX DETAILS] or [TASKS TODO] some default values are overridden such as email_subject, cc_to, or the email_body

The helper updates some tables that "sometimes" have no physical relation to the current table
Tax Correspondence - is simply a backup if you like of the email content sent between staff and customers
Tasks Todo - is a list of tasks for staff / customers to complete and is only updated if a specific correspondence type is sent
User Tax Details - is updated if a specific TASKS TODO type is completed

Although the actual helper is much more complicated I will try to provide two simple examples (and keep them as short as possible)

User Tax Details - Submitted Layout
Users are presented with a fly layout "Submitted" displaying the some basic details from the [USER TAX DETAILS] table such as submitted_date, submitted_time, reference_number...

The current model "submitted" query does not contain enough data to perform several required tasks
- send an email to the accountant or the customer
- update the [TASKS TODO] table
- update the [TAX CORRESPONDENCE] table

TAX Tasks - Task Completed Layout
A task has been completed by an accountant but the "default" email body normally used for this task needs to be overridden by the accountant because we to remind them to provide additional information
- send email to customer with an update of tax progress (override the default email body)
- update the [TAX CORRESPONDENCE] table
- update the [USER TAX DETAILS] table

My need to get the current query and parse it to a helper i guess is related to the query being more precise in Cook V2.0 and does not include "SELECT a.*, .... FROM ....".

AND HERE IS WHERE IT GETS MESSY!
The original code
"SELECT a.field1, a.field2, some_table.field1, some_table.field2"

- We all say "a simple fix" replace the precision with "a.*" now you can access all values of the main table
"SELECT a.*, some_table.field, some_table.anotherfield"

- but it now creates additional problems i guess because a.field3 was not defined in the original build???
so if you use something like below to set values in the helper
$email_subject = $this->field1;
$email_to = $this->field2;
$email_ccto = $this->field3;

Notice: Undefined property: SomeModelCorrespondconfiguration::$field3 in...

(no errors for field1 or field2 because they we defined before we changed the query)

BUT in this case if you
- call the helper from switch statement in the model and pass the current $query to the helper
MyAdditionalHelper::getOverrides($query);
return $query;

- you can access all values of the modified query without error by loading the query result as an associative array and assigning the values as below
class MyAdditionalHelper
{
	function getOverrides($query)
	{		

		// use the query from the model to create an associative array
		$db = JFactory::getDBO();
		$db->setQuery($query);
		$result=$db->loadAssoc();
		
		// set some email overrides
		$email_subject = $result['field1'];
		$email_to = $result['field2']);
		$email_ccto=$result['field3'];

Confusing Ha??

Anyhow thanks for your time again
Last Edit: 11 Nov 2012 19:35 by BTB300.
The administrator has disabled public write access.

Re: Getting the current query in a helper v2.0 21 Nov 2012 15:21 #5570

  • admin
  • admin's Avatar
  • Online
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Is your field3 present in db ?
Is it selected in you SQL query ?

Are you under 2.0 ? With 2.0 it is much much more easy for that. In fact, it is the major improvement, so please use it, and it is much more fun, really !!

You can use a helper (better a class), to send your emails.
- This class can be initialized and loaded everywhere it needs.
- This class can maybe receive different objects types, (maybe also hinneriting a common structure)
- Depending of the initialization of this '$mailer' (for instance), depending of the context, the mail settings can override or not defaults values...

Am I answering ?

Your misuses :
Do not send SQL query to a helper. Send an object.
SQL job mustn't be realized in the same place than mailing. Separate the roles.

SQL Jobs are done in models.
The question is : What contain your SQL ?
The answer is : Customize the SQL in the prepareQuery() function of the model (item, or collection. Front or back)

Then, about prepareQuery(), we have talked a lot here.
- You can reuse a default loading (appling the complete features and ACL securities, and so ...)
or - You can customize the default query (merge) depending on STATES VARS
or - You can create a new context var. Generally 'view.layout', but it can be what you want.
or - Merge all these possibilities
or - Catch specific and recreate a raw call
or - Clean up everithing and create a complex algorithm.

All this only in one unique function for forgiving anything when you merge SQL and call the same table in different places. Now you code is really clean, and every feature you add to your table (wizards : checked_out, access, etc...) can apply everywhere.

And fortunatly, through the more simple way you can find ;-)

EDIT : By the way, when you use addSelect(), addJoin(), ..., then the system is creating unique statements, so the possible redondency of requirements or selects not not kill the SQL.
This is not a stupid fix, it is ABSOLUTLY required when you start to deal with complex structures, like the languages features I am preparing.
Coding is now a piece of cake
Last Edit: 21 Nov 2012 15:27 by admin.
The administrator has disabled public write access.
The following user(s) said Thank You: BTB300

Re: Getting the current query in a helper v2.0 21 Nov 2012 18:26 #5593

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Hi @BTB3000,

Might it be 'cleaner' for you to trigger the emails you want to send with an email plugin? This might offer a more extensible component as you further develop it and also allow other developers to tap into your 'events'.

Just playing devil's advocate. If you've already considered this then please just ignore my comment.

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.
The following user(s) said Thank You: BTB300

Re: Getting the current query in a helper v2.0 21 Nov 2012 18:38 #5594

  • BTB300
  • BTB300's Avatar
  • Online
  • Moderator
  • Posts: 414
  • Thank you received: 130
  • Karma: 46
Thanks Admin for the detailed feed back as always it is appreciated
And your answer is spot on!

Yes V2.0 (always)

As for your question about field3
- field3 is present in the database
- field3 is selected by the a.* ?
"SELECT a.field1, a.field2, ... , ... FROM main_table ...."

// only changed the above to the following
"SELECT a.*, ... , ... FROM main_table ...."

// didn't see any field name aliases in the SQL string such as
"SELECT a.*, a.field3 AS 'A Field Name', ... , ... FROM main_table ...."
- why i can't access field3 using $this->field3 my only guess a typo? (will check)
After your detailed response moving towards a class instead of helper (thank you)

After working with it for a while i was over thinking some parts

when i said i needed more than is presented to the user the solution is simple
- create a layout with all of the required data
- use two bricks
- brick one => the information displayed to the user
- brick two => the rest of the information that the $mailer function needs
- download component
- remove brick two from the layout

Now all the data is available for the $mailer function and the user only sees what they are supposed to

Thanks again for the assistance
Last Edit: 21 Nov 2012 18:39 by BTB300.
The administrator has disabled public write access.

Re: Getting the current query in a helper v2.0 21 Nov 2012 20:13 #5595

  • BTB300
  • BTB300's Avatar
  • Online
  • Moderator
  • Posts: 414
  • Thank you received: 130
  • Karma: 46
audibleid wrote:
Hi @BTB3000,

Might it be 'cleaner' for you to trigger the emails you want to send with an email plugin? This might offer a more extensible component as you further develop it and also allow other developers to tap into your 'events'.

Just playing devil's advocate. If you've already considered this then please just ignore my comment.

Gez
Thanks audibleid!

Plugin why didn't i even think of that - over thinking it again

further development ? :whistle:
10,000++ point project now split into several components (what more could they want :lol: )

So much easier to maintain now its in 5-7 components but a lot of code duplication (by Cook of course)

Personally i kind of like the idea of install 7 components and its done but a headache remembering which components use which tables / fields when something needs changing

Plugin V's Class somehow I'm thinking i will be needing both

For now I can live with the overhead of duplicated code but I can see that i will need to carefully consider plugins modules in the not to distant future. But with that comes the consideration of end user understanding and ability

Take for instance my day job the owners are not what you would call "computer literate" i have tried many times to explain to them
- You need to enable the plugin for this work
- You have to use the module for that
- "hang on" you cant configure that in the component you have to use the module

A balancing act i guess we all deal with constantly

Anyhow thanks for your input
Last Edit: 21 Nov 2012 20:15 by BTB300.
The administrator has disabled public write access.

Re: Getting the current query in a helper v2.0 21 Nov 2012 22:33 #5596

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
No probs!

Of course, you can remove 'shared' models, controllers and views and shrink all 7 components into 1 manifest to install them all in one go... Probably a fair bit of planning but doable. You could even include the plugins and modules in that one manifest to make it easier for those teetering the edges of computer literacy.

I only suggested it as I'd worked out that my shopping cart component would benefit from being rationalised in this way. Specifically, creating user and email plugins to handle functionalities that would otherwise 'bloat' the main component and make the component more modular for future development!

Anyway, however you decide to proceed, I hope its not too asking as it sounds like you have a pretty heavy project on your hands! That said, I'm sure that the worst of it is over if you have integrating 7 components worked out!!! ;)

Best of luck with it...

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.
The following user(s) said Thank You: admin
  • Page:
  • 1
  • 2
Time to create page: 0.153 seconds

Get Started