1

I have searched lots of answers but none of them seem to help me. Basically, when I try loading my page, I get the error

Undefined method 'findTotalMatches'. The method name must start with either findBy or findOneBy!

So obviously Symfony2/Doctrine is not finding my custom repository class. My package structure is like so Nick/AlertBundle/ then all my other directories.

In my Entity class, I have

<?php

namespace Nick\AlertBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints\DateTime;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * AvailabilityAlert
 *
 * @ORM\Table(name="availability_alert")
 * @ORM\Entity(repositoryClass="Nick\AlertBundle\Repository\AvailabilityAlertRepository")
 *
 */
class AvailabilityAlert
{

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="search_command", type="string", length=256, nullable=false)
     */
    private $searchCommand;

    /**
     * @var string
     *
     * @ORM\Column(name="is_connecting", type="string", length=20, nullable=false)
     */
    private $isConnecting;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="last_updated", type="datetime", nullable=false)
     */
    private $lastUpdated;

    /**
     * @var boolean
     *
     * @ORM\Column(name="is_deleted", type="boolean", nullable=false)
     */
    private $isDeleted;

    /**
     * @var string
     *
     * @ORM\Column(name="alert_status", type="string", length=11, nullable=false)
     */
    private $alertStatus;

}

So it is pointing to AvailabilityAlertRepository which is in my Repository directory. Using the command to generate this class didnt work for some reason (no error, just no class created) so I had to make it manually. All I am doing for testing purposes is

<?php

namespace Nick\AlertBundle\Repository;

use Doctrine\ORM\EntityRepository;

class AvailabilityAlertRepository extends EntityRepository
{
    public function findTotalMatches($keyword)
    {
        return 34;
    }
}

And then in my controller, I have

$keyword = "TEST";
$userRepo = $this->getDoctrine()->getRepository('NickAlertBundle:AvailabilityAlert');
var_dump($userRepo->findTotalMatches($keyword));

So everything from what I can see looks correct, however I still get the error mentioned at the beginning. I noticed some answers mentioned something about making sure that I am not using yml, not to sure how to check this though (and I have used annotations for everything else I think).

So am I missing something obvious here?

This is located at Resources/config/doctrine/AvailabilityAlert.orm.xml

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="Nick\AlertBundle\Entity\AvailabilityAlert" table="availability_alert">
    <id name="id" type="integer" column="id">
      <generator strategy="IDENTITY"/>
    </id>
    <field name="searchCommand" type="string" column="search_command" length="256" nullable="false"/>
    <field name="isConnecting" type="string" column="is_connecting" length="20" nullable="false"/>
    <field name="lastUpdated" type="datetime" column="last_updated" nullable="false"/>
    <field name="isDeleted" type="boolean" column="is_deleted" nullable="false"/>
    <field name="alertStatus" type="string" column="alert_status" length="11" nullable="false"/>
  </entity>
</doctrine-mapping>

Thanks

Nick Price
  • 953
  • 2
  • 12
  • 26
  • Did you test a `findAll()` on your repo? Does it work? Also what does a get_class on the `$userRepo` gives you? – Snroki Feb 13 '15 at 13:57
  • findAll() returns all the data. But if I create a findAll function in my custom repo and just return some random text, I still get all the data. So its ignoring my custom repo. Get class just returns string 'Doctrine\ORM\EntityRepository' (length=29) – Nick Price Feb 13 '15 at 14:28
  • Ok, then your repo isn't loaded by your entity (you should have your repo class name) can you show us the full AvailabilityAlert entity? – Snroki Feb 13 '15 at 14:31
  • I have updated the original post with the complete AvailabilityAlert entity minus the setters and getters. – Nick Price Feb 13 '15 at 14:43
  • Out of curiosity did you try moving you `Repository` dir and namespace segement under `Entity` so `Nick\AlertBundle\Entity\Repository\AvailabilityAlertRepository` and `src/Nick/AlertBundle/Entity/Repository/AvailabilityAlertRepository.php`? You shouldnt have to do this, but i wonder if maybe its a bug somewhere - Ive always kept my entities and repos under `Entity` and never had a problem with custom repos. – prodigitalson Feb 13 '15 at 14:53
  • Just moved it, same problem. Its a weird one, but I see a lot of people have problems with it. – Nick Price Feb 13 '15 at 15:00
  • Can you look inside your Resources folder if you have any .orm.yml or .orm.xml file? (usually it will be in : `Resources/config/doctrine/metadata/orm`) – Snroki Feb 13 '15 at 15:02
  • I have added something to the OP. There were some ORM files which I didnt even know about. Not sure if this matters? – Nick Price Feb 13 '15 at 15:06
  • do you have the line `auto_mapping: true` in the `doctrine: orm:` part of the config (config.yml) ? Maybe doctrine isn't using the annotation – Snroki Feb 13 '15 at 15:13
  • Yep, auto_Mapping is set to true. Should it be? – Nick Price Feb 13 '15 at 15:15
  • 1
    Nope it's okay! Well i don't really know why your annotation isn't loaded but can you try to add this ligne: `repository-class="Nick\AlertBundle\Entity\Repository\AvailabilityAlertRepository"` as the attribute of the `` in your .orm.xml ? (cf: http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes when you click on the xml tab) – Snroki Feb 13 '15 at 15:18
  • That seems to have fixed it, perfect, thanks. – Nick Price Feb 13 '15 at 15:29

2 Answers2

1

Usually this comes from the fact that your bundle is not mapped by doctrine:

  • Run the command php app/console doctrine:mapping:info to be sure that your bundle is mapped.
  • If is not mapped just add it in your config.yml file:

     orm:
            default_entity_manager: default
            entity_managers:
                default:
                    connection: default
                    mappings:
                        NickAlertBundle:  ~
    

and then check one more time your repository.

Other stuff that I observed in your code is: $userRepo = $this->getDoctrine()->getRepository('NickAlertBundle:AvailabilityAlert');

Try to change it in: $userRepo = $this->getDoctrine()->getManager()->getRepository('NickAlertBundle:AvailabilityAlert');

Alexandru Olaru
  • 6,842
  • 6
  • 27
  • 53
  • I have the following response from that command Found 5 mapped entities: [OK] Nick\AlertBundle\Entity\AvailabilityAlertBookingClass [OK] Nick\AlertBundle\Entity\AvailabilityAlertPseudos [OK] Nick\AlertBundle\Entity\AvailabilityAlertFlightNumbers [OK] Nick\AlertBundle\Entity\AvailabilityAlertAvailability [OK] Nick\AlertBundle\Entity\AvailabilityAlert – Nick Price Feb 13 '15 at 15:09
0

I think the issue is with your call to the repository. Your bundle is named 'AlertBundle' not 'NickAlertBundle'.

Trying calling your repository like this:

$er = $this->getManager()->getRepository('AlertBundle:AvailabilityAlert');

I hope this helps!

SA Alex
  • 46
  • 3