Abp One to many Relationship - abp

I have Book and Author Entity.
I tried to establish a relationship between Book and Author, but the AuthorId key did not appear. I wonder how it is done.
enter image description here

First of all, I think you are looking at the tutorials part of the ABP framework document. If you haven't looked, I think you should look at this.
As explained in the document, you can do this as follows:
Open the BookStoreDbContextModelCreatingExtensions class under the EntityFrameworkCore folder of the Acme.BookStore.EntityFrameworkCore project and change the builder.Entity part as shown below
builder.Entity<Book>(b =>
{
b.ToTable(BookStoreConsts.DbTablePrefix + "Books", BookStoreConsts.DbSchema);
b.ConfigureByConvention(); //auto configure for the base class props
b.Property(x => x.Name).IsRequired().HasMaxLength(128);
// ADD THE MAPPING FOR THE RELATION
b.HasOne<Author>().WithMany().HasForeignKey(x => x.AuthorId).IsRequired();
});

Related

How can i fetch dynamic data from database based on selected language.?

Hi i am working on a project in laravel 7.0, in back-end i have a table called Posts which contains 2 text language input one in french and the other is arabic added by the back-end application.
what i am trying to do is when the user uses the French Language i want the title_fr to be displayed on the view and same thing in Arabic language the title should be title_ar.
P.S data are stored in French and Arabic
I have tried the similar solution given in an other similar question but none of it worked in my case!
Any idea how i might get this to work ?
Thanks in advance.
You can do something similar to below. We have a model Post, this model has an attribute title. I also assume that you have an attribute that will return user's language from the User model.
class Post extends Model
{
public function getTitleAttribute(): string
{
return Auth::user()->language === 'fr' ? $this->title_fr : $this->title_ar;
}
}
FYI above is just a demo on what can be done. For a full blow solution I would recommend decorator pattern.
Also it might be worth considering using morph for things like that. You can have a service provider that will initiate the morph map for you post model relevant to the language that user has, I.e.
Class ModelProvider {
Protected $models = [
‘fr’ => [
‘post’ => App/Models/Fr/Post::class,
],
‘ar’ => [
‘post’ => App/Models/Ar/Post::class,
]
];
Public function boot() {
$language = Auth::user()->Settings->language;
Relation::morphMap($This->models[$language]);
}
}
Afterwards you just need to call to Relation::getMorphModel(‘post’) to grab Post class that will return correct language.
I.e. App/Models/Fr/Post can have a an attribute title:
Public function getTitleAttribute(): string {
Return $this->title_fr;
}
For example above you would also want to utilise interfaces to make sure that all models follow the same contract, something below would do the trick:
Interface I18nPostInterface {
Public function getTitleAttribute(): string
}
Also, depending on the database you use, to store titles (and other language data) in a JSON format in the database. MySQL 8 has an improve support for JSON data, but there are limitations with that.
So I was Able to fetch data from my database based on the Language selected by the user.
Like i said before I have a table called Posts and has columns id,title_fr and title_ar. I am using laravel Localization.
Inside my PostController in the index function i added this code:
public function index()
{
//
$post = Post::all();
$Frtitle = post::get()->pluck('title_fr');
$Artitle = post::get()->pluck('title_ar');
return view('post.index',compact('post','Frtitle','Artitle'));
}
if anyone has a better way then mine please let me know, i am sure
there is a better way.

Sequelize 2 Many to Many relationship

I have videos that can be liked by users and commented by users so I need 2 many to many associations.
User model
User.belongsToMany(models.Video,{ through: 'user_like_video' });
User.belongsToMany(models.Video, { through: 'user_comment_video' });
Video model
Video.belongsToMany(models.User, {through: 'user_like_video'});
Video.belongsToMany(models.User, {through: 'user_comment_video'});
My resulting schema in DB is :
The many to many relationship between video a tags work well I can use the method getTags() to retrieve all the tags from a video but how are created methods when you have 2 (many to many relationship) ?
When I use user.getVideos(), I only get the last relation registered which is here user_comment_video.
I finally find a way :
Video.belongsToMany(models.User, {through: 'user_like_video', as: 'Like'});
Video.belongsToMany(models.User, {through: 'user_comment_video',as: 'Comment'});
this exposes methods getLike() and getComment()

Cakephp not loading belongsToMany association beetween two Models

