3

I am having trouble accessing a custom class I created in a new folder in a bundle.

I have a bundle called: MemberBundle - located at src/My/Bundle/MemberBundle

I created a directory called Models located at src/My/Bundle/MemberBundle/Models

In that directory I have a file called MemberModel.php with the following code:

<?php
namespace My\MemberBundle\Models;

class MemberModel {
    public function getActiveCampaignId($zone) {
    ### Custom Mysql Query
    ...
    }
}

When I try and access that class from my controller like this:

    $MemberModel = new My\MemberBundle\Models\MemberModel();
    $data = $MemberModel->getActiveCampaignId("1");
    print_r($data);

I get an error:

Fatal error: Class 'My\MemberBundle\Models\MemberModel' not found in ...

Could anyone please point me in the right direction?

7
  • You seem to have an extra Bundle directory. Is there a reason for that? Commented Feb 7, 2013 at 15:43
  • I am new to Symfony2 so must have been from a how-to guide or I saw other paths were set up like that (i.e. Symfony\Bundle\FrameworkBundle\Controller\Controller) - not exactly sure. Commented Feb 7, 2013 at 15:51
  • Download the Symfony Standard Edition and it'll give you a good base to start with. You can browse it online as well. But basically I think your issue is that your namespace is off. Remove your Bundle directory and put MemberBundle as a child of My Commented Feb 7, 2013 at 16:07
  • I remember why I did it now: symfony.com/doc/current/cookbook/bundles/… . Adding the 'Bundle' directory after 'My' does not seem to be causing namespace issues anywhere else. Commented Feb 7, 2013 at 16:14
  • 1
    Though there is not really a convention to if you want to use the Bundle in the middle or not. We had a briefing by some people from Sensio and they used the Bundle in the middle every time. In most bundles though it isn't used that often. Edit Actually the SymfonyBundle itself uses the Bundle in the middle. Commented Feb 8, 2013 at 16:04

2 Answers 2

1

It turns out I was not using the full path as needed. Both paths needed 'Bundle' added into them.

I should have been using these two bits of code:

<?php
namespace My\Bundle\MemberBundle\Models;

class MemberModel {
    public function getActiveCampaignId($zone) {
    ### Custom Mysql Query
    ...
    }
}

And:

    $MemberModel = new My\Bundle\MemberBundle\Models\MemberModel();
    $data = $MemberModel->getRandomActiveCampaignId("1");
    print_r($data);
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, your solution is probably the simpler and cleaner one. I totally overlooked the convention-based autoloading :)
1

I tried to attach older class which had underscore in class name and couldn't get the class to load. Apparently underscores are special chars when it comes to class name and are treated as directory separators or something in those lines.

More information @ http://www.sitepoint.com/autoloading-and-the-psr-0-standard/

Each underscore in the class name is converted to a DIRECTORY_SEPARATOR. The underscore has no special meaning in the namespace.

Phew. This took me waaaaay too long to figure it out.

1 Comment

This answer add substantial information for solving the problem outlined in the OP question. Maybe it should be added as a footnote of the question.

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.