Jump to content

Recommended Posts

Posted

Hello guys!

I'm starting to build a custom csv-import module and doing little pre testing here. Just playing with API and already in trouble..

Why I can't update Page input data with this one:

$demo->setOutputFormatting(false);
$demo->set('product_group', 1040);
$demo->save();

When it works perfectly fine with text fields. How should I update it's value?

Posted

Hi Roope,

Is product_group a Page field and 1040 the ID of a page?

If so, does the page exist and is it allowed for your page field?

Posted

Hello Wanze, thanks for reply!

Yes, product_group is a Page field and 1040 ID for existing page. Actually $demo page has already value set: $demo->get('product_group') will output "1024|1040" so setting it to 1040 shouldn't be problem.

Setting some other field values works like excepted:

$demo->setOutputFormatting(false);
$demo->set('title', 'Cool Title');
$demo->save();
Posted

Hi Roope,

product_group is a PageArray, so you have to add the page to that array:

$demo->product_group->add(1040);
// If above doesn't work, try:
$demo->product_group->add($pages->get(1040));
Posted

I tried to set value with all these three variations:

$demo->set('product_group', array(1040));
$demo->product_group->add($pages->get(1040));
$demo->product_group->add(1040);

No effect. I still get original value (1024|1040) with:

die($demo->get('product_group'));

I know that it's possible to set values (page IDs) to Page input type by Ryans CSV import module and I see no special treatment there for any other fields than FieldtypeFile. I just can't figure out why set doesn't do anything here.

Posted

But the page with id 1040 is already in the array. Just add another page that's not there - that should work.

Posted

Of course... thanks!

It works with reqular setter and you can set multiple values by using array:

$demo->set('product_group', 1038); // append single value
$demo->set('product_group', array(1041,1042)); // append multiple

Cool!

How about when you have need to update whole input - like in the example? I need to set it to 1040 and input already has two values 1024|1040.

Posted

There is something weird happening with Page type inputs when I import values. Code for setting is like this:

// set page values
else if ($this->fieldTypes[$name] == 'page') {
	$page->set($name, explode(',', $value));
}

For some reason I can only set values for Page fields that has PageListSelectMultiple set on Input field type setting. Page fields with Select or AsmSelect are left blank.

Posted

OK, got it figured... Seems that it's safer to set values one by one. This got all values set to every Page fields correctly:

// set page values
else if ($this->fieldTypes[$name] == 'page') {
	$fields = explode(',', $value);
	foreach ($fields as $field) {
		$page->set($name, $field);
	}
}
Posted

Did you try using the other array methods?

$demo->setOutputFormatting(false);
$demo->product_group->remove(1024);
$demo->save();

or:

$demo->setOutputFormatting(false);
$demo->product_group->removeAll();
$demo->product_group->add(1040);
$demo->save();
Posted

Just did. So removeAll() is basicly what I need when reseting all data, thanks! Working nicely. Also add method.

Remove isn't saying anything though - should it?

Posted

Yep, remove() also does't work with me. add() works, pop() works, shift() works and push() also works, why doesn't remove()?

Posted

According to the cheatsheet remove() takes either a key or an object.

Maybe I'm wrong but when you write a page-id like 1040, this is considered as key which doesn't exist.

So you could try this:

// Remove 1st item
$demo->product_group->remove(0);
// Remove 2nd item
$demo->product_group->remove(1);
// Remove object
$p = $this->pages->get(1040);
$demo->product_group->remove($p);
  • Like 2
Posted

You must be right Wanze, add() doesn't accept a key, only the item, so when you pass a number it assumes the ID, while remove() assumes that it's the key.

Posted

Yes Wance, your example works exactly right - good info!

Can you guys help me how to implement removeAll() method in my module? Tried this but doesn't work - Fatal error: Exception: Method Page::removeAll does not exist or is not callable in this context:

// set page values
else if ($this->fieldTypes[$name] == 'page') {
	if ($reset) $page->$name->removeAll();
	$fields = explode(',', $value);
	foreach ($fields as $field) {
		$page->set($name, trim($field));
	}
}
Posted
Fatal error: Exception: Method Page::removeAll does not exist or is not callable in this context:

removeAll() is a method of a PageArray not a Page. So I think your code will need to check what type it's dealing with in order to avoid that error. 

if($reset) {
  if($page->$name instanceof PageArray) $page->$name->removeAll();
    else $page->$name = null;
}
  • Like 3

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.
×
×
  • Create New...