We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Namespaces in model generation

Hi phacon comunity,

I wonder wether there is a way for the developper tool to write namespaces while models are being generated.

E.g : phalcon all-models --namespaces AppName\Models

Tryed to get some doc with phalcon all-models --help But no help available

Thanks

What about using 'namespace' option (without hyphens)?

Tryed phalcon all-models namespace App\Models

But still no namespaces

Hi SneakyBobito, I've checked with 'all-models' and yes, there is no namespacing support for this command. I've done pull request to devtools repo about adding this feature. Take a look at https://github.com/phalcon/phalcon-devtools/pull/86 Btw, if you're using command prompt like bash, you should escape slashes like phalcon all-models --namespace=My\Default\Namespace

Thanks Vladimir.

I hope the pull will be fast.

Ok for the \ escaping. Indeed i didn't consider it.

Thanks again :)

Done https://github.com/phalcon/phalcon-devtools/pull/86

example usage:

// for namespace CoreApp;
phalcon all-models  --namespace CoreApp

// for namespace CoreApp;
phalcon all-models  --namespace=CoreApp

// for namespace CoreApp\Model;
phalcon all-models  --namespace=CoreApp\\Models

I think you forgot thiomething :

when relations are created namespace should be also used, right ?

// with namespace
$this->belongsTo("test1_idtable1", "Rag\Model\Test1", "idtable1");
// without
$this->belongsTo("test1_idtable1", "Test1", "idtable1");

I've just tried to use devtools 1.2.4 to generate models with namespaces. Models were generated properly, but relationships between objects did was not included, just as SneakyBobito stated.

edited Jul '14

I know this is an old thread, but just a pro-tip for anyone using namespaces with Models and relationships, if you're planning on using Implicit Transactions to save model with related records, then you have to set an alias the for relationship definition.

eg. If you have a Model called 'Client', with a 'hasMany' definition to the model 'Contact' (let's say Column/Field name referencing back to Client is 'client_id')

Definitions should be like so:

(Client Model)

$this->hasMany('id', 'App\Contact', 'client_id', NULL);

(Contact Model)

$this->belongsTo('client_id', 'App\Client', 'id', array('foreignKey' => true, 'alias' => 'client'));

Then you can do implicit transactions again like:

$client = new App\Client();
$contact = new App\Contact();
$contact->name = 'Bob';
$contact->client = $client;
$contact->save();

and you can still explicity reference

$contact->client_id = 1;

If need be.

I don't think this was thoroughly covered in the Documentation, but this was my findings anywho. Hopefully this helps someone :)