方法名称必须以findBy或findOneBy开头! (未捕获的异常)

时间:2012-05-07 12:15:35

标签: symfony

我已经检查了this,但我的错误似乎有所不同。

我收到此错误:

[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 [] []

这是我的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();
    }    
}

这是我的控制器,我称之为存储库:

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

最后,这是我的实体:

<?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;
    ----
    ----

任何帮助或线索??

提前致谢

编辑:在开发环境中工作正常,但在prod环境中没有。

3 个答案:

答案 0 :(得分:7)

你已经处于一个reposoritory,你不需要重新获得它。

*存储库中的所有方法都可以与$this

一起使用

另请注意

  • 当使用简单的return $this->findBy();时,查询生成器或手工查询的工作量太大了。
  • findBy()有三个参数,第一个是关系和getter数组,第二个是排序,请参阅Doctrine\ORM\EntityRepository code
  • 而不是使用Raw查询...首先尝试查询构建器。看看我的样本。

您的代码

我建议你这样做:

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

您只需要EntityRepository

我的一个存储库:

注意事项:

  • Order使用$owner实体与User的关系
  • 如果你真的需要一个数组,请$array = $reposiroty->getOneUnhandledContainerCreate(Query::HYDRATE_ARRAY)
  • ContainerCreateOrderOrder@ORM\InheritanceType("SINGLE_TABLE")的延伸。但是,这个问题的范围很大。

这可能会有所帮助:

 <?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);
    }
}

答案 1 :(得分:5)

你清除缓存了吗?

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

答案 2 :(得分:0)

我的app/config/config_prod.yml有一个为教义指定的缓存驱动程序:

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

我使用这些函数调用清除了APC缓存:

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');
}

清空app/cache/目录。

但是我仍然在prod环境中遇到这个错误,而在开发环境中一切都很好。

我终于重新启动了我的虚拟服务器,这就成功了。

这肯定会让我怀疑缓存问题。下次我将尝试(优雅地)重新启动Web服务器,因为这也会清除缓存(php - Does a graceful Apache restart clear APC? - Stack Overflow

否则,在apc.stat = 1中设置/etc/php5/apache2php.inihttp://php.net/manual/en/apc.configuration.php#ini.apc.stat)似乎也是一个好主意,如下所示:do we need to restart apache + APC after new version deployment of app?

<强>更新

我的开发服务器安装了APC而不是APCu。对apcu_clear_cache()的前两次调用导致PHP致命错误,从而阻止了APC缓存被清除。

因此,在向apcu_clear_cache()apc_clear_cache()发出呼叫之前,请检查系统使用的缓存。之后,无需重新启动虚拟机或Web服务器来清除缓存并摆脱令人讨厌的异常。

添加if块以运行APC或APCu特定功能。