Jump to content

PageTable page not being assigned to parent page


Marc
 Share

Recommended Posts

I have a template file that receives some form data that I save to a pageTable field. I use the pageTable to add notes to user profiles, so every user can have child pages containing a note and a date. Here's what I use:

$notes = $input->post->textarea('notes'); // Passed via javascript.
$customerId = $input->post->int('id');
$customer = $users->get($customerId);

$newNote = new Page;
$newNote->template = 'user-notes'; 
$newNote->parent = $customer; 
$newNote->of(false); // turn off output formatting before setting values
$newNote->save();
    
$newNote->title = 'user note';
$newNote->notes_date = date('Y-m-d H:i:s');
$newNote->notes = $notes;
$newNote->save();

// So far so good, but this bit does not work:
$customer->user_notes->add($newNote);
$customer->of(false);
$customer->save('user_notes');
// Just as an example, this part DOES work:
$customer->gender = 2;
$customer->save('gender');

The child page is created but it is not added to the user's 'user_notes' pageTable. If I place this code in my admin module, it works as expected. Any ideas what could be going wrong?

Edit: fixed it by switching these lines around:

$customer->of(false);
$customer->user_notes->add($newNote);

Now it works as expected. Strange it works in the other order on another page, but there you go.

Link to comment
Share on other sites

Fixed it by switching these lines around:

$customer->of(false);
$customer->user_notes->add($newNote);
 

Now it works as expected. Strange it works in the other order on another page, but there you go.

Link to comment
Share on other sites

Looks like I still don't quite understand. In a regular template file I added these tests, trying to remove a page from 'user_notes' which is a PageArray field:

$note = $pages->get(1259);
$customer = $note->parent;

$customer->user_notes->remove($note);
$customer->of(false);

$customer->save('user_notes');

^ Result: no error, but PageArray is not updated.

$note = $pages->get(1258);
$customer = $note->parent;

$customer->of(false);
$customer->user_notes->remove($note);

$customer->save('user_notes');

^ Result: "Field 'user_notes' from page 1172 is not saveable because it is a formatted value."

$note = $pages->get(1258);
$customer = $note->parent;

$customer->user_notes->remove($note);
$customer->of(false);
$customer->user_notes->remove($note);

$customer->save('user_notes');

^ Result: PageArray is successfully updated.

Why would I have to use the 'remove' method twice in order for this to work?

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

×
×
  • Create New...