Jump to content

SwimToWin

Members
  • Posts

    95
  • Joined

  • Last visited

Everything posted by SwimToWin

  1. Thanks all. I have summarized the solution suggested by Soma in two examples (for future reference): Example 1: Simple example that lists your related yourpagefield articles - first pages referred by this page, next pages that refer this page. <ul> <?php foreach($page->yourpagefield as $k=>$p) { echo "<li><a href='{$p->url}'>$p->title</a></li>"; } foreach($pages->find("yourpagefield=$page") as $p){ echo "<li><a href='{$p->url}'>$p->title</a></li>"; } ?> </ul> Example 2: List related pages based on the yourpagefield field (all referring pages are sorted alphabetically, the block is only output when related pages exist). <?php if(count($page->yourpagefield)>0 || count($pages->find("yourpagefield=$page"))>0 ): ?> <h2>Related</h2> <ul> <?php $ref = array(); foreach($page->yourpagefield as $k=>$p) { $ref[$p->url] = $p->title; } foreach($pages->find("yourpagefield=$page") as $p){ $ref[$p->url] = $p->title; } asort($ref); #shuffle($ref); foreach($ref as $url=>$title) { echo "<li><a href='{$url}'>{$title}</a></li>"; } ?> </ul> <?php endif; ?>
  2. The site will have a few thousand pages (far from all will have relations to other pages). 1) I see your point that PW store every field in a unique table - my table field_related has the structure below: pages_id data sort 1842 1844 0 2) Performance: My worry is not a single field or feature, but the accumulated effect when a page is rendered with SQL queries that "talk too much" with the database. It all adds up. (I worked for six years in a software house that develop, sell and support Web CMS systems to the media industry and have seen how bad SQL queries can affect overall site performance. Disclaimer: I have no idea how PW's SQL works. Just trying to be careful.)
  3. @Soma: Thanks. I did consider a similar solution but rejected it because I worry that it will be performance unfriendly on a large site? Or perhaps caching and optimized SQL queries takes care of that? @Ryan: I was looking for a solution where page relations were stored in a single table (one table for all Page Reference fields) => dbtable RELATIONS could have this layout: id, field_id, aid, bid. SQL query to get all relations: SELECT * FROM relations WHERE aid=1 OR bid=1. Hope this makes sense.. /Kristoffer
  4. The Page Reference-field seems to create a one-way link from one page to one/many other pages. This means that the related pages will only display on the referring page while not on the referred pages. Is it possible to make a two-way link that can be displayed both on the referring page and the referred pages? My Page Reference field is configured as follows: Deference in API as: Multiple pages (PageArray). Input field type: PageListSelectMultiple (Perhaps it is necessary to use another field type?)
  5. @jbroussia: $url is now explained in the example above. after you create the *first* page, that test: if(!$page->id) will always return false so the code below won't execute. My update will explain why this is not the case ($page is defined in the foreach loop - before if(!$page->id) - it returns 0 in my tests when Name does not exist. Note that I use CLI.
  6. @Martijn Geerts: $page is not predefined when using CLI.. So - this code will return null: include("./index.php"); // bootstrap ProcessWire var_dump($page);
  7. How would you recommend creating two pages or more using CLI, including custom fields (summary, body, sidebar, etc.)? I tried creating two+ pages using an array - the first page is correct, but the following pages fail. only the Name field is set, other fields are ignored / not set - I suspect this happens because the $wire object is destroyed somehow when I loop the array- no error message is displayed. Here is one variant of the code I have used: $url_parent = "/myparent/"; $template = "basic-page"; $data = array( array('title'=>'Foo title', 'body'=>'Text for foo.', 'name'=>'foo' ), array('title'=>'Bar title', 'body'=>'Text for bar.', 'name'=>'bar' ), ); foreach($data as $v) { // Get page object. $p = $wire->pages->get($url_parent . $v['name'] ); if(!$p->id) { $p = new Page(); $p->template = $template; $p->parent = $url_parent; $p->name = $v['name']; } $p->of(false); foreach($v as $kk=>$vv) { $p->{$kk} = $vv; } $p->save(); }
  8. How can I save a processwire page programmatically? That is, create a new page or update an existing page. I tried the code below in a \site\templates\article.php... <?php // CREATE PAGE - howto? // MODIFY PAGE // Page /foo/bar/ exists. $mypage = $wire->pages->get("/foo/bar/"); $mypage->title = "Foo Bar Test Page"; // Change the title. $mypage->save(); print_r($mypage); ?> ... and then called a page using the Article template: Error Exception: Can't save page 0: : Pages of type NullPage are not saveable (in \wire\core\Pages.php line 508) #0 [internal function]: Pages->___save(Object(NullPage)) #1 \wire\core\Wire.php(271): call_user_func_array(Array, Array) #2 \wire\core\Wire.php(229): Wire->runHooks('save', Array) #3 \wire\core\Page.php(869): Wire->__call('save', Array) #4 \wire\core\Page.php(869): Pages->save(Object(NullPage)) #5 \site\templates\article.php(10): Page->save() #6 \wire\core\TemplateFile.php(92): require('...') #7 [internal function]: TemplateFile->___render() #8 \wire\core\Wire.php(271): call_user_func_array(Array, Array) #9 \wire\core\Wire.php(229): Wire->runHooks('render', Array) #10 \wire\modules\PageRender.module(236): Wire->__call('render', Array)
×
×
  • Create New...