Jump to content

Call to a member function size() on a non-object error on template that was previously working fine


Tyssen
 Share

Recommended Posts

I'm doing some updates on a site that only just launched last week and during development everything was fine. But now I've exported the db from the production site, imported it back into my local and get Call to a member function size() on a non-object.

My template looks like:

foreach($products as $product) :

    $img_src   = $product->images->first();

    echo $img_src;                     // this works
    echo $img_src->url;                // so does this
    echo $img_src->size(300,300)->url; // this gives me the error

endforeach;

What would be causing this to error now when it was working fine before and I can get values for the image and its source, just not the resized version?

Link to comment
Share on other sites

I have no clue and try myself atm, but:

-does it make a difference when you add a space between the width and the height, so size(300, 300)

-does the image variation already exist on disk? Maybe the chaining does not work when it doesn't.

- What if you do something like

$variation = $img_src->size(300,300);

echo $variation->url;

Link to comment
Share on other sites

Unfortunately, that doesn't help.

I get the error whether I use size(), width() or height(). And if I move ->url to another line, the error message still points at the line where size, width or height is first called.

Yes, all these images already exist on disk, as they were created when the site was first in development and everything was working fine.

Link to comment
Share on other sites

It sounds to me like your code is iterating through the $products (pages found by some selector I'm assuming) but some of those pages don't have the images field filled with anything, resulting in your error. Try to validate $img_src like this:

foreach($products as $product){
    $img_src   = $product->images->first();
    if($img_src) {                               // Check $img_src
        echo $img_src;                           
        echo $img_src->url;                      
        echo $img_src->size(300, 300)->url;      
    }
    /** Let's see what pages don't have the images field set to anything **/
    else{
        echo "<br/> The page " . $product->name . " doesn't have the images field set to anything. <br/>";
    }
}
  • Like 4
Link to comment
Share on other sites

Actually, that's what's going wrong. I'm trying to set up a multi-site set up locally where content is pulled in from another part of the site and one of the new test entries I've created doesn't have an image. Thanks for steering me in the right direction.  :)

Link to comment
Share on other sites

Hello, maybe it's better this way

foreach($products as $product){
 
if($product->images){// <- this check

$img_src = $product->images->first(); // <- and this line
echo $img_src;
echo $img_src->url;
echo $img_src->size(300, 300)->url;
 
}
/** Let's see what pages don't have the images field set to anything **/
else{
echo "<br/> The page " . $product->name . " doesn't have the images field set to anything. <br/>";
}
}

regards

  • 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

×
×
  • Create New...