Kioko Kiaza
Kioko Kiaza

Reputation: 1398

The method name must start with either findBy or findOneBy! (uncaught exception)

I´ve checked already this but my error seems to be different.

I´m getting this error:

[2012-05-07 14:09:59] request.CRITICAL: BadMethodCallException: Undefined method 'findOperariosordenados'. The method name must start with either findBy or findOneBy! (uncaught exception) at /Users/gitek/www/uda/vendor/doctrine/lib/Doctrine/ORM/EntityRepository.php line 201 [] []

This is my OperarioRepository:

<?php

namespace Gitek\UdaBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * OperarioRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class OperarioRepository extends EntityRepository
{
    public function findOperariosordenados()
    {
        $em = $this->getEntityManager();
        $consulta = $em->createQuery('SELECT o FROM GitekUdaBundle:Operario o
                                        ORDER BY o.apellidos, o.nombre');

        return $consulta->getResult();
    }    
}

This my controller, where I call the repository:

$em = $this->getDoctrine()->getEntityManager();
$operarios = $em->getRepository('GitekUdaBundle:Operario')->findOperariosordenados();   

Finally, this is my Entity:

<?php

namespace Gitek\UdaBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Gitek\UdaBundle\Entity\Operario
 *
 * @ORM\Table(name="Operario")
 * @ORM\Entity(repositoryClass="Gitek\UdaBundle\Entity\OperarioRepository")
 */
class Operario
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $nombre
     *
     * @ORM\Column(name="nombre", type="string", length=255)
     */
    private $nombre;
    ----
    ----

Any help or clue??

Thanks in advance

EDIT: Works fine on dev environment, but no in prod environment.

Upvotes: 3

Views: 16842

Answers (3)

Daishi
Daishi

Reputation: 14229

My app/config/config_prod.yml has a cache driver specified for doctrine :

doctrine:
    orm:
        metadata_cache_driver: apc
        result_cache_driver: apc
        query_cache_driver: apc

I cleared APC cache using these function calls :

if (function_exists('apcu_clear_cache')) {
    // clear system cache
    apcu_clear_cache();
    // clear user cache
    apcu_clear_cache('user');
}

if (function_exists('apc_clear_cache')) {
    // clear system cache
    apc_clear_cache();
    // clear user cache
    apc_clear_cache('user');
    // clear opcode cache (on old apc versions)
    apc_clear_cache('opcode');
}

And emptied app/cache/ directory.

But I kept getting this error in the prod environment while everything was fine in the dev environment.

I finally rebooted my virtual server and that did the trick.

Which definitely leads me to suspect a cache problem. Next time I will try to (gracefuly) restart the web server only, as that also clears the cache (php - Does a graceful Apache restart clear APC? - Stack Overflow)

Otherwise, setting apc.stat = 1 (http://php.net/manual/en/apc.configuration.php#ini.apc.stat) in /etc/php5/apache2php.ini also seems to be a good idea as suggested here : do we need to restart apache + APC after new version deployment of app?

UPDATE

My development server has APC installed and not APCu. The first two calls to apcu_clear_cache() were causing a PHP Fatal error, which in turn prevented the APC cache from being cleared.

So check which cache your system uses before issuing calls to apcu_clear_cache() or apc_clear_cache(). After that, no need to restart the virtual machine nor the web server to clear the cache and get rid of the nasty exception.

Addded if blocks to run APC or APCu specific functions.

Upvotes: 0

renoirb
renoirb

Reputation: 559

You already are in a reposoritory, you do not need to re-get it.

All methods in a *Repository can be used as with $this

Also, note

  • Query Builder or Hand made Query is way too much work when a simple return $this->findBy(); can be used.
  • findBy() has three parameters, first is an array of relations and getters, the second is for ordering, see Doctrine\ORM\EntityRepository code
  • Instead of using Raw queries... try the query builder FIRST. Look at my sample.

Your code

I would suggest you simply do:

public function findOperariosordenados()
{
    $collection = $this->findBy( array(), array('apellidos','nombre') );
    return $collection;
} 

You only need EntityRepository

One of my repositories:

Things to note:

  • Order has a relationship as $owner using the User Entity
  • If you REALLY need an array, in $array = $reposiroty->getOneUnhandledContainerCreate(Query::HYDRATE_ARRAY)
  • The ContainerCreateOrder is an extend of Order in a @ORM\InheritanceType("SINGLE_TABLE"). Quite out of scope of this question though.

It could be helpful:

 <?php

namespace Client\PortalBundle\Entity\Repository;


# Internal
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Query;
use Doctrine\Common\Collections\ArrayCollection;


# Specific


# Domain objects


# Entities
use Client\PortalBundle\Entity\User;


# Exceptions



/**
 * Order Repository
 *
 *
 * Where to create queries to get details
 * when starting by this Entity to get info from.
 *
 * Possible relationship bridges:
 *  - User $owner Who required the task
 */
class OrderRepository extends EntityRepository
{

    private function _findUnhandledOrderQuery($limit = null)
    {
        $q = $this->createQueryBuilder("o")
                ->select('o,u')
                ->leftJoin('o.owner', 'u')
                ->orderBy('o.created', 'DESC')
                ->where('o.status = :status')
                ->setParameter('status',
                    OrderStatusFlagValues::CREATED
                )
                ;

        if (is_numeric($limit))
        {
            $q->setMaxResults($limit);
        }
        #die(var_dump( $q->getDQL() ) );
        #die(var_dump( $this->_entityName ) );
        return $q;
    }


    /**
     * Get all orders and attached status specific to an User
     *
     * Returns the full Order object with the
     * attached relationship with the User entity
     * who created it.
     */
    public function findAllByOwner(User $owner)
    {
        return $this->findBy( array('owner'=>$owner->getId()), array('created'=>'DESC') );
    }



    /**
     * Get all orders and attached status specific to an User
     *
     * Returns the full Order object with the
     * attached relationship with the User entity
     * who created it.
     */
    public function findAll()
    {
        return $this->findBy( array(), array('created'=>'DESC') );
    }



    /**
     * Get next unhandled order
     *
     * @return array|null $order
     */
    public function getOneUnhandledContainerCreate($hydrate = null)
    {
       return $this->_findUnhandledOrderQuery(1)
                    ->orderBy('o.created', 'ASC')
                    ->getQuery()
                    ->getOneOrNullResult($hydrate);
    }



    /**
     * Get All Unhandled Container Create
     */
    public function getAllUnhandledContainerCreate($hydrate = null)
    {
       return $this->_findUnhandledOrderQuery()
                    ->orderBy('o.created', 'ASC')
                    ->getQuery()
                    ->getResult($hydrate);
    }
}

Upvotes: 7

ilanco
ilanco

Reputation: 9957

Did you clear your cache?

php app/console cache:clear --env=prod --no-debug

Upvotes: 5

Related Questions