• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DMZ - How to resolve object naming conflict?

#1
[eluser]nottRobin[/eluser]
I'm using DataMapper OverZealous Version (DMZ) for my Object Relational Mapping.

The standard way of instantiating a Model object 'participant' in DMZ is:
Code:
var $participant = new Participant();

However, this doesn't work if my controller class has the same name, because it will return an instance of the class itself:
Code:
class Participant extends Controller {
    function index() {
        $p = new Participant();
        $p->get(); // Error - no such method "get"
    }
}

This is frustrating as it's quite important, semantically, for my site that both the Model and the Controller are called "participant".

Does anyone know how I can access the DMZ Model object in another way or something?

Thanks,
Robin.

#2
[eluser]nottRobin[/eluser]
The Model can actually be named anything, so the solution I've gone with for the time being is to prefix all my model classes with 'Model_'. It's not an ideal solution as it requires a fair bit of extra typing, and extra code, as all my relationships now have to be explicitly defined:

Code:
class Model_participant extends DataMapper {
    var $table = 'participant';
    var $has_one = array('organisation' => array('class' => 'organisation', 'other_field' => 'participant'));
}
/* End of file model_participant.php */

This then allows my controller to work fine:

Code:
class Participant extends Controller {
    function index() {
        $p = new Model_participant();
        $p->get(); // No error :)
    }
}
/* End of file participant.php */

I'd still love a more elegant solution though, so I don't have to write all the extra code.

#3
[eluser]jayrulez[/eluser]
using php native namespace can remedy the situation.

#4
[eluser]nottRobin[/eluser]
Thanks jayrulez. I was wondering about that. Can you show me how that would work? Is it this?
PHP Namespaces overview

In which case it's only available in PHP 5.3 onwards. I'm a bit wary of 5.3 as it causes problems with some other PHP sites I'm running. Do you know if there's a way to simulate namespaces in PHP 5.2?

#5
[eluser]nottRobin[/eluser]
I installed PHP 5.3.2 just so I could take advantages of namespaces. Then I added "namespace Model;" to the top of my Model class.
Code:
namespace Model;
class Model_participant extends \DataMapper {
    var $table = 'participant';
    var $has_one = array('organisation' => array('class' => 'organisation', 'other_field' => 'participant'));
}
/* End of file model_participant.php */
And changed the reference to it:
Code:
class Participant extends Controller {
    function index() {
        $p = new \Model\Model_participant();
        $p->get(); // No error :)
    }
}
/* End of file participant.php */
However, I get this error:
Code:
Fatal error: Class 'Model\Model_participant' not found in /path/to/application/controllers/participant.php on line 22
After much investigation I discovered that the reason for this is that the class Model_participant doesn't actually get loaded at compile-time. It somehow gets stored by DMZ until it's needed and then loaded. And DMZ thinks it's called Model_participant, not Model\Model_participant.

I don't think there's an easy solution to this so I should probably give up. If anyone does have any suggestions please go ahead and let me know Smile

Robin.

#6
[eluser]oooobs[/eluser]
[quote author="nottRobin" date="1268821410"]I installed PHP 5.3.2 just so I could take advantages of namespaces. Then I added "namespace Model;" to the top of my Model class.
Code:
namespace Model;
class Model_participant extends \DataMapper {
    var $table = 'participant';
    var $has_one = array('organisation' => array('class' => 'organisation', 'other_field' => 'participant'));
}
/* End of file model_participant.php */
And changed the reference to it:
Code:
class Participant extends Controller {
    function index() {
        $p = new \Model\Model_participant();
        $p->get(); // No error :)
    }
}
/* End of file participant.php */
However, I get this error:
Code:
Fatal error: Class 'Model\Model_participant' not found in /path/to/application/controllers/participant.php on line 22
After much investigation I discovered that the reason for this is that the class Model_participant doesn't actually get loaded at compile-time. It somehow gets stored by DMZ until it's needed and then loaded. And DMZ thinks it's called Model_participant, not Model\Model_participant.

I don't think there's an easy solution to this so I should probably give up. If anyone does have any suggestions please go ahead and let me know Smile

Robin.[/quote]


I have the same problem? Smile I wish I know how to solve this Datamapper namespace issue


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.