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

TOPIC: populateObjects(&$item) Not loading 3rd level item

populateObjects(&$item) Not loading 3rd level item 22 Dec 2012 12:37 #6220

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Hi @admin,

I'm just working on the frontend of my component at the moment, specifically, creating the section, category and item views. I have this relationship setup; Section -> Category -> Inventory.

What I'm trying to do is create a sort of Section Blog layout where viewing section1 will display all related categories. Also, inside of those categories, I should also be grabbing all of the related items (inventory) too.

So, to do this I've written the following functions and added them to the relevant ITEM models:
SECTION MODEL - grab all related categories
/**
* Prepare related categories and add them to @item
*
* @access	public
* @param	Item	&$item
*
* @return	void
*
* @since	1.6
**/
public function populateObjects(&$item)
{
	$modelCategories = JModel::getInstance('Categories', 'JstoresocialModel');
	$modelCategories->addWhere('a.section = ' . (int)$item->id);
	$item->categories = $modelCategories->getItems();
	
	//Debug - Uncomment following
	echo "<pre>"; print_r($item); echo "</pre>";
}
CATEGORY MODEL - grab all related inventory items
/**
* Prepare related categories and add them to @item
*
* @access	public
* @param	Item	&$item
*
* @return	void
*
* @since	1.6
**/
public function populateObjects(&$item)
{
	$modelInvitems = JModel::getInstance('Inventory', 'JstoresocialModel');
	$modelInvitems->addWhere('a.category = ' . (int)$item->id);
	$item->inventoryitems = $modelInvitems->getItems();

	//Debug - Uncomment following
	echo "<pre>"; print_r($item); echo "</pre>";
}
The Issue
Firstly, in themselves, they both work fine; viewing a section does in fact output all related categories and viewing a category displays all of the related items.

However, when viewing a section, I was expecting every category related to the section to also contain inventory items too because of the unlimited cascades and as I am not currently setting any state var.

Am I missing something? Any help/pointers would be massively appreciated!!!

Many thanks,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
Last Edit: 22 Dec 2012 12:39 by JoomGuy.
The administrator has disabled public write access.

Re: populateObjects(&$item) Not loading 3rd level item 22 Dec 2012 13:05 #6222

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
populateObject() is an excellent title to get my attention.

Thanks for asking.

This function has been designed to populate N-M or N-1 lists objects as you are doing.

I cannot see what is missing...

Is populateObject() called in the second cascad call ? (Try to dump)
if not, call it from your first call.

Should work... Strange.
Guess you will find.

Important note :
populateObject is always called, even for short lists...
So, please encapsulate the loading in a check on a state var ('xref.categories' for example)
And : set up this state var to true from the calling view, in order to organize better you code.

Do not forget, in the cascad call, to set up the 'xref..' state var as well.

But for the moment to make it working, you can make your tests without.

EDIT : I read again, and realize that you had voluntary removed the states vars. :blush:

Good luck, and if you cannot find... toc toc admin ok.
Coding is now a piece of cake
Last Edit: 22 Dec 2012 13:11 by admin.
The administrator has disabled public write access.

Re: populateObjects(&$item) Not loading 3rd level item 22 Dec 2012 13:13 #6225

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Hi @admin thanks for the quick response!
Is populateObject() called in the second cascad call ? (Try to dump)
if not, call it from your first call.
Yes, I'm already debugging using print_r() in my category model on inventory items. I can see the related items in category view, but not one level up in sections.

Thanks,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: populateObjects(&$item) Not loading 3rd level item 22 Dec 2012 13:22 #6226

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Here's what I get from printing the section;
stdClass Object
(
    [id] => 2
    [published] => 1
    [alias] => wildlife
    [description] => 
    [image] => 
    [ordering] => 2
    [title] => Wildlife
    [params] => JObject Object
        (
            [_errors:protected] => Array
                (
                )

            [access-view] => 1
        )

    [categories] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 4
                    [published] => 1
                    [params] => JObject Object
                        (
                            [_errors:protected] => Array
                                (
                                )

                            [access-view] => 1
                        )

                    [title] => Birds
                    [alias] => birds
                    [image] => 
                    [description] => 
                    [section] => 2
                    [ordering] => 1
                )

            [1] => stdClass Object
                (
                    [id] => 5
                    [published] => 1
                    [params] => JObject Object
                        (
                            [_errors:protected] => Array
                                (
                                )

                            [access-view] => 1
                        )

                    [title] => Dolphins
                    [alias] => dolphins
                    [image] => 
                    [description] => 
                    [section] => 2
                    [ordering] => 2
                )

            [2] => stdClass Object
                (
                    [id] => 6
                    [published] => 1
                    [params] => JObject Object
                        (
                            [_errors:protected] => Array
                                (
                                )

                            [access-view] => 1
                        )

                    [title] => Seahorses
                    [alias] => seahorses
                    [image] => 
                    [description] => 
                    [section] => 2
                    [ordering] => 3
                )

            [3] => stdClass Object
                (
                    [id] => 7
                    [published] => 1
                    [params] => JObject Object
                        (
                            [_errors:protected] => Array
                                (
                                )

                            [access-view] => 1
                        )

                    [title] => Mini Animals
                    [alias] => mini-animals
                    [image] => 
                    [description] => 
                    [section] => 2
                    [ordering] => 4
                )

            [4] => stdClass Object
                (
                    [id] => 8
                    [published] => 1
                    [params] => JObject Object
                        (
                            [_errors:protected] => Array
                                (
                                )

                            [access-view] => 1
                        )

                    [title] => Dragonflies
                    [alias] => dragonflies
                    [image] => 
                    [description] => 
                    [section] => 2
                    [ordering] => 5
                )
        )
)
As I said, I was expecting each of the [categories] elements to contain an [inventoryitems] element too. Just to clarify this, I am getting the inventoryitems from the category view.

