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

PageRepository find by category #8

Open
matudelatower opened this issue Jun 28, 2017 · 6 comments
Open

PageRepository find by category #8

matudelatower opened this issue Jun 28, 2017 · 6 comments
Assignees

Comments

@matudelatower
Copy link

When find pages by category.

must be add ->andWhere('page.enabled = true') like:

public function findByCategory(Category $category, $order, $orderBy, $page, $limit): Paginator
    {
        $qb = $this->createQueryBuilder('page')
            ->where('page.category = :category')
            ->andWhere('page.enabled = true')
            ->orderBy('page.'.$orderBy, $order)
            ->setMaxResults($limit)
            ->setFirstResult($limit * ($page-1))
            ->setParameter('category', $category)
        ;

        return new Paginator($qb->getQuery()->useResultCache($this->cacheEnabled, $this->cacheTtl));
    }
@matudelatower
Copy link
Author

And $orderBy param comes with createdAt value from CategoryController->indexAction() and throws:

captura de pantalla 2017-06-28 a la s 15 47 54

in current($validOrderFields) gives createdAt if i use id instead works fine, or remove ->orderBy('page.'.$orderBy, $order) from de function above

@Pierstoval
Copy link
Member

Thanks for the report, I'll investigate on this 👍

@Gyvastis
Copy link

Gyvastis commented Aug 16, 2017

Any fix for this yet? I'm using this hack for now.

@matudelatower
Copy link
Author

@Gyvastis im solve overriding the repository

namespace WebBundle\Repository;

use Doctrine\ORM\Tools\Pagination\Paginator;
use Orbitale\Bundle\CmsBundle\Repository\PageRepository;
use Orbitale\Bundle\CmsBundle\Entity\Category;
use Orbitale\Bundle\CmsBundle\Entity\Page;

/**
 * PaginaRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class PaginaRepository extends PageRepository {

	/**
	 * @param Category $category
	 * @param string $order
	 * @param string $orderBy
	 * @param int $page
	 * @param int $limit
	 *
	 * @return Paginator
	 */
	public function findByCategory( Category $category, $order, $orderBy, $page, $limit ): Paginator {
		$qb = $this->createQueryBuilder( 'page' )
		           ->where( 'page.category = :category' )
		           ->andWhere( 'page.enabled = true' )
//		           ->orderBy( 'page.' . $orderBy, $order )
                   ->setMaxResults( $limit )
		           ->setFirstResult( $limit * ( $page - 1 ) )
		           ->setParameter( 'category', $category );

		return new Paginator( $qb->getQuery()->useResultCache( $this->cacheEnabled, $this->cacheTtl ) );
	}

and changing the repository mapping in entity

 * @ORM\Entity(repositoryClass="WebBundle\Repository\PaginaRepository")
 */
class Pagina extends BasePage implements AmpInterface {

@Gyvastis
Copy link

@matudelatower but you're messing up the order of page items inside a category page, no?

@matudelatower
Copy link
Author

@Gyvastis yes, but they always came in order by id by default

@Pierstoval Pierstoval self-assigned this Aug 16, 2017
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

3 participants