Jump to:

23456 Posts in 18905 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » weird non redirect issues

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

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

Page: 1
Go to End
Author Topic: 654 Views
  • mattclegg
    Avatar
    Community Member
    56 Posts

    weird non redirect issues Link to this post

    Hi,

    Im using a lookup function to get a relevant object by ID like;

    $val=(search string as integer);

    $sqlQuery = new SQLQuery("COUNT(myObject.ID)","myObject","ID=".intval($val));
    if($sqlQuery->execute()->value()){
    return DataObject::get_by_id('myObject', intval($val));
    }else{
    Director::redirect('/myObjectSearchPage/');
    }

    but instead of being redirected the function is returning null and I get;

    [Notice] Trying to get property of non-object

    Any ideas?

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: weird non redirect issues Link to this post

    EDIT: misread the question, let me try again:

    Why are you running an SQL Count query? The following makes a lot more sense:

    if($object = DataObject::get_by_id('myObject', intval($val)))
       return $object;
    else
       Director::redirect('/myObjectSearchPage/');

    The error is probably being caused in whatever method is calling the above code. It should check that a valid object is returned. Note that Director::redirect doesn't halt execution - so you still need to check if the method has returned a valid object before doing something with it.

    654 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.