Welcome, Guest
Username: Password: Remember me

TOPIC: Pull in model data, not an Xref

Pull in model data, not an Xref 20 Apr 2015 07:42 #12911

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 16
I need to display some data in my view that is not a Xref, ie not linked by foreign key.

How is this achieved

IE primary model and view is called locations

The secondary model I with to pull in and display is called periods, I just want all the data

Thanks in advance for any assistance
Morgan Leecy MCSE

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

Pull in model data, not an Xref 13 May 2015 11:35 #12977

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
You can get your second model in the view like this (or something like this):
$modelPeriods = CkJModel::getInstance('periods', YourComponentModel');

then do
$periods = $modelPeriods ->getItems();
Last Edit: 13 May 2015 11:36 by Romkabouter.
The administrator has disabled public write access.
The following user(s) said Thank You: admin, MorganL

Pull in model data, not an Xref 14 May 2015 18:24 #12990

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

Argh... This basic should be in docs.

I promise to write some docs after the coming release.
K++
Coding is now a piece of cake
The administrator has disabled public write access.

Pull in model data, not an Xref 14 May 2015 19:43 #12991

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 16
OK that worked superbly. I assume there is a way of using that call to point at a specific model function defined within the periods model itselfl? i.e
$modelPeriods = CkJModel::getInstance('periods', YourComponentModel', 'custom.periodmodel');

or similar?
Morgan Leecy MCSE

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

Pull in model data, not an Xref 15 May 2015 08:03 #12992

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
I don't know, I don't think the $config argument in getInstance can be used to call a function, but I have never tried.
If you want to call a function, use $modelPeriods->yourfunction();
The administrator has disabled public write access.
The following user(s) said Thank You: MorganL

Pull in model data, not an Xref 15 May 2015 09:04 #12994

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 16
OOps.. I just read my question and got it completely wrong. I am not after calling a function but adding a custom query in the prepareQuery function
protected function prepareQuery(&$query, $pk)
	{
		$acl = RtambassadorHelper::getActions();
		//FROM : Main table
		$query->from('#__rtambassador_rotas AS a');

		//IMPORTANT REQUIRED FIELDS
		$this->addSelect(	'a.id,'
						.	'a.created_by');

		switch($this->getState('context', 'all'))
		{
			case 'rota.rota':

So if I want to add a case of 'rota.mycustomquery' and call it with the CkJModel::getInstance( code is that possible?
Morgan Leecy MCSE

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

Pull in model data, not an Xref 16 May 2015 10:08 #12999

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
Are you talking about joining another table?

Then you can add a case like this:
case 'rota.mycustomquery':
 $this->addJoin('`#__yourcomponent_yourtable` AS _link_ ON _link_.id = a.keyfield', 'LEFT');
break;
Last Edit: 16 May 2015 10:09 by Romkabouter.
The administrator has disabled public write access.

Pull in model data, not an Xref 18 May 2015 13:56 #13068

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
And yes, you can use the CkJModel::getInstance also.
For instance:
case 'rota.mycustomquery':
   $modelPeriods = CkJModel::getInstance('periods', YourComponentModel);
   $periods = $modelPeriods->getItems();
   var_dump($periods);
break;

$periods should give you all the items from the Periods model :)
With that, you can code whatever you need
The administrator has disabled public write access.

Pull in model data, not an Xref 18 May 2015 17:36 #13071

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 16
Not quite

When I use
$modelPeriods = CkJModel::getInstance('periods', YourComponentModel');

$periods = $modelPeriods ->getItems();

it brings through then ENTIRE model, every single record from periods. Hence I would like (in the periods model) to create a custom case in prepareQuery that filters the information i need and call THAT custom case using the CKjModel in the locations view.

Summary - I want to pull though the periods, but I dont need all of them so need to be able to filter them
Morgan Leecy MCSE

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

Pull in model data, not an Xref 18 May 2015 18:51 #13073

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
I understand your question now (I think).
If you want to pull data from periods in another model, your solultion can be simpler.

You can still use
$modelPeriods = CkJModel::getInstance('periods', YourComponentModel);
And then use
$modelPeriods->addWhere("yourfield=needvalue");
to only get the records you want.
"yourfield=needvalue" is added as regular SQL querystring. (i.e.: "id in (2,5,6)") is added to the SQL, can be anything you need.

You cán set up an extra case in the prepareQuery of the periods model, but then you should set the state to 'periods.customquery' before calling getItems on the periodmodel. Because switch($this->getState('context', 'all')) is checked in prepareQuery
After that, set it back to what is was.
I think that is much more complicated then need be.

Does that make any sense?
The administrator has disabled public write access.
The following user(s) said Thank You: MorganL

Pull in model data, not an Xref 18 May 2015 19:00 #13074

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 16
Perfect sense, and so obvious I just slapped myself around the head.

Many thanks, question finally answered =)
Morgan Leecy MCSE

Novell / Linux
PHP. MYSQL, Apache, node.js
Coldfusion, JQuery, HTML5
Joomla
The administrator has disabled public write access.
Time to create page: 0.102 seconds

Get Started