Pavel Radvan Posted May 1 Share Posted May 1 I have quite specific question. I am working on app in processwire which Is for contract and product management. I have already quite lots of pages with imported data. I need to establish link for page(contract) which should be connected with multiple pages (products). Imported data have same numeric field on contract template and also on product template. This field is not a name or title of page. Now is all achievable on fronted where is possible to prepare markup with selected data. But I need to have this connected in backend(admin) - is possible to connect fields between pages 1:n from already created data? Reason for this is to have option to edit connected data in backend ... I would appreciate any advice. Anybody know how to do this? Link to comment Share on other sites More sharing options...
gebeer Posted May 2 Share Posted May 2 If I understand correctly, you want to make the connection between contract and product pages after data import. In the imported data you have a numeric ID that connects contracts to products. Can you please clarify if that numeric ID has been saved to a field on both contract (multiple IDs) and product page (single ID)? Once we know that, it would be quite easy using a small script to make the connection through a page reference field on the contract page that references one or more products. 2 Link to comment Share on other sites More sharing options...
Pavel Radvan Posted May 3 Author Share Posted May 3 Hi. Thanks for help and you understand well. I can clarify that numeric field (ID) is saved on both contract and product page. I would appreciate to have some script to handle links. Link to comment Share on other sites More sharing options...
gebeer Posted May 3 Share Posted May 3 First you need to add a page reference field to the contract template. I assume you name it 'products' Set it to accept multiple pages Choose type ASM select and product template for allowed templates Now that you have your products field in place, you can write a script to assign the product pages to the contract pages. foreach($pages->find("template=contract") as $cp) { // assuming you have a field $cp->uid with the ids of the products // if you have a comma separated list of ids or the like, convert it to array $uids = explode(',', $cp->uid); // add products for each $id to products field foreach($uids as $id) { $cp->products->add($pages->find("template=product, uid=$id")); } // save the changes $cp->setAndSave('products'); } I would recommend using TracyDebugger Console to run the script. Or you can run it from site/templates/home.php This should assign the correct products to the contract pages and make them editable from the contract pages edit screen. 2 Link to comment Share on other sites More sharing options...
Pavel Radvan Posted May 3 Author Share Posted May 3 Thank you very much and I will try it. I will let you know how it worked. Link to comment Share on other sites More sharing options...
Pavel Radvan Posted May 3 Author Share Posted May 3 I have more explored your script. I do not have array of products IDs. I have products as pages already in PW and each of this product has numeric field with its ID. Is it possible to link all those pages(they have main parent page("products") by this script with corresponding pages by same contract ID ? Link to comment Share on other sites More sharing options...
gebeer Posted May 3 Share Posted May 3 6 hours ago, PavelRadvan said: Thanks for help and you understand well. I can clarify that numeric field (ID) is saved on both contract and product page. Did you save the product IDs for the linked products to your contract pages when importing the data? If you don't have the product IDs for each contract page, there is no way of linking products to contracts by a script. Link to comment Share on other sites More sharing options...
Pavel Radvan Posted May 3 Author Share Posted May 3 Yes. There is imported lots of pages of product items (products) with "contract head ID". Same "contract head ID" is also on pages which are contracts. I am using pages->find and pages->get and other function on frontend to show them by this ID - so it now all works on frontend. But when I click to edit product in backend it shows only current product without link to contract there. So reason for this is to have editable contracts in backend and with already selected products(multiple) in page reference field of specific contract. When I create now in PW new contract I could select by page reference field multiple products - but I do not want to do it manually for next several thousands of contracts... I hope this is understandable for you... Link to comment Share on other sites More sharing options...
gebeer Posted May 4 Share Posted May 4 Thank you for the explanation. I still do not fully understand how you contract head ID is saved to the product and contract pages. 9 hours ago, PavelRadvan said: But when I click to edit product in backend it shows only current product without link to contract there. I assume that on the product template you do not have page reference field which links to contract(s). 9 hours ago, PavelRadvan said: So reason for this is to have editable contracts in backend and with already selected products(multiple) in page reference field of specific contract. But on the contract template you already have a page reference field for products which contains products for that contract. Please specify the field name Now you want to "link back" the contracts to a product using the products available in the contracts' page reference field for products. Is this correct? Could you please share screenshots of the templates for contract and product? And also screenshot of the current edit page for contract and product with the relevent fields? That would help to clarify. 9 hours ago, PavelRadvan said: When I create now in PW new contract I could select by page reference field multiple products - but I do not want to do it manually for next several thousands of contracts... Sorry, but you lost me there ? When you create a new contract, how does the contract know, which products are connected to it? When creating a new contract, do you assign an existing contract head ID that already is linked to products? Link to comment Share on other sites More sharing options...
Pavel Radvan Posted May 4 Author Share Posted May 4 Thanks very much for reply and I have to explain current situation more. I am sorry but mys system is in my language (Czech), but I think it is clearly visible in pictures how are fields and IDs. I have imported data from Access in form of CSV trable files. This import was done always from one table to one page to create lots of children pages. I could as ne example of data take table with one type contracts named "ks-hlavicka" which is table of contracts (chidren pages) and details (head of contract) and products table is named "ks-old-pol" (also children pages). Each of those tables have numeric field "id_hlavicka_ks" with label ID hlavičky KS or ID hl KS. Here are screenshots from admin (backend): Contract: Product item: As you could see there is same ID in both pages. This is actual current situation. There is not page reference field yet in those pages - I need to know how to prepare it to template for scripts or guides or by your advice... On Frontend I could use API of PW to show content by ID but I would like to have interconnected pages also visible in admin (backend). What I want is to achieve this - it is example of created contract on another page with products items linked in page reference field: Same functionality I would like to have same for product also with connected customers(also they have IDs) which are on contracts. But after I will have some script or guide to link products I think other connected data could be handled by same way. Here are screenshots of templates for contract (ks_hlavicka) and product item (ks_polozka_old): Does this helped to you to understand ? Let me know how you think is possible to make that interlinked connections. Thanks Pavel 1 Link to comment Share on other sites More sharing options...
gebeer Posted May 5 Share Posted May 5 Thank you for providing the screens. That helps. If you want to have linked products on the contract pages, you need to add a page reference field for products, like I described above. After you have added that field, you can use this script to loop through all contract pages and assign linked products by id: foreach($pages->find("template=contract") as $cp) { // assuming you have a field $cp->uid with the id // get products with same id $products = $pages->find("template=product, uid={$cp->uid}, status<" . Page::statusTrash); // add products with same id to products field $cp->products->add($products); // changed this line // save the changes $cp->setAndSave('products'); } EDIT: changed a line in the code You need to change the template and field names to fit your installation. After running the script, every contract page should have linked products assigned. The process for linking customers to products should be similar. Let me know how it goes. Link to comment Share on other sites More sharing options...
Pavel Radvan Posted May 5 Author Share Posted May 5 Hi. Thanks very much. I will test it and let you know Link to comment Share on other sites More sharing options...
gebeer Posted May 6 Share Posted May 6 8 hours ago, PavelRadvan said: Hi. Thanks very much. I will test it and let you know Before you try, I changed a line in the code above. Use the new version. Link to comment Share on other sites More sharing options...
Pavel Radvan Posted May 6 Author Share Posted May 6 Hi. Thank you. I hope this evening try it. If there will be some problem I will let you know... Link to comment Share on other sites More sharing options...
Pavel Radvan Posted June 15 Author Share Posted June 15 On 5/5/2024 at 2:12 PM, gebeer said: Thank you for providing the screens. That helps. If you want to have linked products on the contract pages, you need to add a page reference field for products, like I described above. After you have added that field, you can use this script to loop through all contract pages and assign linked products by id: foreach($pages->find("template=contract") as $cp) { // assuming you have a field $cp->uid with the id // get products with same id $products = $pages->find("template=product, uid={$cp->uid}, status<" . Page::statusTrash); // add products with same id to products field $cp->products->add($products); // changed this line // save the changes $cp->setAndSave('products'); } EDIT: changed a line in the code You need to change the template and field names to fit your installation. After running the script, every contract page should have linked products assigned. The process for linking customers to products should be similar. Let me know how it goes. Hi. I did not have time to test it sooner so I did it today. I used your script which was run from tracy console and there is final variant of it : foreach($pages->find("template=ks-import-hlavicka") as $cp) { $polozky = $pages->find("template=polozka_ks, id_hlavicka_ks={$cp->id_hlavicka_ks}, status<" . Page::statusTrash); $cp->id_polozka_ks->add($polozky); $cp->setAndSave('polozky'); } where id_polozka_ks is that integer id field which is on both pages of contracts and products. As you could see in one of contract pages, there is no linked data: But system is linked to see items from products template: Script just reported that executed with 4487.67 ms, 15.1 MB What do you think of it - is there any problematic part or I need some permissions to interconnect data to pages ? Thanks for reply Pavel Link to comment Share on other sites More sharing options...
Pavel Radvan Posted June 15 Author Share Posted June 15 It seems I find myself where is problem with script from you. This version is working for me: foreach($pages->find("template=ks-import-hlavicka") as $cp) { $polozky = $pages->find("template=polozka_ks, id_hlavicka_ks={$cp->id_hlavicka_ks}, status<" . Page::statusTrash); $cp->Polozky_ks->add($polozky); $cp->setAndSave('Polozky_ks',$polozky); } Most important is that in setAndSave is needed to specify that page field to which I need to save changes and update data... Without it there was no change in data...and no linked other pages... Regarding page reference field with single-page input, there is not needed to use "add" method. It throws error if I used it for adding data, because I had same data already on same page... I tried to add linked page with customer data to contract page and it is always only one customer for contract... So thanks for your initial hint with script and now it is working...hope this could help to others also... 1 Link to comment Share on other sites More sharing options...
gebeer Posted June 16 Share Posted June 16 Happy to hear that you figured it out! Link to comment Share on other sites More sharing options...
Pavel Radvan Posted June 16 Author Share Posted June 16 Thanks for reply. I am just figuring another think and it is validating data before creation of new page - it is for preparing right contract number so customer just click on new contract button on frontend and PW should prepare new page with constructed name from several parts and fields. But I need to validate if there is not already another contract number already, but before it is created. I already discussed this with @ryan, but it seems that he is quite busy. I will invite you to this and just need some small hint to figure it... Link to comment Share on other sites More sharing options...
Pavel Radvan Posted June 16 Author Share Posted June 16 It it here https://processwire.com/talk/messenger/5614/?do=findComment&comment=34986 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now