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

TOPIC: [HELP HOWTO] form for a specific record

[HELP HOWTO] form for a specific record 04 Jul 2012 10:09 #2753

  • zutiste
  • zutiste's Avatar
  • Offline
  • New Member
  • Posts: 18
  • Thank you received: 1
  • Karma: 3
Hello,

I'd like to display a record as a form using a specific value (user id).
Do I have to code the constraint or is there any way to do it directly in Cook?


My application let the user register himself and I'd like to let him go back to its record using a link to the form.
Last Edit: 14 Oct 2012 09:54 by admin.
The administrator has disabled public write access.

Re: form for a specific record 04 Jul 2012 11:23 #2755

  • admin
  • admin's Avatar
  • Online
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
TODO :

In the item model, 'where' statement :
->Instead of filtering in cid coming from the url, filter on the user id key with the user id value.
(If I remember well, this is maybe in the common super class, so you should override it in your specific concerned model.)


Arghh, in the V2.0, this is much more easy to do.
When I look at the model source code, it is sooo dirty.

I say this because I have a case of consciousness every time I tell the people to write in models !!!
And also because with the coming of the V2.0, you will should rewrite your added code in the model.
Coding is now a piece of cake
The administrator has disabled public write access.

Re: form for a specific record 04 Jul 2012 11:37 #2757

  • zutiste
  • zutiste's Avatar
  • Offline
  • New Member
  • Posts: 18
  • Thank you received: 1
  • Karma: 3
Thank you for your answer.

What I did :
in the view.html.php :
$model->addWhere	("a.user = ".$user->id);
and in the jmodel.item.php ;
function addWhere($where_clause)
{
	$this->_where[] = $where_clause;
}

Is that what you meant?
The administrator has disabled public write access.

Re: form for a specific record 04 Jul 2012 11:50 #2758

  • admin
  • admin's Avatar
  • Online
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Yes, good code. but I am not sure if it works.
Because the id from the url is still filtered until you desctivate it in your model (for that specific view / layout)

So it will result a double where filter. For example, if the cid = 0 (or wrong value), it should block.
Coding is now a piece of cake
The administrator has disabled public write access.

Re: form for a specific record 04 Jul 2012 11:57 #2759

  • zutiste
  • zutiste's Avatar
  • Offline
  • New Member
  • Posts: 18
  • Thank you received: 1
  • Karma: 3
admin wrote:
Yes, good code. but I am not sure if it works.
Because the id from the url is still filtered until you desctivate it in your model (for that specific view / layout)
Indeed, it doesn't work. Where's the part of the model where I can desactivate the filter ?admin wrote:
So it will result a double where filter. For example, if the cid = 0 (or wrong value), it should block.
I get a form for a new record.
The administrator has disabled public write access.

Re: form for a specific record 10 Jul 2012 12:15 #2772

  • admin
  • admin's Avatar
  • Online
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
It is a bit tricky. Because it is in the main model item class.
So your changes will affect all models calls.

You can create an override of the function in your concerned model. But be careful to apply these changes ONLY for the concerned layout call.


If you are not in hurry, wait for the V2.0 for that it will be much more easy for you.
Coding is now a piece of cake
The administrator has disabled public write access.

Re: form for a specific record 15 Jul 2012 13:51 #2793

  • BTB300
  • BTB300's Avatar
  • Online
  • Moderator
  • Posts: 414
  • Thank you received: 130
  • Karma: 46
In the mean time (waiting for v2)

- Copy the function buildQueryWhere() from your model
- paste the the original buildQueryWhere statement under the original in your model
- rename the function to where author
Add the standard joomla db check
Add the standard joomla get user

Function buildQueryWhereAuthor()
{
$db=JFactory::getDBO();
$user =JFactory::getUser();
[ original code goes here]

and Change the where
$where[] = 'a.id = '.(int) $this->_id;
to
$where[] = 'a.author = '.(int) $user->id;

}

in the
administrator/classes/jmodel.item.php
Copy the buildQueryWhere()

buildQueryWhere()
{
.........
}

Paste and rename
To buildQueryWhereAuthor()

Then where ever the you need to filter on author for that given model (or what ever value you decided to give the $where[] = 'a.id = '.(int) $user->id;)
Just change the
. $this-> buildQueryWhere() in your SQL statements
To
. $this-> buildQueryWhereAuthor()

With out fear of of losing any existing where functionality such as access filtering

it should be noted that this will only work for the specific model... you will need to do this for each model where you need to filter on a specific field

