Skip to content

Instantly share code, notes, and snippets.

@hissy
Created July 13, 2017 03:25
Show Gist options
  • Save hissy/9ec1c8b594edf0126951a52ef7778992 to your computer and use it in GitHub Desktop.
Save hissy/9ec1c8b594edf0126951a52ef7778992 to your computer and use it in GitHub Desktop.
[concrete5][V8][Express] Get Page by Express Attribute Value
<?php
namespace Application\Concrete\Express\Service;
use Concrete\Core\Application\Application;
use Concrete\Core\Attribute\Category\PageCategory;
use Concrete\Core\Cache\Level\RequestCache;
use Concrete\Core\Database\Connection\Connection;
use Concrete\Core\Database\DatabaseManager;
use Concrete\Core\Entity\Attribute\Key\PageKey;
use Concrete\Core\Entity\Express\Entry;
use Doctrine\DBAL\Query\QueryBuilder;
/**
* A helper class for relations with Express and Page
*
* @package Application\Concrete\Express\Service
*/
class Page
{
/** @var Application $app */
protected $app;
/** @var RequestCache $cache */
protected $cache;
public function __construct(Application $application)
{
$this->app = $application;
$this->cache = $application->make('cache/request');
}
/**
* @param PageKey $ak
* @param Entry $entry
* @return \Concrete\Core\Page\Page
*/
public function getPageByExpressAttributeValue(PageKey $ak, Entry $entry)
{
$item = $this->cache->getItem(sprintf('page/express/entry/%d', $entry->getID()));
$cID = $item->get();
if ($item->isMiss()) {
$category = $this->app->make(PageCategory::class);
/** @var Connection $db */
$db = $this->app->make(DatabaseManager::class)->connection();
/** @var QueryBuilder $qb */
$qb = $db->createQueryBuilder();
$qb
->select('cID')
->from($category->getIndexedSearchTable())
->where('ak_' . $ak->getAttributeKeyHandle() . ' = ?')
->setParameter(0, $entry->getID())
;
$sth = $qb->execute();
$cID = $sth->fetchColumn();
$this->cache->save($item->set($cID));
}
return \Concrete\Core\Page\Page::getByID($cID);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment