beto

Addind a new Value to a PageArray Field for a User

4 posts in this topic

I can't add a new value to a custom PageArray field  for my user.

Quote

$current_user = $users->get(3085);
$current_user->setOutputFormatting(false); 
$current_user->galerias->add('2075') ;
$current_user->save();

 

However, when I create a new user, I CAN add the new PageArray value:

Quote

$new_user = new User();
$new_user->name = 'user_2017';
$new_user->galerias->add('2075');
$new_user->save():

 

I need help. Thank you very much.

 

Share this post


Link to post
Share on other sites
$current_user = $users->get(3085);
$current_user->of(false);
$current_user->galerias->add(2075);
$current_user->save();

$existente ???

Share this post


Link to post
Share on other sites

@kixe: 

Sorry, I mistyped my "quote code". The $existente variable is actually $current_user.

This is my real code/problem:

Quote

$current_user = $users->get(3085);
$current_user->setOutputFormatting(false);
$current_user->galerias->add(2075) ;
$current_user->save();

It does NOT add the page to my "galerias" pagetype field.

Share this post


Link to post
Share on other sites
7 hours ago, beto said:

This is my real code/problem:

Quote

$current_user = $users->get(3085);
$current_user->setOutputFormatting(false);
$current_user->galerias->add(2075) ;
$current_user->save();

It does NOT add the page to my "galerias" pagetype field.

It works for me. Double-check your page IDs, field name, and settings of your Page Reference field (allows multiple pages, 'selectable pages' settings allow the page you are trying to add).

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By bmacnaughton
      I would like to be able to fetch the labels for fields in a language different than the current logged in user.
      For field values that's easy 
      // p is page, l is language, and f is field $p->getLanguageValue($l, $f); I'm looking for something like
      $fields->getLanguageValue('en', 'length'); The only solution I know of is to save the current user language, iterate through the languages by setting the user language and fetching the value, and then restore the user language.
       
    • By bmacnaughton
      Is there a way to tell if a field is a multi-language field?
      I am currently checking to see if the last 8 characters are 'Language' but that seems like a fragile solution.
       
       
    • By louisstephens
      I was working on a simple "to-do" style template that has a form on a page. Once the form is submitted, I use the API to  create a new subpage under a pre-existing page, but I notice that it does resubmit the form data (as to be expected) if a user were to refresh the page. Is there a way to prevent this behavior so duplicate content will not be posted by using something like exit(), or is this not proper due to usability?  I guess I am curious as to how other developers handle the same hurdle.
    • By cosmicsafari
      Hi all,
      I am creating a page field (field of type FieldtypePage) via the API, however im still trying to find some documentation as to how I would go about setting the Selectable Pages for said field using the API.
      From what I have found it looks like it involves the use of, albeit this looks like a getter rather than a setter:
      $field->getInputfield($page) Which looks like it would make sense if I wanted to specify the selectable pages by a parent page, but what if I wanted to specify it by say a template?
    • By EyeDentify
      Hello Fellow PW Fans and Gurus.

      I have run into a problem where i have created a template without a template file associated with it.
      Now i would like to HAVE a template file associated with it, not an alternate but as the main template file.

      So i thought, that's easy, I upload the template file and change it in template settings but I only get the Alternate template file setting?

      Ok so maybe i can change it via the API , so i wrote some code for this.
      <?PHP function changeTemplate($pages = null, $templates = null) { /* get the pages to change template on */ $logItems = $pages->findMany('template=tmp_log_item'); /* get the template object for our desired template */ $Template = $templates->get('tmp_new_log_item'); foreach($logItems AS $key => $logData) { /* use template object $Template to set template */ $logData->template = $Template; /* save page item */ $logData->save(); } } /* do the magic change */ changeTemplate(); ?> Needless to say, PW was not happy about this, It throw up an error message:
      Error: Exception: Invalid value sent to Page::setTemplate (in /home/virtual/mydomain/public_html/wire/core/Page.php line 1782)
      Now from what i understand i gave $logItem->template the wrong type of value.
      So what should the value be to correct this?
      When I read the docs at: https://processwire.com/api/ref/page/
      the value can be "string" or "Template".

      So i gave it a string and it went haywire.

      How should I approach this cause I realy dont want to manually change template file on över 50 plus pages

      Maybe I should use the Template class to create the value that the API wanted, but I am unsure how to use it.

      Thank you for any info you can give me on this.

      If you want more info to help me, ask and I will try my best to give it to you.
      Updated and Solved
      I solved the problem and have updated the code to reflect this.

      Essentialy i used the get() method of $templates to get the template object for my template i wanted to use using the template name.
      I gave the Object to the $logItem->template property and all went well.
      We learn something new every day.
      One Note:
      The template file you want to use must have been uploaded and added as a template in the Template section in the Admin for this code to work as expected.