Symfony2 Accessing Custom Classes

I am having trouble accessing a custom class that I created in a new folder in the 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 this directory, I have a MemberModel.php file with the following code:

<?php
namespace My\MemberBundle\Models;

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

      

When I try to access this class from my controller like this:

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

      

I am getting the error:

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

      

Can anyone point me in the right direction?

+3


source to share


2 answers


It turns out I didn't use the full path as needed. Both paths needed a "Bundle" added to them.

I had to use 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);

      

+1


source


I tried to plug in an old class that had an underscore in the class name and couldn't get the class to load. Apparently underscores are special characters when it comes to class name and are treated as directory separators or something along those lines.

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



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

Uf. It took too long to figure it out.

+1


source







All Articles