Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

3.1.10 DataObject / onAfterWrite / in_array


Go to End


6 Posts   2224 Views

Avatar
timo

Community Member, 47 Posts

10 April 2015 at 5:46pm

Edited: 10/04/2015 5:49pm

I want to update a field on StartPage from a DataObject:
should remove the ID from a comma separated Field:

On StartPage: static db ……. 'MixedGalleries' => 'Varchar' ……… value is: 1,2,3

On Product- Dataobject:

function onAfterWrite(){
		parent::onAfterWrite();
			
		if($this->ShowProductImages != '1' ){
			
			$startPage = StartPage::get()->First();
			$mixedGalleriesField = $startPage->MixedGalleries;
			$fieldArray = explode(",", $mixedGalleriesField);
			if(in_array($this->ID, $fieldArray)){ // in_array  always returns Null !!!!!!  WHY??????
		
				$arr = array_diff(explode(",", $mixedGalleriesField), array($this->ID));
				$startPage->MixedGalleries = $arr;
				$startPage->write();
				$startPage->writeToStage('Stage');
				$startPage->publish("Stage", "Live");
			}
		}
	}

in_array always returns Null !
Thanks.Timo.

Avatar
timo

Community Member, 47 Posts

11 April 2015 at 12:43am

Edited: 11/04/2015 1:06am

a hard childbirth:

function onAfterWrite(){
		parent::onAfterWrite();
			
		if($this->ShowProductImages != '1' ){
			
			$startPage = StartPage::get()->First();
			$mixedGalleriesField = $startPage->MixedGalleries;
			$fieldArray = explode(",", $mixedGalleriesField);
				
				while(($i = array_search($this->ID, $fieldArray)) !== false) {
					unset($fieldArray[$i]);
				}
				
				$string = implode(",", $fieldArray);
				$startPage->MixedGalleries = $string;
				$startPage->write();
				$startPage->writeToStage('Stage');
				$startPage->publish("Stage", "Live");
		}
	}

Avatar
Pyromanik

Community Member, 419 Posts

11 April 2015 at 12:55am

Edited: 11/04/2015 12:56am

in_array never returns null.
http://php.net/in_array#refsect1-function.in-array-returnvalues

It will however throw a non-fatal error:

php -r "var_dump(in_array(2,'asdf'));"
PHP Warning:  in_array() expects parameter 2 to be array, string given in Command line code on line 1
NULL

Your parameter mustn't be what you think it is.

Avatar
timo

Community Member, 47 Posts

11 April 2015 at 1:01am

Edited: 11/04/2015 1:03am

true / false
1 / 0
i ment in_array always gives me 'is not in array'
so its false ?

Avatar
Nicolaas

Forum Moderator, 224 Posts

11 April 2015 at 10:51am

Here are some ideas:

	function onAfterWrite(){
		parent::onAfterWrite();
		//why do you need the quotation marks here?
		//if ShowProductImages is Boolean, you can just write if($this->MyBooleanField) {...}
		if($this->ShowProductImages != '1' ){
			//how many start pages are there?  Are you sure you are getting the right one?
			$startPage = StartPage::get()->First();
			// you may want to check you get a StartPage back before doing anything more.
			// will there always be a start page?
			$mixedGalleriesField = $startPage->MixedGalleries;
			$fieldArray = explode(",", $mixedGalleriesField);
			//I would add:
			//foreach($mixedGalleriesField as $key => $value) {$mixedGalleriesField[$key]  = intval(trim($value));}
			//for debugging purposes I would write here: print_r($mixedGalleriesField);
			// 
			if(in_array($this->ID, $fieldArray)){ // in_array  always returns Null !!!!!!  WHY??????
				// why do the exploding again?  use the variable from above and in case 
				// you are planning to change mixedGalleries field copy it to 
				// a base sort of variable that you can come back to again and again
				$arr = array_diff(explode(",", $mixedGalleriesField), array($this->ID));
				$startPage->MixedGalleries = $arr;
				$startPage->write();
				//line below is superfluous
				$startPage->writeToStage('Stage');
				$startPage->publish("Stage", "Live");
			}
		}
	}

