Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed missing env options in admin context #402

Closed
wants to merge 6 commits into from
Closed

Fixed missing env options in admin context #402

wants to merge 6 commits into from

Conversation

hbugdoll
Copy link
Member

@hbugdoll hbugdoll commented Jan 19, 2024

What are you changing/introducing

  • Fixed the passed "page object" for admin context, so env option 'pageObject' is available
  • Added env options 'index', 'itemsCount', 'isFirst', 'isLast', 'isPrevEqual', 'isNextEqual' and 'equalIndex' for admin context in the block iteration in getPlaceholder() analog to renderPlaceholderRecursive()
  • Multiple return type for NavItemPage::getBlockItem()

What is the reason for changing/introducing

QA

Q A
Is bugfix? yes
New feature? no
Breaks BC? no
Tests pass? yes
Fixed issues Please let the PR open!

@hbugdoll
Copy link
Member Author

hbugdoll commented Jan 19, 2024

I can't unterstand the rector error 😳
(all local changes are committed)
Edit: Ah, now I see the problem... git-auto-commit-action hung on the uncommitted rector changes.

@nadar
Copy link
Member

nadar commented Jan 21, 2024

We should preload this relations, otherwise this will strongly increase db requests and memory usage.

@hbugdoll
Copy link
Member Author

We should preload this relations, otherwise this will strongly increase db requests and memory usage.

To avoid repeating getNavItem() (with same returned nav item) for every block of a page item?

- Removed unnecessary @param's
- Added multiple possible @return types
- Added missed negation in a comment
- Reduced database access
@hbugdoll
Copy link
Member Author

hbugdoll commented Jan 21, 2024

The NavItem of a NavItemType doesn't change on object lifetime. Is this assumption correct?
So only one database access should be sufficient (73c5072).
Edit: I've reverted that commit.

- Added env options in block iteration in `getPlaceholder()`
- Added optional argument `$envOptions` for `getBlockItem()`
- Ternary operator for env option `'equalIndex'`
@nadar
Copy link
Member

nadar commented Jan 22, 2024

Thanks for all the work, sorry for late and bad reply. There is no need for private variable in a yii2 relation

if you define $this->hasOne() relation in a method:

public function getNavItem()
    {
        return $this->hasOne(NavItem::class, ['nav_item_type_id' => 'id'])->where(['nav_item_type' => static::getNummericType()]);
    }

then you have to use $object->navItem, this will establish the relation and its already stored, so a second call to $object->navItem will not result in a second query.

what i meant is that this above relation should be preloaded using ->with(['navItem]) at the point where all blocks are iterated, maybe its already the case.

src/base/NavItemType.php Outdated Show resolved Hide resolved
if (!$blockItem->block) {
return false;
}

$blockObject = $blockItem->block->getObject($blockItem->id, 'admin', $navItemPage);
$blockObject = $blockItem->block->getObject($blockItem->id, 'admin', $navItemPage->getNavItem());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this won't pass the relation, this will pass an active query instance, not an active record. So if you like to pass the nav item object, then you have to use navItemPage->navItem. There is no unit tests for this, then this would fail here :-)

Copy link
Member Author

@hbugdoll hbugdoll Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused because of two reasons.
The guide https://luya.io/guide/cms/blocks.html#env-context-information says

pageObject: Returns the luya\cms\models\NavItem object where you can run luya\cms\models\NavItem->getNav() to retrieve the luya\cms\models\Nav object.

And for the frontend context getNavItem() is already used in NavItemPage::renderPlaceholderRecursive():

$blockObject = Block::createObject($placeholder['class'], $placeholder['block_id'], $placeholder['id'], 'frontend', $this->getNavItem());

The pageObject should anyway be the same in frontend and admin context, so we should use $navItemPage->navItem resp. $this->navItem.

src/models/NavItemPage.php Show resolved Hide resolved
@nadar
Copy link
Member

nadar commented Jan 22, 2024

in general, i think we need a unit tests for this huge changeset.

@hbugdoll
Copy link
Member Author

in general, i think we need a unit tests for this huge changeset.

Of course!

then you have to use $object->navItem, this will establish the relation and its already stored, so a second call to $object->navItem will not result in a second query.

what i meant is that this above relation should be preloaded using ->with(['navItem]) at the point where all blocks are iterated, maybe its already the case.

I should have read https://www.yiiframework.com/wiki/834/relational-query-lazy-loading-and-eager-loading-in-yii-2-0#eager-loading before.

But at the point in question (NavItemPage::getPlaceholder()) the query is on NavItemPageBlockItem which doesn't have getNavItem(), so ->with(['navItem']) fails.

Would it be sufficient to store the ActiceRecord in a variable before the block iteration?

foreach ($nav_item_page_block_item_data as $blockItem) {
$item = self::getBlockItem($blockItem, $navItemPage);

$nav_item_page_block_item_data = NavItemPageBlockItem::find()...

$navItem = $navItemPage->navItem;

foreach ($nav_item_page_block_item_data as $blockItem) {
    $item = self::getBlockItem($blockItem, $navItemPage, $navItem);
    ...

@nadar
Copy link
Member

nadar commented Jan 25, 2024

i have quickly created a PR with just the required parts for your code, thanks!

#403

can you check it quickly maybe?

@hbugdoll
Copy link
Member Author

i have quickly created a PR with just the required parts for your code, thanks!

#403

Thank you.

can you check it quickly maybe?

See #403 (review).

@nadar
Copy link
Member

nadar commented Jan 25, 2024

lets continue with my PR and we take care of each problem by seperate pr. agree? 👍

@hbugdoll
Copy link
Member Author

lets continue with my PR and we take care of each problem by seperate pr. agree? 👍

Agreed!

@nadar
Copy link
Member

nadar commented Feb 5, 2024

Thank again. The PR with the env options have been merged into the master. Problem with page object still exists.

@nadar nadar closed this Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants