Jump to content

Returning image results as Pageimage objects from a database query


cosmicsafari
 Share

Recommended Posts

Hi all,

This is probably a stupid question but is there anyway to select from the database all the images as Pageimage objects?

The idea being so that I could have just a 1 level deep array of Pageimage objects so that I can loop through and run the removeVariations() method on them?

My original idea was something along the lines of:

$query = $this->database->prepare('SELECT name FROM FIELDS WHERE TYPE = "FieldtypeImage"');
$query->execute();
$results = $query->fetchAll();

foreach ($results as $row) {
	$query = $this->database->prepare('SELECT * FROM FIELD_'.$row[0]);
	$query->execute();
    $images = $query->fetchAll();
    foreach($images as $image){
     // my thinking was at this point if said results where Pageimage objects or
	 // could be turned into one I could then run the removeVariations() method on them ie.
	 // $image->removeVariations()
    }
}

I tried working with the above results within my module code but I could never get them into the correct context to run the method in question.

The above implentation obviously doesn't work but is the idea sound?

Could it ever work in this fashion or do you only have access to Pageimage::removeVariations() in the context of starting with a page selection ie.

$page->image->removeVariations();

The reason I ask is that I have a solution using the built in selectors which partially works.

However it comes up short when attempting to remove the image variations for image fields within repeaters, theres also the issue of images fields within nested repeaters.

Any ideas are much appreciated.

  • Like 1
Link to comment
Share on other sites

This looks okay. You just need to get the correct image from the page. For performance reasons, I'd build an intermediate array that groups the images by page, as you have to load each page into memory to retrieve the image. This way, you can uncache the page after you have rebuilt all its variations. Untested:

$query = $this->database->prepare('SELECT name FROM FIELDS WHERE TYPE = "FieldtypeImage"');
$query->execute();
$results = $query->fetchAll();

$pageimgdata = [];

foreach ($results as $row) {
	$query = $this->database->prepare('SELECT * FROM FIELD_'.$row[0]);
	$query->execute();
    $images = $query->fetchAll();
    foreach($images as $image){
    	$pageid = $images["pages_id"];
    	$filename = $images["data"];
    	if(! isset($pageimgdata[$pageid])) $pageimgdata[$pageid] = [];
    	$pageimgdata[$pageid][] = $filename;
    }
}


foreach($pageimgdata as $pageid => $filename) {
	$p = $this->pages->get($pageid);
	$PFM = new PagefilesManager($p);
	foreach($data as $entry) {
		$img = $PFM->getFile($filename);
		if($img) $img->removeVariations();
	}
	$PFM = null;
	if($p != $this->page) $this->pages->uncache($p);
	// To clear up memory early in case there are a lot of pages
}

 

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

34 minutes ago, BitPoet said:

This looks okay. You just need to get the correct image from the page. For performance reasons, I'd build an intermediate array that groups the images by page, as you have to load each page into memory to retrieve the image. This way, you can uncache the page after you have rebuilt all its variations. Untested:


$query = $this->database->prepare('SELECT name FROM FIELDS WHERE TYPE = "FieldtypeImage"');
$query->execute();
$results = $query->fetchAll();

$pageimgdata = [];

foreach ($results as $row) {
	$query = $this->database->prepare('SELECT * FROM FIELD_'.$row[0]);
	$query->execute();
    $images = $query->fetchAll();
    foreach($images as $image){
    	$pageid = $images["pages_id"];
    	$filename = $images["data"];
    	if(! isset($pageimgdata[$pageid])) $pageimgdata[$pageid] = [];
    	$pageimgdata[$pageid][] = $filename;
    }
}


foreach($pageimgdata as $pageid => $filename) {
	$p = $this->pages->get($pageid);
	$PFM = new PagefilesManager($p);
	foreach($data as $entry) {
		$img = $PFM->getFile($filename);
		if($img) $img->removeVariations();
	}
	$PFM = null;
	if($p != $this->page) $this->pages->uncache($p);
	// To clear up memory early in case there are a lot of pages
}

 

Thats exactly the kind of thing I was looking for! ?

If im ever anywhere near Munich i'll buy you as many beers as you can drink ?

  • Like 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...