cakephp : save All condition - cakephp

I have userdetails table and user table where user has many userdetails ,& userdetails belongs to user.
on userdetails/add i want the cost column under user table to be saved on userdetails/add->submit.
in my userdetails/add form i have $this->Userdetail->input(User.cost);
in my controller userdetails -> add i am using saveall .
now when i add a new user detail ,a user detail record is created and in user table a new record is created with cost column filled what i entered in userdetail add form,
what i want is cost in user column should not be added as a new record,it should be saved or updated with the user i have logged in .
i want to know how to pass condition in saveall so that when i add a user detail the cost column in user is saved but in front of the logged in user not a newly created record in user.

What you need to do is set the User ID to the ID of the logged in user, before saving. If the ID isn't set it creates a new record, so to edit an existing record it has to be set.
Try this before saving:
$this->User->id = $this->Auth->user('id');

Related

Create formula field that will show Email of the User in your layout when you will create a new record

I want to Create formula field that will show Email of the User in your layout when you will create a new record
Can anyone help to how should I do
So do you want the Email Address of the current user or the user that created the record? Either way it isn't a difficult formula. Just create a formula field of type text and then insert the field you want.
For the Current User:
$User.Email
For the User that Created the record:
CreatedBy.Email

Yii2 - login from more database tables

I need a help. I have three tables (admin, teacher, student) and I want to log in them from one login form. And I need also distinguish them as roles that for example student can't go to some page as teacher.
I created three radio buttons for every table in login form.
Like * admin * teacher * student.
With using this:
<?= $form->field($model, 'role')->inline()->radioList(array(1 => 'admin',2 => 'teacher',3 =>'student'))->label('You are: '); ?>
How can I make to find and log in the user when the user put there username and pass word and click on one from the radio buttons who he is? I generated three models for each table with using gii. So what and where I must write to make it work? Thank you for help!
If you have table/model per each role and you want to login from the same form, then on your login form except of usual username and password fields, you'll also need a role field (some radio button where user will have to specify what role they are). Otherwise, you won't know which table to query for username/password. Of course you can query all of them step by step, but what if there are teacher or student with the same username? Username has to be something unique, if you have one login form and it is not possible to achieve in case you have 3 tables for every type of user.
Another option would be to use one User table/model for all roles by simply adding extra column role. I think there's role column inside user table out of the box if you use Yii2 Advanced Template. Probably this is where you can start from.
As for permission to access specific pages per role. What you need is ACL (Access Control filter in Yii2).
I found few existing articles on how to achieve what you need.
http://yii2-user.dmeroff.ru/docs/custom-access-control - this is
simple example
http://code.tutsplus.com/tutorials/how-to-program-with-yii2-user-access-controls--cms-23173 - this is a bit more advanced
Hope that helps.

How to synchronize tables in Django?

In my database I want to synchronize two tables. I use auth_user(Default table provided by Django) table for registration and there was another table user-profile that contain entities username, email, age etc. How to autometically upadate the columns username and email in user-profile according to updation in auth_user table.
from django.contrib.auth.models import User
class profile(models.Model):
username = models.CharField(max_length = 30)
email = models.EmailField()
age = models.PositiveIntegerField()
auth_user_id = models.ForeignKey(User)
You can either modify the django registration code and include the code to save into your profile model on every new registration.
Or
You can set a signal on every save of User model. See the documentation.
def create_profile(sender, **kwargs):
if kwargs["created"]:
p = Profile(user=kwargs["instance"], ...)
p.save()
django.db.models.signals.post_save.connect(create_profile, sender=User)
create_profile() will be called every time any User object is saved.
In this example, I create Profile object only if a new User instance has been created. You can modify it to change existing Profile objects on every change in User instance, also.

CakePHP Associated Models available after save?

EDIT, I am rewriting the question for more clarity.
I have a "profile" model that has a belongs to relationship to a "user" model.
A certain user exists already. Later he creates a profile. The profile controller takes care of creating a new entry, but then needs to update a profile_id field as part of the associated user. I was under the impression that saveAll could take care of all the associations but it is not working?
What is the easiest/CakePHP standard way to do something like this? Thanks!
saveAll() creates new records. So you can't use it to update an already existing record in the Users table. As Anh Pham already mentioned, you've got your associations wrong. A Profile belongs to a User, and a User has one Profile. By having a profile_id field in your Users table, you're doing it the other way around.
So remove the profile_id field from the Users table, add a user_id field to the Profiles table, and update your model associations in user.php & profile.php.
To save a new Profile for an existing User, you can then either query the user id for the current User, or for example retrieve it through Auth, and add it manually to $this->data prior to calling the save() method of your Profile method.
You shouldn't have a profile_id field in the users table, you should have user_id field in profile table (the foreign key is similar to hasMany relationship). I'm surprised that the app still works http://book.cakephp.org/view/1041/hasOne
Also, I usually don't have hasOne relationship. If User hasOne Profile, then just include all fields in the profiles table into users table, unless there's some reason not to do it. But again, it's just my preference.

sql edit save for log

I want to know how to save edited values in sql server to return to it's value after that.
For example:
I have the user which it's status is working, after an year it's status is "Closed" the status is saved as an id in table user when it is working the id of status is 1 when it is closed i update the id to 2. how can I see that this user from date to date was working and now he is closed.
Thanks for your replies.
You got my question.
But the problem here is that I give here only one example which was user status, but the problem is that I have so many tables which I change they id's. For example I have Id of city, or Id of products etc.
If I save it at the same table of user for example I lost user id because every time I update user it will take a new id, or if I do a user status table I will do 20 or more tables like user status history. Also I want to save the text not only id's. For example the user got e new address I want to say from this date to date he was at previous address and now-days hi live in another place.
Any suggestion please :).
Your question is very unclear, but I think you mean "I have a User table with a Status column, and I need to see a history of status changes because when I change the Status value for a specific user, I lose the previous value".
If that's correct, one simple solution is to add a second table called UserStatus or whatever, and INSERT a new row into it every time the user status changes. You can then easily see the complete history of status changes for each user, and you can also find the current status for any user by querying the 'newest' row for that user.
In principle, the Status column on the User table would be unnecessary if you add the UserStatus table, so you could remove it. If that isn't possible (e.g. you don't control the applications using the table), then you can add a trigger to the User table that populates the UserStatus table automatically when you UPDATE the Status.
My suggestion is to have two columns
One is working/date and closed/date so that you can maintain the history of data.

Resources