Thanks for all the additional pointers with the state vars/calls!!!

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: populateObjects(&$item) Not loading 3rd level item 22 Dec 2012 14:46 #6237

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Can't work this one out - As I said it works to get sub (one-level related) items. Just not sub-sub-items.

What am I missing??? :unsure:

Thanks,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: populateObjects(&$item) Not loading 3rd level item 26 Dec 2012 09:24 #6273

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Still not been able to get 2nd level results into object.

Any ideas?

Thanks,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: populateObjects(&$item) Not loading 3rd level item 03 Jan 2013 11:45 #6341

  • molenwal1
  • molenwal1's Avatar
  • Offline
  • Senior Member
  • Posts: 63
  • Thank you received: 17
  • Karma: 3
Hi Audibleid, without looking into too much details, i wondered the following in the category model:
$modelInvitems->addWhere('a.category = ' . (int)$item->id);

What is the value of the item->id. Is it the id of the category, or does it contain somehow the id of the section?
Molenwal1 Webdesign
www.molenwal1.nl
The administrator has disabled public write access.

Re: populateObjects(&$item) Not loading 3rd level item 03 Jan 2013 11:56 #6343

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Hi @molenwal1,

$item->id in the category model is the id of the category, yes... The section is stored in the category table as FK (section).

What you thinking?

Cheers for the reply!

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: populateObjects(&$item) Not loading 3rd level item 03 Jan 2013 12:59 #6345

  • molenwal1
  • molenwal1's Avatar
  • Offline
  • Senior Member
  • Posts: 63
  • Thank you received: 17
  • Karma: 3
I thought that maybe there is a glitch that it contains the section id number and not the category id number and for that reason there are no inventory items found. I don't know if you have tested the value of the id.
Molenwal1 Webdesign
www.molenwal1.nl
The administrator has disabled public write access.

Re: populateObjects(&$item) Not loading 3rd level item 03 Jan 2013 13:09 #6346

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Yep, it all works fine when getting the first related item - it just doesn't work at the next level.

Thanks,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: populateObjects(&$item) Not loading 3rd level item 15 Jan 2013 05:20 #6450

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

I don't know if this is the correct solution but I added $model->populateObjects($item); to the foreach loop of the populateParams function in the com_myproject/classes/jmodel.list.php file to solve this issue.

The jmodel.list.php and jmodel.item.php files currently have empty "populateObjects" functions.

Please let me know if you have found a better way to solve this issue.

Thanks
vlemos


if ($model)
{
if ($model->canView($item))
$item->params->set('access-view', true);

if ($model->canEdit($item))
$item->params->set('access-edit', true);

if ($model->canDelete($item))
$item->params->set('access-delete', true);

if (!$model->canCheck($item))
$item->params->set('tag-checkedout', true);

$model->populateObjects($item);

}
Last Edit: 15 Jan 2013 05:26 by vlemos.
The administrator has disabled public write access.
The following user(s) said Thank You: JoomGuy

Re: populateObjects(&$item) Not loading 3rd level item 15 Jan 2013 08:24 #6451

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Hi @vlemos,

Thanks for your reply...

I'm working on a few other bits and pieces today but will try and check that later! Thanks again!

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: populateObjects(&$item) Not loading 3rd level item 15 Jan 2013 10:50 #6453

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
I want to check that as well to furnish you the good answer.

The thing is now I cannot uprade because the new comming version is changed a lot and I must funish it before to upgrade next Cook version.
Coding is now a piece of cake
The administrator has disabled public write access.
The following user(s) said Thank You: JoomGuy

Re: populateObjects(&$item) Not loading 3rd level item 15 Jan 2013 15:42 #6458

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

Sorry for answering late.

There are TWO different places for populateObjects() : ITEM and COLLECTION

For you first, if you start in the ITEM model, write in the ITEM model,
Then, for the second and all related, write in the COLLECTION :
public function populateObjects(&$items)
{
	if ($this->getState('xref.categories'))
	{
		foreach($items as $item)
		{
			$model = JModel::getInstance('Categories', 'XxxxModel');

//Set the cascade state DO NOT FORGET !! (inventoryitems for you)
			$model->setState('xref.inventoryitems', true);
			
			$model->setState('filter.section', $item->id);

			$item->categories = $model->getItems();
		}
	}

}
Coding is now a piece of cake
Last Edit: 15 Jan 2013 15:44 by admin.
The administrator has disabled public write access.
The following user(s) said Thank You: vlemos, JoomGuy

Re: populateObjects(&$item) Not loading 3rd level item 15 Jan 2013 16:18 #6461

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Of course!!! Thanks Monsieur!
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Time to create page: 0.138 seconds

Get Started