Avatar
timo

Community Member, 47 Posts

12 April 2015 at 5:13am

Edited: 12/04/2015 5:15am

Hi Nicolaas!

Thanks for your suggestions.
@ //I would add:
//foreach($mixedGalleriesField as $key => $value) {$mixedGalleriesField[$key] = intval(trim($value));}
//for debugging purposes I would write here: print_r($mixedGalleriesField);

I only want to remove $this->ID from the $mixedGalleriesField on StartPage.
(If ! ShowProductImages on this Product remove this ID from $mixedGalleriesField on StartPage )

StartPage :

public function getRandomGalleries() {
		$allGalleries = ArrayList::create();
		foreach(Dataobject::get('Product')->filter(array('ShowProductImages' => '1')) as $gallery) {
			$existingGallery = $gallery;
			$allGalleries->merge($existingGallery);
		}
		return $allGalleries;
	}

public function getCMSFields() {
............
$mixedGallery = $this->getRandomGalleries();
$mixedGalleryMap = $mixedGallery->map('ID', 'Title', '(Select one)', true);

if($this->ShowMixedGallery){
			$fields->addFieldToTab('Root.GalerieBilder', new ToggleCompositeField('Mixed Galerie', 'Mixed Galerie', 
			array( new CheckboxSetField( $name = "MixedGalleries", $title = "choose galleries", $source = $mixedGalleryMap, $value = ''))));
			
			//TotalGalleryImages (0-40)
			$fields->addFieldToTab('Root.GalerieBilder', new DropdownField('TotalGalleryImages', 'Anzahl Bilder', $this->getNumbers()));  
		}
...........

AND HERE COMES THE NEXT PROBLEM:

Im trying to merge all Products where ShowProductImages == 1 && checked in CheckboxSetField (on StartPage)
I came up with this but i can not get it working with ->sort('RAND()')->limit($startPage->TotalGalleryImages) :
( I need $gallery->MyImages() AND mylink' => $gallery->Link() )

Page_Controller:

public function getMixedGallery(){
		
		$startPage = StartPage::get()->First();
		$mixedGalleriesField = $startPage->MixedGalleries;
		$fieldArray = explode(",", $mixedGalleriesField);
		
		$images = ArrayList::create();
		foreach($fieldArray as $id){
			foreach(Dataobject::get('Product')->filter(array('ID' => $id, 'ShowProductImages' => '1')) as $gallery) {
				if($gallery->MyImages()->Count() > 0){
					
				$imagesForGallery = array('myimage' => $gallery->MyImages(), 'mylink' => $gallery->Link());
				$images->push($imagesForGallery);
				
				}
			}
		}
		return $images;

StartPage.ss :

<div class="gallery-content" >
					<% if $ShowGallery %>	
						<% if $ShowMixedGallery == 1 %>
							<% include Gallery gallery=$MyGallery , mixed=$ShowMixedGallery , Mixed=$getMixedGallery() %>
						<% else %>
							<% include Gallery gallery=$MyGallery , mixed=$ShowMixedGallery %>
						<% end_if %>
					<% end_if %>
				</div>

Gallery.ss :

	<% if $gallery == '1' %>
	
		<div id="gallery-holder" style=" margin: 0px -0px 0px -4px; padding: 0px 0px 0px 0px; ">
			<div id="gallery" class="masonry">
			
				<% loop Mixed %>
					<% loop myimage %>
				
				
					<div class="box masonry-brick img-responsive" >
						<a href=" $Up.mylink " class="lightbox alpha dark " rel="myscale[$Top.ID]">
							<img class="gallery_img img-responsive img-rounded-light" src="<% with $Image %>$CroppedImage(100,100).Link<% end_with %>"  alt="$Title" style=""/>
						</a>
					</div>
					
					<% end_loop %>
				<% end_loop %>
				
			</div>
		</div>
			
	<% end_if %>

Thanks.timo.