i know had to do this outside the cook environment on many many tables... would be nice to see the addWhere() and buildQuerywhere[something]() auto generated in item class and all models

And yes the addWhere in model.item class is also handy for adding additional filtering on a specific item

To admin -> can we get this added to the class and models in the mean time whilst waiting for v2 Please... ;) ???

But im not complaining cook has saved me many many hours so far (THANKS ADMIN) so and to hand code this for the moment there is no argument from me

and as always there coud be a simpler way such as a switch statement based on the current view but i will leave that up to you to explore..
Last Edit: 27 Aug 2012 11:14 by BTB300.
The administrator has disabled public write access.
The following user(s) said Thank You: admin, twev, edwardcox, e-struct

Re: form for a specific record 23 Aug 2012 17:13 #3272

  • e-struct
  • e-struct's Avatar
  • Offline
  • Senior Member
  • Posts: 76
  • Thank you received: 4
  • Karma: 5
Humm ... at the first view it's not working for me

does the "order by" may have any interaction? I don't t think so!

Instead in the classes list (jmodel.list.php) shoudn't be missing the ...WhereAuthor classe declaration ???
I added it.

Also there are two functions _buildQuery() and _buildQuery_default()
I guess both of them must be changed to:
. $this->_buildQueryWhereAuthor()

But with all above edited I have still an incomplete Query statement...
SELECT a.*
  FROM `..._mycomp_mytable` AS a
(nothing more...)

The changed where code is already under an if clause with a filter: is it fine for you ?
if (isset($this->_active['filter']) && $this->_active['filter'])

If I make a search using the search box the function is broken:
500 - JDatabaseMySQL::query: 1054 - Unknown column 'a.author' in 'where clause' SQL=SELECT a.* FROM `j25pr1_adminbackoffice1v00_resellerchoice` AS a WHERE a.author = 0 ORDER BY a.sold,a.boxsoldid LIMIT 0, 15

I would like to have my result filtered by author outside the search box... meaning that a user can see only its own records even with no search.

Any idea ?
I am using eclipse with no debugger so it's painful to debug...
Last Edit: 24 Aug 2012 05:05 by e-struct.
The administrator has disabled public write access.

Re: form for a specific record 24 Aug 2012 07:40 #3278

  • BTB300
  • BTB300's Avatar
  • Online
  • Moderator
  • Posts: 414
  • Thank you received: 130
  • Karma: 46
HI e-struct,
Yes The WhereAuthor() IS MISSING in the jmodel.list.php - you need to create it
- you need to copy the buildQuery_Where() from the jmodel.list.php
- paste buildQuery_Where() into the and jmodel.list.php
- rename buildQuery_Where() in the jmodel.list.php to buildQuery_WhereAuthor()

- you need to copy the buildQuery_Where() from the respective models mytablename.php
- paste buildQuery_Where() { } it into the respective models mytablename.php
- rename to buildQuery_WhereAuthor() { } it into the respective models mytablename.php

you will notice a series of function calls before the return $query;

such as

. build_queryWhere()
. left_join()
.
These all combine together to create the final statement

To see the full sql statement on the layout / view concerned put echo in front of the $query = 'SELECT a.* FROM
- such as this

echo $query = 'SELECT a.* FROM

you will see that the . somefunctioncall() just makes sure you dont forget someting such as access control or a predefined GROUP BY etc.


$query = 'SELECT a.*
FROM A TABLE
WHERE a.field_name=1 AND
buildQuery_WhereAuthor()

This will give ... WHERE a.field_name=1 AND a.author=$user->id

- Copy the function buildQueryWhere() from your model
- paste the the original buildQueryWhere statement under the original in your model
- rename the function to where author
Add the standard joomla db check
Add the standard joomla get user

As for filter - its in the mytablename.php
Function buildQueryWhereAuthor()
{

and Change the where
$where[] = 'a.id = '.(int) $this->_id;
to
$where[] = 'a.author = '.(int) $user->id;

}

_buildQuery_default() - is the sql statement for the default view
_buildQuery_newview() - would be the sql statement used to display the data in the layout called "newview"
_buildQuery_anotherview() - would be the sql statement used to display the data in the layout called "anotherview"
_buildQuery() does the data in the drop down / combo boxes (from memory)

Hope it helps

EDIT / Note: Another thing to note is that author depreciated
new projects use created_by instead of author
That would make it

Function buildQueryWhereAuthor()
{

and Change the where
$where[] = 'a.id = '.(int) $this->_id;
to
$where[] = 'a.created_by = '.(int) $user->id;

}

