Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isValidEntry() - Validate entry belongs to category? #33

Open
ghost opened this issue May 5, 2016 · 2 comments
Open

isValidEntry() - Validate entry belongs to category? #33

ghost opened this issue May 5, 2016 · 2 comments

Comments

@ghost
Copy link

ghost commented May 5, 2016

Lets say I've got the url /blog/:category/:entry/, is there anyway using isValidEntry() to validate that the entry is assigned to the category?

@rsanchez
Copy link
Owner

rsanchez commented May 5, 2016

You can't with isValidEntry, but you can with a custom query:

$config['resource_router'] = array(
  'blog/:category/:entry' => function($router, $wildcard1, $wildcard2) {
    if (! $wildcard2->isValidEntry()) {
      return $router->set404();
    }

    $isValidCategory = ee()->db->where('entry_id', $wildcard2->value)
      ->where('cat_url_title', $wildcard1->value)
      ->join('categories', 'categories.cat_id = category_posts.cat_id')
      ->count_all_results('category_posts') > 0;

    if (! $isValidCategory) {
      return $router->set404();
    }

    $router->setTemplate('blog/_entry');
  },
);

@ghost
Copy link
Author

ghost commented May 9, 2016

Thanks that looks good to me, although is there a way to url_title's instead? I tried changing it to...

$isValidCategory = ee()->db->where('url_title', $wildcard_2->value)
    ->where('cat_url_title', $wildcard_1->value)
    ->join('categories', 'categories.cat_url_title = category_posts.cat_url_title')
    ->count_all_results('category_posts') > 0;

And I'm getting the error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'url_title' in 'where clause':
SELECT COUNT(*) AS `numrows` FROM (`exp_category_posts`) JOIN `exp_categories` ON
`exp_categories`.`cat_url_title` = `exp_category_posts`.`cat_url_title` 
WHERE `url_title` = 'london-pbfa-shubiz' AND `cat_url_title` = 'european-footwear-accessories-gifts'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant