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

TOPIC:

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

  • MorganL
  • MorganL's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
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

Please Log in or Create an account to join the conversation.

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

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();
The following user(s) said Thank You: admin, MorganL

Please Log in or Create an account to join the conversation.

Last edit: by Romkabouter.

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

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 986
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

Please Log in or Create an account to join the conversation.

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

  • MorganL
  • MorganL's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
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

Please Log in or Create an account to join the conversation.

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

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 following user(s) said Thank You: MorganL

Please Log in or Create an account to join the conversation.

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

  • MorganL
  • MorganL's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
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

Please Log in or Create an account to join the conversation.

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

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;

Please Log in or Create an account to join the conversation.

Last edit: by Romkabouter.

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

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

Please Log in or Create an account to join the conversation.

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

  • MorganL
  • MorganL's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
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

Please Log in or Create an account to join the conversation.

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

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 following user(s) said Thank You: MorganL

Please Log in or Create an account to join the conversation.

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

  • MorganL
  • MorganL's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
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

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
Time to create page: 0.127 seconds
  let me say what a brilliant app! Thank you very very much for all the hard work to build Cook!
Tocpe (Forum)

Get Started