Forest header image

Symfony Finland
Random things on PHP, Symfony and web development

How to search and find Content with the eZ Platform PHP API (part 1/3)

This is a code example from the eZ Publish / eZ Platform API Cookbook on how to search and find Content using the eZ Publish content API in Symfony Controllers or Commands:

 * File containing the FindContentCommand class.
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
 * @license For full copyright and license information view LICENSE file distributed with this source code.
 * @version //autogentag//
namespace EzSystems\CookbookBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

 * This command performs a simple full text search
 * @author christianbacher
class FindContentCommand extends ContainerAwareCommand
    protected function configure()
        $this->setName( 'ezpublish:cookbook:find_fulltext' )->setDefinition(
                new InputArgument( 'text', InputArgument::REQUIRED, 'Text to search for' )

    protected function execute( InputInterface $input, OutputInterface $output )
        /** @var $repository \eZ\Publish\API\Repository\Repository */
        $repository = $this->getContainer()->get( 'ezpublish.api.repository' );
        $searchService = $repository->getSearchService();

        $text = $input->getArgument( 'text' );

        $query = new \eZ\Publish\API\Repository\Values\Content\Query();
        $query->criterion = new Query\Criterion\FullText( $text );

        $result = $searchService->findContent( $query );
        $output->writeln( 'Found ' . $result->totalCount . ' items' );
        foreach ( $result->searchHits as $searchHit )
            $output->writeln( $searchHit->valueObject->contentInfo->name );

Also see other examples of working with the eZ Platform to find content:

Learn more in the eZ Platform documentation or the eZ Platform Tips section on this site.

Written by Jani Tarvainen on Wednesday March 30, 2016
Permalink -

« How to load and view Content Metadata with the eZ Platform PHP API - How to search and find Content with the eZ Platform PHP API (part 2/3) »