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

12
  • Did you test a findAll() on your repo? Does it work? Also what does a get_class on the $userRepo gives you? Commented Feb 13, 2015 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) Commented Feb 13, 2015 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? Commented Feb 13, 2015 at 14:31
  • I have updated the original post with the complete AvailabilityAlert entity minus the setters and getters. Commented Feb 13, 2015 at 14:43
  • 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 <entity> in your .orm.xml ? (cf: symfony.com/doc/current/book/… when you click on the xml tab) Commented Feb 13, 2015 at 15:18

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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
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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.