Welcome, Guest
Username: Password: Remember me

TOPIC: [SOLVED] AddWhere in Model Item

[SOLVED] AddWhere in Model Item 24 Nov 2012 07:38 #5629

  • blue-canoe
  • blue-canoe's Avatar
  • Offline
  • Senior Member
  • Posts: 57
  • Thank you received: 16
  • Karma: 7
Hello,

I am not sure if this is a big, but it is something I noticed in my generated code (so possibly it could be due to the configuration I had set up in the generator).

Anyway, I noticed that in the JModel.item.php class there is no function generated to add a where clause to the generated query (like there is in JModel.list.php). Of course this was easy to fix, by adding the following code to jmodel.item.php:
function addWhere($where_clause) {
	$this->_where[] = $where_clause;
}

I needed this to be able to filter data in some of the views.

Kind regards,

Misha
The administrator has disabled public write access.

Re: AddWhere in Model Item 27 Nov 2012 07:40 #5651

  • BTB300
  • BTB300's Avatar
  • Offline
  • Moderator
  • Posts: 415
  • Thank you received: 132
  • Karma: 47
Hi blue-canoe,

This was the same in cook 1.5 builds

as you suggested the solution is drop the function addWhere($where_clause){...} into the JModel.item.php

Then you can display item views based on such things as user by using addWhere() without having to display a collection view first something like
addWhere( $where[] = 'a.author = '.(int) $user->id;)
it could be due to the configuration I had set up in the generator).
www.j-cook.pro/forum/9-coding-inside-you...specific-record#2793
NOT relevant to Cook V2.0 (The Model Sql structure has changed drastically since then)
but it gives you some idea of what you "might need to do" if your still working with 1.5 builds
Last Edit: 27 Nov 2012 08:01 by BTB300.
The administrator has disabled public write access.

Re: AddWhere in Model Item 27 Nov 2012 09:47 #5655

  • BTB300
  • BTB300's Avatar
  • Offline
  • Moderator
  • Posts: 415
  • Thank you received: 132
  • Karma: 47
Just Checked Cook V2.0
you dont need the addWhere() function in the jmodel.item.php
to limit data by multiple values
use something similar
//SELECT : raw complete query without joins
	$query->select('a.*');
	$query->where('a.id=1');
	$query->where('a.anotherfield=2');
The administrator has disabled public write access.

Re: AddWhere in Model Item 27 Nov 2012 12:23 #5657

  • blue-canoe
  • blue-canoe's Avatar
  • Offline
  • Senior Member
  • Posts: 57
  • Thank you received: 16
  • Karma: 7
Thanks! That is good to know.
The administrator has disabled public write access.

Re: AddWhere in Model Item 28 Nov 2012 12:34 #5667

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
Exactly.

AddWhere() is not available in item model. This is a choice to force you coding well.

Because ITEM is returning only one item, so you can never mix a filter on ID and something else.

in prepareQuery() :
- Create a new SQL context.
- Receive a customized state var.
- Filter on something else than ID, write your filter statement based on this received var.
Coding is now a piece of cake
The administrator has disabled public write access.
The following user(s) said Thank You: BTB300
Time to create page: 0.127 seconds

Get Started