Jump to content

Adding an image to a Repeater Matrix Field


Guy Verville
 Share

Recommended Posts

Hi,

I'm trying to simply save an image contained in a Repeater Matrix field. But I get an error with this code:

$added = $p->member_mx_images->getNew();
			$added->member_images->add($responseAdded['imageURL']);
			$added->crm_products_sold = $products;
			$added->save();
			$p->of(false);
			$p->member_mx_images->add($added);
			$p->save();

The error is: Call to a member function add() on null at the line $added->member_images->add($responseAdded['imageURL']);

member_images is the image field. If I remove the add instruction, nothing is saved.

$responseAdded['imageURL'] is a valid address coming from and external site (my purpose is to make two Processwire sites communicate certain information).

crm_products_sold is a Page reference field. $products is an array of IDs.

If I remove the instruction for member_images, I do get a repeater saved, but it is empty anyway.

I don't know what I am doing wrong.

Link to comment
Share on other sites

You have to save a newly created page (including a repeater page) before you can add files or images to it.

$added = $p->member_mx_images->getNew();
$added->save();
$added->member_images->add($responseAdded['imageURL']);
$added->crm_products_sold = $products;
$added->save();
$p->of(false);
$p->member_mx_images->add($added);
$p->save();

 

  • Like 2
Link to comment
Share on other sites

Thank you for answering. I did that before, but it's not working. Still the same error.

Given:

  1. Site accepting the request: mq.local
  2. Site where the file resides: afmq.local
  3. $responseAdded['imageURL] = http://afmq.local/site/assets/files/1862/img_0729.jpg

Since the url given is outside mq.local, should I download the file before?

Link to comment
Share on other sites

You need to set the type of the repeater matrix....

ie: $new->repeater_matrix_type = 1;

and save the item before adding files.

And you don't need to add the new item to the field after that, it's already added.

  • Like 3
  • Thanks 1
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...