Yii 2 Custom Module Namespace for Models - module

Is there anyway to change the namespace of the Module and use it as a namespace of my models?
I'm building a project using the basic app template of yii2.
What I want is to have a centralized namespace so that when I use the module into different project and reuse the models inside it, (either basic or advanced template), I won't have to change the namespace based on its root directory.

This will possible with Yii2 aliases.
first put all your models in your custom folder and set it alias name Yii config file. like below
// an alias of a file path
Yii::setAlias('#myFolder', '/path/to/foo');
finally, update your all models namespace based on this alias one time. like below.
namespace myFolder\models\xxx;
now you can copy this folder to any yii2 app and just set alias name, like above i mention.
Note:- directory will be independent like below and namespace will based on this directory.
folderName
->models
->xxxxx
->xxxxx
.....
// and namespace should add all the models like below
namespace folderName\models;
// while using these models, you can import like below
import myFolder\xxxxxx;

You can edit the composer.json file find
{
.....
"autoload": {
"psr-4": {
"App\\": "src/" #<-key=namespace value=dirname
}
}
......
}
run composer update

Related

How to inherit OrderReceipt file from PoS app

I'm trying to inherit OrderReceipt.xml file from PoS to change the way cashier name is displayed. So far I tried this -> put the file in my module in this location: static > src > XML > orderReceipt_inherited.xml
Inside of it is this code:
And inside of my manifest file I am calling it with qweb like this:
I got no luck with it, changes are not showing on receipt. Can someone please help. Odoo version is 15.
You need to use the 'point_of_sale.assets' entry under assets.
You can find an example in the l10n_co_pos module
'assets': {
'point_of_sale.assets': [
'l10n_co_pos/static/src/js/**/*',
'l10n_co_pos/static/src/xml/**/*',
],
},
You need also to use the module name in the file path as you can see in the example above

Define a NameSpace in VB

I want to define a unified namespace for all my classes and put them all in it.
I did it with
Namespace
...
End Namespace
But now I can not import it in other projects, also I can not import it in another file in my main project (That file is not in my namespace it is just UI).
Thank you in andvance !
Your project has a root namespace defined in the project properties. Let's say this root namespace is MyApp.MyProject.
Then inside this project, you have a class defined like this:
Namespace MyNamespace
Public Class MyClass...
End Namespace
If you want to use MyClass inside another class, you would just add the following Imports statement at the top of the file:
Imports MyApp.MyProject.MyNamespace
You could also define a Namespace globally outside of the root namespace of your project with Global like so:
Namespace Global.MyNamespace
...
End Namespace
More information can be found in the .NET documentation

How to create global attribute in Yii2 application

I am using Yii2 and want to create a attribute and access this attribute in every where in my project for exp:
Yii::$app->name
Above is an example of yii2 default name.
Is there any way to declare my custom attribute as following :
Yii::$app->supportname
You can extend Application class, add required properties, and use it on bootstrap.
Create class for web:
class MyApplication extends \yii\web\Application {
public $supportname;
}
And use it in index.php:
(new MyApplication($config))->run();
You need to do the same for \yii\console\Application and yii script.
But probably the best way is to use Application::$params and access value by Yii::$app->params['supportname'].

Crud for existing model in YII

I am trying to create CRUD for my model classed generated automatically by GII.
Classes are stored like models/entity/Article.php. When i try to Gii generator put as class name models.entity.SomeClass or entity.SomeClass it doesn`t work.
The gii stops with exception Alias "entity.Article" is invalid. Make sure it points to an existing directory or file.
How can I run CRUD script properly?
Set the alias in your config/main.php file (do this before the return array):
Yii::setPathOfAlias('entity',dirname(__FILE__).DIRECTORY_SEPARATOR.'../models/entity');
// rest of config
return array(
// ...
);
Then use entity.Article in gii.

Dynamic including/autoload of external TypoScript templates

Is it possible to include automatically all external TS templates in some folders to avoid manual linking ?
Instead of writing each time manually INCLUDE_TYPOSCRIPT like this :
Constants :
<INCLUDE_TYPOSCRIPT: source="FILE:fileadmin/templates/typoscript/setup/1.ts">
<INCLUDE_TYPOSCRIPT: source="FILE:fileadmin/templates/typoscript/setup/2.ts">
...
<INCLUDE_TYPOSCRIPT: source="FILE:fileadmin/templates/typoscript/setup/999.ts">
Setup :
<INCLUDE_TYPOSCRIPT: source="FILE:fileadmin/templates/typoscript/constants/1.ts">
<INCLUDE_TYPOSCRIPT: source="FILE:fileadmin/templates/typoscript/constants/2.ts">
...
<INCLUDE_TYPOSCRIPT: source="FILE:fileadmin/templates/typoscript/constants/999.ts">
I would like to have something like this instead of manual include file per file :
Constants :
<INCLUDE_TYPOSCRIPT_ALL: source="FILE:fileadmin/templates/typoscript/constants/">
Setup :
<INCLUDE_TYPOSCRIPT_ALL: source="FILE:fileadmin/templates/typoscript/setup/">
Recursive option could be helpful also.
Is it possible, or I need to hack the core to realize such functionality ?
The only way is including sub-files in one main file. It's similar approach like using #import in CSS files for including other substyles.
Then you can just include one file in your TS:
<INCLUDE_TYPOSCRIPT: source="FILE:fileadmin/templates/typoscript/setup/main.ts">
and at the main.ts you can paste additional includes