Laravel 5.2 update last created record - database

I am new to laravel, I have tried a bunch of recommendations but none have worked for what I need. Do to requirements for design I am splitting my registration into two tables artist and the normal user table. The problem is email verification is done through the artist table. After that process has been completed I would simply like to update the last created user in the user table from 0 to 1 in the active column.
I can not figure out how to get this to work.
return $this->users()->where('verified')->first()->verified()- >orderBy('created_at', 'desc')->first();
Then ran update didn't work
User::orderby('created_at', 'desc')->first();
Then tried update didn't work
$user = User::find(1)->verified()- >update(array(
'Verified' => '1'
));
Non of the above have worked. Any suggestions are appreciated.

Given your requirements, and the way you are doing things - you are creating a user during registration (in the users table and setting verified to 0) and then putting some of their information in an artists table until they are verified.
I would add a users_id column to your artists table and set the value of this to the id of the newly created user.
When they verify, you can find their information in your artists table using their user_id and then move it anywhere you need to.

Related

How don't select the same row twice in two select cassandra queries?

I am working on a social networking project with cassandra. Users can subscribe to a profile and have access to the list of people who have subscribed to that same profile. My goal is to retrieve in a table called user_follows the list of people subscribed to a profile.
CREATE TABLE users_follows (to_id text, from_id text, followed_at timestamp, PRIMARY KEY(to_id, from_id))
The problem is that some profiles can have thousands of subscribers and I don't want to get them all at once. That's why I'd like to get the list in increments of 20 depending on how far down the user goes. My problem is that I can't see how to retrieve the other parts of the list after the first select because Cassandra always returns the same users.
SELECT * FROM users_follows where to_id = 'xxxxx'
A possible solution was to sort with a timestamp but in case I want to retrieve the list of people to whom a user is subscribed (the reverse query) this would not work. One solution would be to use materialized views but I'm not sure that it would be very optimal given the size of the table. Or to create a different table, one user_follows and another user_followers, but I don't think this is very recommended....

Magento 2 category in database, but not showing in admin or front end

I have been adding categories for different types of aircraft, under a subcategory called "aircraft," which is a subcategory of the default root category. At one point, I created a category called "bombers" and saved it. It showed up in the treeview. Then, I created a category called "cargo" and it showed up under treeview, but "bombers" disappeared. When I tried to re-enter "bombers," I got the dreaded "URL key for specified store already exists." The category "bombers" does not show up in the admin treeview, and it does not show up on the front end either.
When I looked in the database, I saw there are entries for "bombers" in the catalog_category_entity_varchar table (see below). Do I/can I just delete these entries and try adding it again via the admin screen, or is it not that simple? I can just delete the category and start over, as I have no products associated with it yet. What should I do? Thank you.
"bombers" show up in this table
* UPDATE *
OK. I think I fixed this issue. First, I moved a copy locally (Windows/XAMPP), in case I screwed something up! I looked and saw two categories were both given entity_id of 60. I think that's what screwed everything up. I deleted any entries in any category tables that were 60. I then had to go into the marketing section of the admin area and delete the two url rewrites it created for 60. I could then add "Bombers" and "Fighters" and any other category I wanted to. Now, I just need to decide if I want to try and export the sql, or just make the changes manually on the server.
Let me know if what I did seems ok, and/of if there's more I need to do. Thanks.

Saving a user in CakePHP with ACL group name specified rather than group id

