Welcome, Guest
Username: Password: Remember me

TOPIC: [FIXED] Retrieve a single item by ID

Retrieve a single item by ID 17 Feb 2016 04:10 #13820

  • vlemos
  • vlemos's Avatar
  • Online
  • Elite Member
  • Posts: 295
  • Thank you received: 41
  • Karma: 21
Hello Admin

In a legacy Cook component, the code below worked well to retrieve a single item by ID. Now it gives an error. Any ideas why?

Thanks in advance
vlemos

	// load linked model (addresses)
	$model = CkJModel::getInstance('demoitem', 'DemoModel');

	// Define the SQL context
	$model->setState('context', 'demoitem.demoitem');

	// Call the state explicitly
	$model->setState('demoitem.demoitem', true);

	// Complete the object by inserting the addresses into the item
	$item = $model->getItem($id);
The administrator has disabled public write access.

Retrieve a single item by ID 17 Feb 2016 12:26 #13821

  • admin
  • admin's Avatar
  • Online
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 985
  • Karma: 140
What is the error ?
Coding is now a piece of cake
The administrator has disabled public write access.

Retrieve a single item by ID 17 Feb 2016 13:28 #13822

  • vlemos
  • vlemos's Avatar
  • Online
  • Elite Member
  • Posts: 295
  • Thank you received: 41
  • Karma: 21
Sorry:

Fatal error: Call to a member function getKeyName() on boolean in . . . \demo\libraries\legacy\model\admin.php on line 950
The administrator has disabled public write access.

Retrieve a single item by ID 18 Feb 2016 09:29 #13823

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
Why do you do this?

$model->setState('demoitem.demoitem', true);
You do not even need to set any state

If you have the singular model, the only two line you need are:
$model = CkJModel::getInstance('demoitem', 'DemoModel');
$item = $model->getItem($id);

Assuming you $id has a value

You can use a state if you want:
$model = CkJModel::getInstance('demoitem', 'DemoModel');
$model->setState('demoitem.id', $id);
$item = $model->getItem();
Last Edit: 18 Feb 2016 09:31 by Romkabouter.
The administrator has disabled public write access.
The following user(s) said Thank You: admin, vlemos

Retrieve a single item by ID 18 Feb 2016 10:46 #13824

  • admin
  • admin's Avatar
  • Online
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 985
  • Karma: 140
Yes, Romkabouter, you are right.
K++

In cook, the way is to set the ID we are searching in the state var. "[itemname].id"
But getItem($id), would be nice also and I try to make it working if possible properly.

I'll check that later. For the moment, just use the state var.

You can make your own fork by yourself if you want to use the model that way.
Fork the model item superclass :
classes/model/item.php
Coding is now a piece of cake
The administrator has disabled public write access.
The following user(s) said Thank You: vlemos

Retrieve a single item by ID 18 Feb 2016 11:40 #13825

  • vlemos
  • vlemos's Avatar
  • Online
  • Elite Member
  • Posts: 295
  • Thank you received: 41
  • Karma: 21
Hello Romkabouter / Admin

If you have the singular model, the only two line you need are:
   $model = CkJModel::getInstance('demoitem', 'DemoModel');
   $item = $model->getItem($id);

Assuming you $id has a value

You can use a state if you want:
   $model = CkJModel::getInstance('demoitem', 'DemoModel');
   $model->setState('demoitem.id', $id);
   $item = $model->getItem();

I tried both of these methods before posting; they do not work anymore in my modules.

Thanks again
vlemos

Fatal error: Call to a member function getKeyName() on boolean in  ....\demo\libraries\legacy\model\admin.php on line 950
The administrator has disabled public write access.

Retrieve a single item by ID 18 Feb 2016 11:51 #13826

  • admin
  • admin's Avatar
  • Online
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 985
  • Karma: 140
Only in module ?
So it means that a file is missing in the loader.

The last change achieved on the loader is this ticket :
www.j-cook.pro/index.php/forum/18-closed...ing-in-joomla-module
You remember for sure because YOU fixed it. lol.

Guess you don't have problem when the module appears on the page of the component where all dependencies are loaded.

getKeyName()
Is called on a Table object. Search that way.


Try to force loading (include_once) of the concerned missing class in the component loader.
Or at least some explicit XxxLoader::register() furnishing the class name and the full file path.

Hope it helps.

In case... same component than previous ticket ?
Coding is now a piece of cake
The administrator has disabled public write access.
The following user(s) said Thank You: vlemos
Time to create page: 0.092 seconds

Get Started