Pathfinder: accessing Admin models from Frontend - atk4

In my application (residing in a subdirectory of webroot) I have an Admin area with models for category type lookups. In the main frontend application I have other models with references to the lookups defined in Admin.
Eg. Model Expense is defined in the Frontend, it has a reference to Model ExpenseType (which is a subclass of Model Lookup defined in Admin/lib/Model:
webroot
My atk4 project
-admin
-lib
-Model
-Model_Lookup with subclass ExpenseType
-atk4
-atk4-addons
..
-lib
-Model
-Model_Expense with reference to model ExpenseType
...
When opening the expenses page using CRUD -> setModel('Expense') I get a "fatal Error" - Model_ExpenseType cannot be found. So from what I understand (just starting with atk4) I need to addLocation to Pathfinder but I don't know how to do it so that Frontend models can access Admin models.

try
$this->pathfinder->addLocation('admin',array(
'php'=>'lib'
));
inside your Api::init()

I sometimes do like this:
given: webroot/lib/Model
cd webroot/admin/lib
ln -s ../../lib/Model .

Related

Organize Models in subdirectories CakePHP 3

we are using subdirectories in our projects no separete views and controllers but in models we didn’t learn yet. Recently I’ve found this https://github.com/cakephp/cakephp/issues/60451 and actually routes and plugins we are already using, we just want to separete our models like this:
Model
-Entity
–Financial
—Money.php
-Table
–Financial
—MoneyTable.php
I’ve tryed put like this then controller is not able to find his model. How can I do to organize it, and make it work?
Things that we've tried:
Use $this->setAlias('TableModel');
Call in controller:
$this->TableModel = $this->loadModel('Subfolder/TableModel');
didn't work for SQL build, and other classes.
CakePHP uses the TableRegister to load models. That class can be configured to use a class that implements the LocatorInterface, and CakePHP uses the TableLocator as the default.
The only thing you can do is configure your own LocatorInterface instance in your bootstrap.php. You would have to create your MyTableLocator and have it change the className for tables to point to subdirectories. What rules for this class name rewritting are used is purely up to you.
bootstrap.php:
TableRegister::setTableLocator(new MyTableLocator());
MyTableLocator.php:
class MyTableLocator extends TableLocator {
protected function _getClassName($alias, array $options = [])
{
if($alias === 'Subfolder/TableModel') {
return TableModel::class;
}
return parent::_getClassName($alias, $options);
}
}
The above isn't working code.
I'm just demonstrating what the function is you need to override, and that you need logic in place to return a different class name.
You can check if the $alias contains the / character, and if so. Return a class name by extracting the subfolder name from the $alias. Take a look at the TableLocator to see how it's using the App::className function.

Template file not created after adding Page Class to models.py

So I'm trying to get started with Wagtail, following the 10 minute guide but I'm having a problem creating A basic blog.
So after modifying blog/models.py:
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel
class BlogIndexPage(Page):
intro = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('intro', classname="full")
]
And running makemigrations and migrate:
$ python manage.py makemigrations
Migrations for 'blog':
blog/migrations/0001_initial.py:
- Create model BlogIndexPage
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, home, sessions, taggit, wagtailadmin, wagtailcore, wagtaildocs, wagtailembeds, wagtailforms, wagtailimages, wagtailredirects, wagtailsearch, wagtailusers
Running migrations:
Applying blog.0001_initial... OK
The default template (blog/templates/blog/blog_index_page.html) is nowhere to be found. From what I understand, this file should be created automatically:
Since the model is called BlogIndexPage, the default template name (unless we override it) will be blog/templates/blog/blog_index_page.html:
Or perhaps I'm missing something?
Wagtail uses normal Django templates to render each page type. It automatically generates a template filename from the model name by separating capital letters with underscores (e.g. HomePage becomes home_page.html)
This is the expected behaviour. The template file / directory isn't created automatically on creating the page model - you have to add it yourself.

cakephp2x Model, View,Controller Naming Conventions

i am new in the cakephp2x and i dont know how to named the controller and view and model files in the cakephp 2x.
for the simple Event module i have
contoller : EventsController.php
model : Event.php
vide : Events ( folder Name )
now this look like http://example.com/events
Now my i want the link look like this http://example.com/itemsbidders
for the above url i have named the file as below
controller : ItemsBiddersController.php
model : ItemBidder.php
view : ItemsBidders
but i am getting 404 page not found Error in this page.
so can you tell me my mistake. advice me for this issue
Thanks in advance
With HABTM link tables you order model names alphabetically and first model kept plural:
Controller : BiddersItemsController.php
Model : BiddersItem.php
View : BiddersItems
And URL now looks like this: http://example.com/bidders_items (with underscore to separate models).
Remember, first letter capitalised and singular for folders: Controller / Model / View
Now to get URL you want above, you will need to create a router declaration in your Config/routes.php file like:
Router::connect('/itemsbidders',
array('controller' => 'bidders_items', 'action' => 'index'));

How do I rename a mongo collection in Mongoid?

I have a collection called artists, i'd like to rename it to artist_lookups. How do I do this?
With mongoid5 / mongo ruby driver 2:
# if you need to check whether foo exists
return unless Mongoid.default_client.collections.map(&:name).include?('foo')
# rename to bar
Mongoid.default_client.use(:admin).command(
renameCollection: "#{Mongoid.default_client.database.name}.foo",
to: "#{Mongoid.default_client.database.name}.bar"
)
Very simple, in mongo shell, do that:
db.artists.renameCollection( "artist_lookups" );
if you want to drop artist_lookups if it exist:
db.artists.renameCollection( "artist_lookups", true );
Some exception you can got.
10026 – Raised if the source namespace does not exist.
10027 – Raised if the target namespace exists and dropTarget is either false or unspecified.
15967 – Raised if the target namespace is an invalid collection name.
From the Mongoid Docs:
class Band
include Mongoid::Document
store_in collection: "artists", database: "music", session: "secondary"
end
Use store_in collection: "artist_lookups" in your model. This will let you store your Artist model in the artist_lookups collection.
If you want to preserve the existing data in the artists collection, and rename it, I suggest shutting down your app temporarily, renaming the collection to artist_lookups on your MongoDB server, and then restarting the app.
db.artists.renameCollection("artist_lookups")
will work for sure.

How do I migrate data from one model to another using South in Django?

I created a Django app that had its own internal voting system and a model called Vote to track it. I want to refactor the voting system into its own app so I can reuse it. However, the original app is in production and I need to create a data migration that will take all the Votes and transplant them into the separate app.
How can I get two apps to participate in a migration so that I have access to both their models? Unfortunately, the original and separate apps both have a model named Vote now, so I need to be aware of any conflicts.
Have you tried db.rename_table?
I would start by creating a migration in either the new or old app that looks something like this.
class Migration:
def forwards(self, orm):
db.rename_table('old_vote', 'new_vote')
def backwards(self, orm):
db.rename_table('new_vote', 'old_vote')
If that does not work you can migrate each item in a loop with something along these lines:
def forwards(self, orm):
for old in orm['old.vote'].objects.all():
# create a new.Vote with old's data
models = {
'old.vote' = { ... },
'new.vote' = { ... },
}
Note: You must use orm[...] to access any models outside the app currently being migrated. Otherwise, standard orm.Vote.objects.all() notation works.

Resources