And don't forget to check the current user as described before
Last Edit: 27 Aug 2012 11:16 by BTB300.
The administrator has disabled public write access.
The following user(s) said Thank You: admin, e-struct

Re: form for a specific record 24 Aug 2012 07:57 #3279

  • admin
  • admin's Avatar
  • Online
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Arrrgh, I can't believe that...

5 months now I am developping for the V2.0, and every time I see this source code (in the model), I am disgusted...

Serious, my code is ugly (Joomla 1.5 standard), and please wait for the V2.0 (1 or 2 weeks), can you do that ?

Thank you very much for helping on that part, because, me I will not give a sh.t ;-)
Serious...
I am so sorry for all these repercussions !!

Enjoy the summer ;-)
Coding is now a piece of cake
The administrator has disabled public write access.

Re: form for a specific record 24 Aug 2012 12:36 #3286

  • BTB300
  • BTB300's Avatar
  • Online
  • Moderator
  • Posts: 414
  • Thank you received: 130
  • Karma: 46
@ Admin
No need to be sorry and do not be down on yourself, evolution takes time! the code might seem messy to yourself but just look at what Cook could do when it was first launched and look at what it can do today in such a sort period of time - All Positive

No amount of clean and tidy code can accomodate different levels of user knowledge and ability... just doing my bit to help.

I am sure there is a number of us here that understand the current structure well enough that can help others out while you keep developing v2 :cheer:
The administrator has disabled public write access.
The following user(s) said Thank You: admin

Re: form for a specific record 26 Aug 2012 17:05 #3301

  • e-struct
  • e-struct's Avatar
  • Offline
  • Senior Member
  • Posts: 76
  • Thank you received: 4
  • Karma: 5
It is not working at all because this statement
(int) $this->$user->id
always returns '0'

$where[] = 'a.author = '.(int) $this->$user->id;

so for sure the SQL query returns no records.
When, I check in the debugger (I have sucessfully set-up Xdebug :) ) the user is correctly set. In my case its is 174.

Any idea. ?
For my part I am stuck .. (in the middle of nowhere).


Last Edit: 26 Aug 2012 17:17 by e-struct.
The administrator has disabled public write access.

Re: form for a specific record 26 Aug 2012 22:05 #3302

  • edwardcox
  • edwardcox's Avatar
  • Offline
  • Premium Member
  • Here to help.
  • Posts: 131
  • Thank you received: 26
  • Karma: 12
Hi,

could you try just $user->id and see what value that returns?
i.e.
$where[] = 'a.author = '.(int) $user->id;

Edward.
Passionate Joomla! Developer and J-Cook pro evangelist.
www.jcombuilder.com - we build great Joomla!® Components so you don't have to.
Last Edit: 26 Aug 2012 22:06 by edwardcox.
The administrator has disabled public write access.

Re: form for a specific record 27 Aug 2012 02:32 #3303

  • BTB300
  • BTB300's Avatar
  • Online
  • Moderator
  • Posts: 414
  • Thank you received: 130
  • Karma: 46
Did you check the current user...

Function buildQueryWhereAuthor()
{
$db=JFactory::getDBO();
$user =JFactory::getUser();

**** It will not work unless you check current user...
always good practice to always check current user before doing anything user related

**** Does your sql statement include where author = 34 or similar number (or created_by = 34 for newer projects)

**** You have to call the buildQueryWhereAuthor() in the relevant function

such as
function usersItems()
{
$query = 'SELECT a.*
FROM A_TABLE
. someFunction()
. anotherFunction()
. .......................
. buildQuery_WhereAuthor()
. ......................
return $query;
}

Can you paste your code changes here...
AND the query that is output if you echo the query

echo $query = 'SELECT a.*
FROM A_TABLE
.....
Last Edit: 27 Aug 2012 02:51 by BTB300.
The administrator has disabled public write access.

Re: form for a specific record 27 Aug 2012 07:11 #3306

  • e-struct
  • e-struct's Avatar
  • Offline
  • Senior Member
  • Posts: 76
  • Thank you received: 4
  • Karma: 5
@edwardcox:
OK it works without $this... great !!!

@all
First thanks for your support... B)
Now suppose I want to do that outside of the search box ? Which methods am I suppose to edit ?
Does this method useful for this purpose and how to use it ?
function addWhere($where_clause)

Can you also explain the diff between those both methods
function _buildQuery()
function _buildQuery_default()
Last Edit: 27 Aug 2012 07:58 by e-struct.
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Time to create page: 0.136 seconds

Get Started