Jump to content

$mypage->children vs $pages->get($mypage->id)->children


valan
 Share

Recommended Posts

Recently I've discovered unexpected (for me) behavior of Page children property. 

My scrip creates several pages - $mypage and its few child pages. Within the same script this code "$mypage->children" returns empty PageArray, while this code "$pages->get($mypage->id)->children" returns expected children PageArray.

Is there any way to stay with "$mypage->children" syntax? E.g. may be I miss some step during Page creation that properly initializes this property? 

Link to comment
Share on other sites

А можешь подробнее описать, что именно не так, по твоему?

Просто не очень понимаю откуда именно у тебя берется $mypage, просто мне когда нужна специфичная страница, изначально всегда её объявляю в шапке ( $homepage = $pages->get('/') ) и дальше использую. Все объекты с данным классом и все их методы отрабатывают как положено, не смотря на контекст, даже есть пример с рекурсией в файлах страницы через которые можно получить страницы на которых они объявлены, что бы вновь получить список файлов.

Link to comment
Share on other sites

MPP, thanks for reply!

$mypage it is not PW api variable :rolleyes:. It is created in the php script as below:

$mypage = new Page();
$mypage->template = 'mytemplate';
$mypage->parent = 'path-to-mypage';
$mypage->name = uniqid();
$mypage->of(false);
$mypage->myproperty = $somevalue;
$mypage->save();
$mypage->of(true); 
Its child Pages are created in the same way after $mypage.
 
Problem in brief: in the same script $mypage->children doesn't work (returns empty PageArray), while $pages->get($mypage->id)->children works (returns non-empty PageArray with all newly created children Pages).
Link to comment
Share on other sites

Maybe you can also let us know what PW version, and where this script is run: Template, Module, Bootstrap ...

This works as expected for me. Though possible there was some fix. 

I could image the problem has to do with pages in cache/memory vs pages queried from DB.

Once you create a new page and add children to it, the $mypage page is already in memory and $mypage->children() may not return any children cause there was none when you created the page. Though it works for me so looks like children() makes a DB query after all.

BTW you don't need $page->of(false), because a newly created page via API has no output formatting yet.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...