Error in setAttribute()

Permalink 1 user found helpful
Hi Everyone,

I have to set attribute for particular pages using code, so i have tried the below given code, but it shows Fatal error.

Loader::model('collection_attributes');
$pages = Page::getByID($pages_id->cID);
$pages->setAttribute('reference_number',$unique_id[$i]);


Error:
Fatal error: Call to a member function setAttribute() on a non-object in E:\xampp\htdocs\concrete\job\concrete\models\collection.php on line 220


Any help would be appreciable.
Thank you
Vijay

SVijay
View Replies: View Best Answer
jaredquinn replied on at Permalink Reply
jaredquinn
My guess would be that your Page::getByID(..) isn't returning a page object (ie. false).

Is $pages_id->cID a valid page ID?

Jared
TheRealSean replied on at Permalink Reply
TheRealSean
I think Jared is right here it would appear to be a problem with the $pages_id->CID;

You can run this check

if (is_object($pages)) {
echo "Frankie says Relax, don't do it";
$pages->setAttribute($attribute,$value)
}else{
echo "Not an Object";
}

That would also stop the error from appearing as if $pages is not an object then the setAttribute function is not called.
Mnkras replied on at Permalink Reply
Mnkras
or use the simpler way,

$pages = Page::getByPath('derp');
if(!$pages->isError()) //page is not error
SVijay replied on at Permalink Reply 1 Attachment
SVijay
Hi everyone,

First of all thanks for your responses and sorry for my delayed reply.
I have tried your methods to check the $pages, but there is no error in $pages. Its returning page object.

For more details, please refer the attachment.

Any help would be appreciable.
Thank you
Vijay
TheRealSean replied on at Permalink Best Answer Reply
TheRealSean
Sorry I have not looked at the attachment yet, but I stumbled across a very similar looking error, when attempting to add a thumbnail to the page,

It turned out the value I was adding was null,

my
attibute was then trying to add,
$setAttribute('page_thumb',)

that caused an error on a non object. could it possibly be the $unique_id[$i] that you are calling?? is actually not reurning anything?

Ill take a look through the attachment later when not at work see if I can be any more fruitful, this was just a quick thought I had while coding last night
SVijay replied on at Permalink Reply
SVijay
Hi,

Thanks for your reply. Found and solved the problem.