Extend the library
Opened this issue · 7 comments
Hi,
I've created a new model file in models directory called Employee as described on readme file.
The content of the file is:
I think I've figured out.
I've updated the composer.json file to include models directory for autoload and created a pull request.
Regards
The issue I am having is that the vendor directory gets compiled on the production server so any extensions I make on development will never make production. I cannot reference to a different namespace within the core code and obviously my new classes will not be autoloaded with 'Daursu/Xero/Models' namespace. Any changes I make will be a complete hack, have you got any suggestions or a way not to reference a to class using 'NAMESPACE' ?
Alternatively we can make new classes on a fork and create pull requests but I'm guessing this isn't ideal.
You can easily create a new folder in your main app, for example xero. Inside you can create your model extensions. Here's an example for CreditNote.php
:
// File CreditNote.php
<?php
use Daursu\Xero\Models\BaseModel;
class CreditNote extends BaseModel {
/**
* The name of the primary column.
*
* @var string
*/
protected $primary_column = 'CreditNoteID';
}
As you can see, the PHP file is not namespaced and you can easily use it within your app without any namespace.
@Daursu from my (brief) experimentation, it seems like the above code is broken because getFullClassName
will still return
Daursu\Xero\Models\CreditNote
which is the wrong thing. At present, I think, user-created models need to be namespaced in Daursu\Xero\Models
to work properly. This shouldn't be the case.
getFullClassName
should probably be removed and the calls to $this->getFullClassName()
replaced with get_class($this)
, which is built into PHP and does the same thing, only correctly.
Nevermind, get_class
isn't a valid substitute because getFullClassName
is on the collection but trying to get the class name of the model. I follow now.
The existing code is still broken; we need to somehow pass the class name to a Collection
when creating one.
I've refactored the code to fix the issue within the collection and I've pushed the changes on the develop branch
, however I've noticed that the setRelationship
method still uses the local namespace to resolve classes.
This requires a new section in the config file where users can define aliases for classes they've created.
For now you can load the develop branch using composer like so:
"require": {
"daursu/xero": "dev-develop"
}
So I went on and I've added aliases to the config file so people can define the correct classes for each model. I've pushed all the changes onto the develop branch. I will merge them onto the master once I get a chance to fully test everything.