phpsymfonysymfony1

How to check if category doesn't exist?


I have a blog post URL as follow:

/blog/testing-url

which was linked to a category & that category has been deleted! What i want now is to show the post. The category of this post has been deleted but how can i check if the category doesn't exists? I tried the following

if(isset($category)) but this isn't working.

Error:

Fatal error: Call to a member function getCategory() on a non-object in /httpdocs/apps/mainv2/modules/blog/actions/actions.class.php on line 168

This is a piece of code where the error is happening:

  public function executeEdit(sfWebRequest $request) { #edit video
    ...

    $this->nrpag = $request->getParameter('nrpag', 1);
    $cats = array(); #video_cat
    $c = new Criteria();
    $c->addAscendingOrderByColumn(VideoCatPeer::CATEGORY);
    $vcats = VideoCatPeer::doSelect($c);
    foreach ($vcats as $vcat) {
        if($vcat != NULL){
            $cats[$vcat->getId()] = $vcat->getCategory();
        }else{
            continue;
        }
    }
    $this->cats = $cats;

    ...
  }

Solution

  • You can check whether the object $vcat has an instance of the Category entity and do something in such case. Ex:

    foreach ($vcats as $vcat) {
        if($vcat != NULL && $vcat->getCategory() instanceof Category){
            $cats[$vcat->getId()] = $vcat->getCategory();
        } 
    }
    

    I haven't tried is just a though. Let me know if that worked