Welcome, Guest
Username: Password: Remember me

TOPIC: Only view and access items owned by the user

Only view and access items owned by the user 26 Sep 2017 15:43 #15316

  • dyvel
  • dyvel's Avatar
  • Offline
  • Elite Member
  • Posts: 200
  • Thank you received: 11
  • Karma: 10
I have created a table where I've added a FK to a Joomla user. In frontend collection and item view I want only to display items belonging to the user selected for the item.
I'm thinking I have to modify the populateState function in the model for the 2 views, but unsure about the rest :D
Any help is greatly appreciated :-)
The administrator has disabled public write access.

Only view and access items owned by the user 26 Sep 2017 16:27 #15317

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Simply... a filter... lol
(Maybe I didn't got it)


not in populateState()

better in : prepareQuery(),

or even better : ormLayoutXxx()

But if you want a persistant filter for all layouts of this model, then prepareQuery() is the best place.


Hope it helps, but i may not understood, because I guess you know this already, you are not a neebie
Coding is now a piece of cake
The administrator has disabled public write access.

Only view and access items owned by the user 26 Sep 2017 16:31 #15318

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
And you can force the value of the filter with some data coming from where you want.

in orm declaration it is :
'value' => $myValue
(inside the proper filter orm declaration.)
Coding is now a piece of cake
The administrator has disabled public write access.

Only view and access items owned by the user 26 Sep 2017 17:13 #15319

  • dyvel
  • dyvel's Avatar
  • Offline
  • Elite Member
  • Posts: 200
  • Thank you received: 11
  • Karma: 10
I think I'm the one who doesn't get it :D
But yes, it's a persistent "filter" I'm after.

What I'm looking to do is to create a view, where the user can only see stuff that is connected to the user, so each user has it's own items.

Looking at prepareQuery I have this orm array
		$this->orm(array(
			'select' => array(
				'checked_out',
				'created_by',
				'published'
			),
			'access' => array(

				// ACCESS : Restricts accesses over the local table
				'a' => array(
					'publish' => 'published',
					'author' => 'created_by'
				)
			)
		));

I don't fully understand the ORM concept yet, so I'm unsure how to implement a "WHERE a.user = $userid" to filter the result.
The administrator has disabled public write access.

Only view and access items owned by the user 26 Sep 2017 17:30 #15320

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
For a basic filter :
$this->orm(array(
	'select' => array(
		'checked_out',
		'created_by',
		'published'
	),
	'access' => array(

		// ACCESS : Restricts accesses over the local table
		'a' => array(
			'publish' => 'published',
			'author' => 'created_by'
		)
	),
	
//HERE	
	'filter' => array(
		'fk_user' => array(
			'value' => $theCurrentUserId
		)
	)
));
You can also write this in a new $this->orm(..); call (for identifiyng your forks it is practical)

Then, about the 'acces' filter, it is only used for authoring, publishing, and viewlevels.
At the moment, use a simple filter as I told you. Then, you can need the 'access' orm feature in some cases.

I try to explain...
actualy, the access feature has two reasons to be :
First purpose: When you are using the 3 possible directives (authoring, publishing, and viewlevels), then you get a special filter that is a combinaison of the three. Some allow, other restricts.
Second purpose : Propagation. It is used when the local items are linked to foreign table with filters on it. The basic example is to show 'articles' only from the filtered 'categories'. Then in the article query, you get a very complex query (see in com_content).
In your case, you can eventualy need this propagation, but you can also do it with simple filter, the same as previous, simply you filter over 'mytable.fk_user' as filter name.
ORM is really great when you start to use it. (better than JDom, lol)
Coding is now a piece of cake
The administrator has disabled public write access.
The following user(s) said Thank You: dyvel

Only view and access items owned by the user 26 Sep 2017 18:03 #15321

  • dyvel
  • dyvel's Avatar
  • Offline
  • Elite Member
  • Posts: 200
  • Thank you received: 11
  • Karma: 10
Thank you! :-)

Got my ORM modified like this
		$this->orm(array(
			'select' => array(
				'checked_out',
				'created_by',
				'published'
			),
			'access' => array(

				// ACCESS : Restricts accesses over the local table
				'a' => array(
					'publish' => 'published',
					'author' => 'created_by'
				)
			),
			'filter' => array(
		        'user' => array(
		            'type' => 'string',           // Mandatory
		            'value' => $user->id         // Force the filter value (otherwise, the value is read from the filter state var)
		        )
		    )
		));

It works, filtering items by the current logged in user. However, if the user is not logged in (e.g. user id = 0), then all items is displayed instead of none.
Do we have a solution for this case ?
The administrator has disabled public write access.

Only view and access items owned by the user 26 Sep 2017 18:05 #15322

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Then limit the access of that page when user is not logged.
If you don't expects any result, then you even don't need to call SQL
Coding is now a piece of cake
The administrator has disabled public write access.

Only view and access items owned by the user 26 Sep 2017 21:04 #15323

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
I suggest using
'value' => ($user->id > 0) ? $user->id : -1
I think that works, but otherwise use above the $this->orm
$currentUserId = ($user->id > 0) ? $user->id : -1;
and then
'value' => $currentUserId
Mind the semicolpn!
Last Edit: 26 Sep 2017 21:05 by Romkabouter.
The administrator has disabled public write access.
The following user(s) said Thank You: admin, dyvel

Only view and access items owned by the user 26 Sep 2017 21:10 #15324

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
I was thinking another one
What about
(string)((int)$user->id)
If the zero is sent as a string, maybe it is not ignored for the ORM filter.
Often the filters lacks with zero value, and I often use this trick.
(I really love PHP, you can never do things like that in C#)

I mean, if you need to load SQL query when not logged (maybe some items are not private and fk = 0...)
Coding is now a piece of cake
The administrator has disabled public write access.

Only view and access items owned by the user 27 Sep 2017 06:59 #15325

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
admin wrote:
(I really love PHP, you can never do things like that in C#)
Well, actually you can and very easy B)
The administrator has disabled public write access.

Only view and access items owned by the user 27 Sep 2017 08:04 #15326

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Si I know...

I meant, when a var is formated to be integer type (such as this filter), then you can parse to string, but it will never fit into a int type. You get it ?
Coding is now a piece of cake
The administrator has disabled public write access.

Only view and access items owned by the user 04 Oct 2017 17:26 #15341

  • dyvel
  • dyvel's Avatar
  • Offline
  • Elite Member
  • Posts: 200
  • Thank you received: 11
  • Karma: 10
Awesome. Thanks guys :-)

I ended up with
$user = JFactory::getUser();
$userid = ($user->id > 0) ? $user->id : -1;

Not sure why, but the other solutions was not working for me.
Last Edit: 04 Oct 2017 17:26 by dyvel.
The administrator has disabled public write access.
Time to create page: 0.144 seconds

Get Started