I just started my project with cakephp3.0. I startet with the Users. I put a function isAuthorized in the UsersController which should validate if the User is permitted to view other users, to edit other users and so on.
I created a table "userprivileges" where all possible privileges, which can be assigned to a user are listed.
The table is "userprivileges" is:
id | controller | function
Next I want to group the priviliges into roles.
I created following table: "userprivilegeroles"
id | role
Next I created the BTM-Table: "userprivileges_userprivilegeroles"
id | userprivilegerole_id | userprivilegerole_id
The User is linked to the userprivilegeroles table, so I need to search if the requested controller/function is view-able for the user.
I tried like this:
$this->loadModel('Userprivilegeroles');
$userprivilegeroles = $this->Userprivilegeroles->find('all', [
'conditions' => [
'Userprivileges.controller' => $this->request->controller,
'Userprivileges.function' => $this->request->action],
'contain' => ['Userprivileges']
]);
Now this fails with following error:
Userprivilegeroles is not associated with Userprivileges.
There is a little more description, where I can search for the problem prompted to the 500 page.
The class for the specified table does not exist.
The Table was created with a typo: TableRegistry::get('Atricles');
The class file has a typo in the name or incorrect namespace: class Atricles extends Table
The file containing the class has a typo or incorrect casing: Atricles.php
The Table was used using associations but the association has a typo: $this->belongsTo('Atricles')
The table class resides in a Plugin but no plugin notation was used in the association definition.
Lets go for it one by one:
The class for the specified table does not exist.
It does:
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class UserprivilegerolesTable extends Table
{
public function initialize(array $config){
$this->belongsToMany('Userprivileges', [
'joinTable' => 'userprivileges_userprivilegeroles',
'className' => 'Userprivileges'
]);
}
}
Next
The Table was created with a typo: TableRegistry::get('Atricles');
No.
The class file has a typo in the name or incorrect namespace: class Atricles extends Table
no.
The file containing the class has a typo or incorrect casing: Atricles.php
no. its in src/Model/Table/Userprivilegeroles.php
The Table was used using associations but the association has a typo: $this->belongsTo('Atricles')
See above. I don't see a typo.
The table class resides in a Plugin but no plugin notation was used in the association definition.
As described, I called it from the UsersController.
Can anyone help me finding, why the association doesn't work as expected? Maybe I am to much stuck in the cakephp 2.0 ORM, but I am almost sure that I did the association like described in the cookbook.
I've searched already and found some other issues with the belongstomany association. But they all handle problems with saving data. I want to search data, so I see no connection there.
Actually the filename is incorrect, filenames must match classnames (PSR-4), so it should be UserprivilegerolesTable.php. The warning notes may need some fixing or further explanation, as they are a little misleading by not using the Table suffix.
Once you've fixed that you'll stumble over the next problem, contained belongsToMany and hasMany associations cannot be used in conditions, as they are retrieved in a separate query, you'll have to use matching() instead.
Cookbook > Database Access & ORM > Query Builder > Filtering by Associated Data
How to filter by conditions for associated models?

how status of Product in Magento get changed/saved?

I am eager to know how status of Product in Magento get changed/saved?
Requirement-:
Suppose there are existing products which are enabled in Magento...Now If admin Disable particular product from Backend then I need to catch that particular product's Id through code in Magento file system?
So from where Can I get disabled product's id in Magento code? What is the file location & function name for the same? how Can I get that particular id?
Please guide me...
I think the down voting here is a bit unfair. The op is only asking one question - how to get the product id and status of a product after it has been saved.
#Sam - in Magento, instead of finding the exact point in code where a product is being saved, you would typically hook into an event by creating a custom module and use the Magento event/observer facility from within that module.
Have a look through this tutorial which will guide you through the process of creating a module with event/observers: http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method
Specifically related to your question: the event you are looking for is catalog_product_save_after.
The xml for your event would look similar to this:
<events>
<catalog_product_save_after>
<observers>
<yourmodule>
<class>Yourcompany_Yourmodule_Model_Observer</class>
<method>catalog_product_save_after</method>
</yourmodule>
</observers>
</catalog_product_save_after>
</events>
Your observer is going to look similar to this:
class Yourcompany_Yourmodule_Model_Observer
{
public function catalog_product_save_after($observer)
{
$product = $obvserver->getEvent()->getProduct();
$productStatus = $product->getStatus();
$productId = $product->getId();
}
}
Note - code is untested

Favoriting system on Appengine

I have the following model structure
class User(db.Model) :
nickname = db.StringProperty(required=True)
fullname = db.StringProperty(required=True)
class Article(db.Model) :
title = db.StringProperty(required=True)
body = db.StringProperty(required=True)
author = db.ReferenceProperty(User, required=True)
class Favorite(db.Model) :
who = db.ReferenceProperty(User, required=True)
what = db.ReferenceProperty(Article, required=True)
I'd like to display 10 last articles according to this pattern: article.title, article.body, article.author(nickname), info if this article has been already favorited by the signed in user.
I have added a function which I use to get the authors of these articles using only one query (it is described here)
But I don't know what to do with the favorites (I'd like to know which of the displayed articles have been favorited by me using less than 10 queries (I want to display 10 articles)). Is it possible?
You can actually do this with an amortized cost of 0 queries if you denormalize your data more! Add a favorites property to Authors which stores a list of keys of articles which the user has favorited. Then you can determine if the article is the user's favorite by simply checking this list.
If you retrieve this list of favorites when the user first logs in and just store it in your user's session data (and update it when the user adds/removes a favorite), then you won't have to query the datastore to check to see if an item is a favorite.
Suggested update to the Authors model:
class Authors(db.Model): # I think this would be better named "User"
# same properties you already had ...
favorites = db.ListProperty(db.Key, required=True, default=[])
When the user logs in, just cache their list of favorites in your session data:
session['favs'] = user.favorites
Then when you show the latest articles, you can check if they are a favorite just by seeing if each article's key is in the favorites list you cached already (or you could dynamically query the favorites list but there is really no need to).
favs = session['favs']
articles = get_ten_latest_articles()
for article in articles:
if article.key() in favs:
# ...
I think there is one more solution.
Let's add 'auto increment' fields to the User and Article class.
Then, when we want to add an entry to the Favorite class, we will also add the key name in the format which we will be able to know having auto increment value of the signed in user and the article, like this 'UserId'+id_of_the_user+'ArticleId'+id_of_an_article.
Then, when it comes to display, we will easily predict key names of the favorites and would be able to use Favorite.get_by_key_name(key_names).
An alternative solution to dound's is to store the publication date of the favorited article on the Favorite entry. Then, simply sort by that when querying.

Resources