Porting Yii1's $user->setState('key', 'value') to Yii2 - yii

I'm upgrading a Yii1 app to Yii2 and got stuck at porting
Yii::app()->user->setState('key', 'value');
Yii::app()->user->getState('key');
to
Yii::$app->user->XXXX('key', 'value');
Yii::$app->user->XXXX('key');
I know the setState method sets a value in the session (And db if the session is also in the db), but I can't figure out which of the new \yii\web\User method I should use as none of the login, setIdentity, switchIdenty methods seem to work the same way.
More info if needed: The "setState()" code is used to save the user id entered in a text field, which will then be used on the next page to ask the password.

This feature was dropped in Yii2. You should use Yii::$app->session in this cases, because setState/getState was just simple wrappers on it.
About prefix for user session
Simply use
Yii::$app->session->set('user.some_param',$someparam);
and use below to retrieve
Yii::$app->session->get('user.some_param' , $defaultValue);
Or you can implement this feature from Yii1 in your own class. For flashes see CODE

Related

Vue: Showing that the function does not exist when I have defined it the relevant store

I am currently working on a simple app to store workout routines in Nuxt 3 and Appwrite. The link to the source code is here.
After logging in and adding in some workouts in the app's UI, whenever I try to call the deleteWorkout function, I get an error in the console saying that the function is not defined, whereas I have clearly defined in the workoutStore. I can't seem to figure out the reason for the same.
The same can be seen in the given screenshot.
Console on clicking the delete button
PS:
Most probably the error should be originating from either /pages/workouts.vue, /components/WorkoutDetails.vue or /stores/workout.js.
I am using Appwrite to manage the back-end of the web app, and the instructions to setup the same can be found in the README.md. (Though I don't think the error I am facing is related to the same.)
In your code the problem is, you declear your deleteWorkout() function outside of the actions block in workout.js file.
Make sure all your functions in the workout store are inside the actions block. Then it will be accessable from the vue component

How can I refresh my composable when new data is being inserted?

My team and I have to make a license plate scanning app as a school project. With that, we have comments and pictures which can be added to cargo. Whenever the user scans a plate they also get the chance to change the checked info in case of a mistake. The problem is that whenever we delete data from the scanned plate it doesn't show on the screen that it has been deleted until we go to another screen. The same goes for the lazy column which we use for inserting new instances of comments and pictures. The data doesn't show on screen until we turn our screen or go back to another screen. private val pictureList = mutableListOf() is what we use for the pictures and for the text we use var countryCode by remember { mutableStateOf(CountryCodeText) }
var licenseNumber by remember { mutableStateOf(LicenseNumberText) }. pictureList is a global variable and the other ones are local variables which use global variables in the mutableStateOf. How can we make sure that the UI updates whenever the data changes? In advance I want to say thanks for the help! (Code is written in Kotlin and jetpack compose)
Just replace the mutableListOf() with a mutableStateListOf(...), and prefer to keep all the state logic confined to a ViewModel. The viewmodel should preferably be only one for the entire app, and the entire app should have only one activity.
The viewmodel should act as a single source of truth for the entire activity's UI state, while also handling all the updates to the UI efficiently.
The #Composables should only be incharge of displaying the state, and sending events to the viewmodel to update the state, for example, an onClick event may be sent up to the viewmodel by a button too trigger a state change in another part of the app.
Take this codelab to learn all about state in Compose (Well, not all, really, but good starter).
Also, changing screens destroys all the #Composables of the current screen, and so when you ce back there, all the #Composables are re-created, and the correct data is fetched. If you wish to trigger "recompositions" upon changing a variable, you must ensure that the concerned variable is a state-holder, i.e., initialized with one of the pre-built state initializers, like mutableStateOf(), or mutableStateMapOf, etc.
We usually have a mutableState*Of format for determining whether a pre-built state initializer is available. The most common ones are covered, obviously, but if not, you'll need to create a new type of initializer yourself, and if that is not something you know how to do, currently, you can just go about checking whether the type of data you wish to store is Immutable. If so, you can just wrap it in a mutableStateOf<DataType>() call, and it will trigger the recompositions. Know that this is not as efficient as pre-built initializers, but definitely gets the job done.
Also, I suggest you take the compose-pathway to get the basics down. It covers everything ranging from creating a simple UI using basic pre-built Layouts to creating a complex animation-driven application using custom Layouts.
So, definitely a lot to take it, but I hope you get there.
Happy composing,

Ionic 2 app: remember user on the device

I have an Ionic App and I'd like to put a "Remember me" checkbox in the login page, so that once the user has logged in he'll be logged in forever (unless he logouts), even if he close and re-opens the application, just as the Facebook app does.
Is there a way to do that in Ionic 2?
Thanks!
You can use localstorage to save some data for your own verification/logic handling.It's html5 supported and easy to use. The downside would be it won't be secure and it might be erased by system when there's low memory or clearing of cache.
HTML(Upon button press, but u can set it to your own preference):
<button secondary (click)="addLocalStorage()">Done</button>
In your controller:
import {Storage, LocalStorage} from 'ionic-angular';
constructor(navController, local) {
this.navController = navController;
this.local = new Storage(LocalStorage);
}
addLocalStorage(){
this.local.set("didTutorial","true");
//set the doneTutorial to be true
}
Explanation:
At the constructor , we create an new localstorage object called 'local'.
To call it, we use 'this.local' and 'set' is the method to store it.
Set(Key,Value)
In the example code above, i use 'didTutorial' as the key and 'true' as the value.
To retrieve it, you can retrieve it at this page or at any other page.
Just remember to import Storage and LocalStorage and declare a new localstorage object in the constructor.(same as above)
The code to retrieve is :
var value = localStorage.getItem('didTutorial');
getitem(Key,Value)
You can use storage and sqlStorage to save user data. And then check it for saved user.
Refer :-
https://www.thepolyglotdeveloper.com/2015/12/use-sqlite-in-ionic-2-instead-of-local-storage/
Hope this helps!
localStorage works fine but I confirm the drawbacks that #Gene talks about. It is not reliable, especially on iPhones that do not have much free storage
I use ionic 1 so I cannot try this solution, but you might want to check this out : https://ionicframework.com/docs/native/native-storage/
; could be more reliable!

How to use models from models/ in layouts/main.php in yii 2.0 (basic template)

I have started to learn about Yii framework about day ago and I came up with the problem.
I have downloaded basic application template (so that you know the structure of my application).
I would like to use one of my classes's function from "models/" in the "views/layouts/main.php", however, I am not sure how I can access them from "main.php". I have searched around the internet and none of the solutions have helped me.
I've read about widget creation, so I can use it in "main.php" in this link - Yii - how to retrieve model data into a layout page?, but this does not provide my version of Yii as I coulnt not find protected folder and etc. So I was kind of confused.
Also read some other solution, but they did not help me as well.
What would you suggest me to do? Because I am currently have no clue about the solution. I am a newbiew in this framework, so, don't judge me hard :)
UPDATED: [SOLUTION]
// use statement on top of main.php like the other use statements
use app\models\Modelname;
// anywhere in the file
....
$myModel = new Modelname;
$myModel->myFunction();
...
// Or if it is a static function:
Modelname::myFunction();
This is how to access model files (classes) from folder models/ from for example views/layouts/main.php.
Here is the deal - view is actually only a "view". View should only represent some data, but not do something with this data. You don't want to have any logic in the view.
Anyway, if you want to show something from your model - you should pass this model through controller, which renders your view.
Controller will look like:
$model = new Device;
$this->render('index', array(
'model' => $model
));
Then you can use it in the view, normally to get some data from the model.
You could also get it directly in the view, but this is not good practice.
Here can you read some basics about models and best practices how to use them.
Update [Yii 1.x]
To use Model-Class specifically in your main.php, import it at the place you want to use it:
Yii::import('application.models.LoginForm');
After that you can normally use functions from your Model Class.
Update 2
Namespaces in Yii2:
use yii\models\LoginForm;
at the start of your main.php layout-File. Look also at the "Layouts" part here.

Custom Grid App

So I am trying to make modifications to the custom grid app that rally has already created. I found the source code at https://github.com/RallyApps/app-catalog/tree/master/src/apps/grid. However, I cannot get that code to work. I have added it to a js and then ran build (I also changed the json). However, when I then add the app to rally it doesn't give me the settings options (object, query, pagesize, etc) and just generates a table. This table generates 4 rows (the number of user stories I have), but the rows are completely blank except for gears at the beginning of each row. I was wondering if I was building this app incorrectly or if I had grabbed the wrong code. If not, is there a place where can I get the complete code or a way to modify the already existing code?
Thanks
You may add a fetch to the config as below:
config: {
defaultSettings: {
types: 'hierarchicalrequirement',
fetch: 'FormattedID,Name'
}
}
and it will display a grid.
It will not display settings dialog however. The source code in this github repo is not exactly the CustomGrid app available from the AppCatalog in the UI. It does not have the full functionality and not ready yet. I submitted a defect.