I have set up a Cake 2.3.1 app with ACL following this tutorial so I have a users table and a groups table.
Everything is working fine, but I'm a little confused about how I should go about saving new users since I don't want the user to be able to select their own group. The group will be chosen based on a number of things that the user inputs on the form so it will be determined by conditional statements in the controller.
At the moment I'm doing this:
$this->request->data['User']['group_id'] = '2'; (or 1, 3 or 4 depending on conditional statements in the controller).
Then, I'm doing a save on $this->data. This works but I'd like to be able to specify the group name rather than hardcode the id so that when we move to production and clear the database, it won't matter if the groups are added in a different order.
I see two ways of doing this.
The first I haven't tried, but it would be to change the primary key of the group table to the name of the group and set the primaryKey variable in the Group model and all subsequent reference and associations with this table in all the other models. That would probably bring you more than a headache and I wouldn't recommend it because you'd probably break the ACL component one way or another.
The other way is what I'd do, and it requires a bit of programming on your part (Cake doesn't have an easy way of doing this that I know of).
In the User model, put a beforeSave and in that function, create a dummy variable with the group name, do the lookup of the name and insert the correct id, and delete that variable.
On User.ctp model
public function beforeSave() {
if (isset($this->data[$this->alias]['group_name'])) {
//do the lookup with the group_name
$group = $this->Group->find('first', array('conditions'=>array('name'=>$this->data[$this->alias]['group_name'])));
if (count($group) <= 0)
//... throw an Exception or handle any way you want this case
$real_group_id = $group['Group']['id'];
$this->data[$this->alias]['group_id'] = $real_group_id;
unset($this->data[$this->alias]['group_name']);
}
return true; //remember this is important! otherwise the save will stop
}
When you want to change the user's group or add another user, just do this in the controller
$data_to_save['User']['group_name'] = 'webuser';
and the beforeSave function will take care of the rest.
I realize you asked for a Cake-easy-way of doing this, but I guess there's none (not sure though, feel free to correct me), the beforeSave function could save you a lot of repetitive code on the controllers. The only thing you'd have to be careful when clearing the database or changing group ids is to reassociate them correctly with the users already in the database, but since is a "moving to production" change, those users already predefined should be few or none.

Granting access of specific user to specific (multiple) docs

I'm building a small project with database. I have a user table which has two columns, user_id and name, The second table stores the id and name of some documents: it also has two columns doc_id and doc_name. I want to grant access of specific user to specific (multiple) docs.
For example:
user1 can access doc_2 and doc_3 Only.
user2 can access doc_1 and doc_2 Only and so on.
Users and forms keep changing (eg. after some time i need to add a new doc, and add access to existing or new user to that new doc).
Do i need to change database design? (for example add a column in docs to store name of each user who can access it? ) If this is so, can you tell me what changes i should do?
OR
Is it possible to do by creating views? In this case, do i still need to change the database design? If this is the case, can you tell me an example view please? In this case, will i need to create view for each user? For example if there are 100 users, i will need to create 100 views?
You need a third table (I'll call it user_doc). You need 2 main columns; user_id and doc_id.
You then insert one row for each document and user combo that has access permissions.
If their user_id doesn't appear in the user_doc table with the relvelant doc_id, they don't have permission.
A sample query to get a list of all docs a specific user has access to:
SELECT doc_id FROM user_doc WHERE user_id = #UserId
or to find all users with access to a specific doc:
SELECT user_id FROM user_doc WHERE doc_id = #DocId
You need to have a PERMISSIONS table with relationship between Users & Documents. The columns could be PERMISSIONS_ID,USER_ID (Refer User), DOC_ID (Refer Document). Every time access has to be given to a user for a document this table needs to be populated.

Entity Deletion Strategy

Say you have a ServiceCall database table that records down all the service calls made to you. Each of this record contains a many to one relationship to Customer record, where it stores which customer made the Service Call.
Ok, suppose the Customer has stop doing business with you and you do not need the Customer's record in your database. No longer need the Customer's name to appear in the dropdown list when you create a new ServiceCall record.
What do you do?
Do you allow the user to delete the Customer's record from the database?
Do you set a special column IsDeleted to true for that Customer's record, then make sure all dropdown list will not load all records that has IsDeleted set to true? Although this keeps the old records from breaking at innerjoins, it also prevents user from adding a new record with the same name as the old Customer, won't it?
Do you disallow deletion at all? Just allow to 'disable' it?
Any other strategies you used? I am guessing everyone have their way, I just need to see your opinions.
Of course the above is quite simplified, usually a ServiceCall record will link to many other entity tables. All of which will face the same problem when they are required to be deleted.
I prefer to set an IsDeleted flag, one of the benefits is you can still report on historical information (all teh data is still there).
As to the issue of not being able to insert another customer with the same name, this isn't a problem if you use an ID column (eg CustomerId) which is generally auto populated.
I agree with #Tetraneutron's answer.
Additionally, you can create a VIEW that lists only the active customers, to make it more convenient to populate drop-down lists and such.

Resources