How to check if category doesn't exist?

php symfony symfony1 symfony-1.2

91 观看

1回复

50 作者的声誉

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;

    ...
  }
作者: Dev Troubleshooter 的来源 发布者: 2017 年 12 月 27 日

回应 1


1

7402 作者的声誉

决定

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

作者: ReynierPM 发布者: 2017 年 12 月 27 日
32x32