i have admin list and member list which i have categorize by 'a' and 'm'. Now i am showing admin lists and member lists in different tabs. Now problem is if a user is in member list and is logged in than his name should not shown in member list.
Here is the controller:
public function view_customer(){
$data = json_decode(file_get_contents('php://input'));
$customers=$this->um->view_customers();
header("Content-Type: application/json; charset=UTF-8");
echo json_encode($customers);
}
Here is the model:
public function view_customers(){
$this->db->select('reg_id,reg_name,email,user_type,pic_url,id,name,cover_image,restaurant_id');
$this->db->from('registration');
$this->db->where('user_type','m');
$this->db->join('rest_restaurant_master','registration.restaurant_id=rest_restaurant_master.id','inner');
$query=$this->db->get()->result_array();
return $query;
}
Basically you need a variable that is both contained in your table, and is related to the user. I'm assuming you have one called id that relates to a session variable called user_id.
Add to view_customers:
$this->db->where_not_in('id', $this->session->user_id);
Or (easier):
$this->db->where('id !=', $this->session->user_id);
This way your array will contain users who are only in m and who are not the active user.
When you set session (in login)
$newdata = array(
'id' => 3, # id is database user recorded id
'username' => 'johndoe',
'email' => 'johndoe#some-site.com',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
and then in all the method you can call
$loggedUser = $this->session->id; # or $this->session->userdata('id');
$this->db->where_not_in('id', $loggedUser);
Read this Session Library class
Related
I have the registered Comment model which has a User reference, like this:
public function user() {
return $this->belongsTo('App\User');
}
This function returns an instance of a User, and that's correct, but I don't know how to register the User column the get que user property using Backpack. Instead, I'm get a JSON representation of my model:
So, how do I get a specific field from my relationship function?
Sounds like the select column is a perfect match for you. Just use it in your EntityCrudController's setup() method, like so:
$this->crud->addColumn([
// 1-n relationship
'label' => "User", // Table column heading
'type' => "select",
'name' => 'user_id', // the column that contains the ID of that connected entity;
'entity' => 'user', // the method that defines the relationship in your Model
'attribute' => "user", // foreign key attribute that is shown to user
'model' => "App\Models\User", // foreign key model
]);
The "attribute" tells CRUD what to show in the table cell (the name, the id, etc).
If you do:
$user = $comment->user->user;
You'll get 'test'; (from your example)
It may seem confusing because your User model has a user attribute. Maybe you can call it 'name' instead of 'user'. That way you'll call it:
$username = $comment->user->name;
Remember to check if the relationship exists before calling a property on the related model.
if(!is_null($comment->user)) {
$username = $comment->user->user;
}
Or:
$username = !is_null($comment->user) ? $comment->user->user : 'No user';
If you need to get some field from deeper relation, you can use closure column type:
$this->crud->addColumn([
'label' => trans('backend.column_names.agency_linked_service'),
'name' => 'agency_service_id',
'type' => 'closure',
'function' => function (AgencyLinkedServices $entry) {
return $entry->agencyService->agencyCategory->title;
}
]);
I can't for the life of me find info on how to do this despite my gut telling me its a basic fundamental.
I have a simple boolean that redirects users based on a table value reference_id. I want it to redirect the user after inputting a value in a column in that user's row.
Here is the code: (you can ignore the code I commeneted out. That's for a different implementation)
https://gyazo.com/9f2774cd9069b4678d67b80391f9f276
protected function redirectTo()
{
$id = Auth::id();
$rfid = DB::table('users')->where('id', $id)->value('reference_id');
//$userid = User::find($id);
if ($rfid == ''){
//$clientRole = DB::table('roles')->where('id', '2')->value('slug');
//$userid->attachRole($clientRole); // sets role based on redirect
input 'client' to database 'users' column 'temproles' //dummy code for what I want to do
view('/clientdirect');
}
else {
//$employeeRole = DB::table('roles')->where('id', '3')->value('slug');
//$userid->attachRole($employeeRole); // sets role based on redirect
input 'employee' to database 'users' column 'temproles'
view('/empdirect');
}
}
Again my apologies if this is common knowledge, I couldn't find a reference source anywhere. Either a link to where I can read about this or directions would be great!
If you want to input 'client' on a table called 'users' in a column 'temproles' you should be able to accomplish that by this:
DB::table('users')->insert(
['temprole' => 'client']
);
If you have models created you could do the following:
$user = new User
$user->temproles = 'client';
$user->save();
I have a table called users. It has the following columns:
id
name
username
email
User can not edit any data from this table except the username column. The question is that when user wants to edit his username, what would the query be in ZF2 Tablegateway?
In my controller I have a function called getUserTable(), as created in ZF2 manual also. So I am doing something like:
$this->getUserTable()->updateUser($id, $name);
And in UserTable.php class that is in Model folder I have a function called updateUser($id, $name).
My function look like this at the moment:
public function updateUser($id, $name){
$user_id = $id;
$username = $name;
$this->tableGateway->update($username, array('id' => $user_id));
}
So Basically all I want to implement is:
Update users set 'username' = $username where 'id' = $user_id
->update (array ('username' => $username), array ('id = ?' => $id));
Sorry for formatting, I am writing on phone.
I have my Authentication setup in CakePHP to redirect all users to a dashboard using this function:
function dashboard() {
$group_name = $this->User->Group->field('name', array('id' => $this->Auth->User('group_id')));
$action = $group_name . '/dashboard/';
$this->redirect = array('controller' => 'users', 'action' => $action);
}
My question is what would be the best practice (or resource I can look at) to manage group-specific, and user-specific content within this dashboard
A model method that will fetch all the data you need in a single array with keys named like the view vars you want to use and set it from the controller to the view if you need to fetch different things that are not necessary associated.
If not simply return the data and set it to a view var like this:
public function dashboard() {
$this->set('artist', $this->Artist->dashboard($this->Auth->user('id')));
}
By passing the user id you can fetch whatever you need in the model through the associations.
I have a JavaScript based page where a grid displays all the data for the registered users which is taken from the database.In the UI I have also column named 'groups' which display the groups that every users is participating, but this info is taken using additional two additional tables - 'groups' with 'id' and 'group_name' as columns, and a table 'users_groups' which has 'user_id' and 'group_id' as columns which are foreign keys associated with 'users' and 'groups' tables.
The problem is that when you click to edit any user from the UI grid there an AJAX that post JSON to containing the information for the user which include the
'groups' column which actually is not part of the 'users' table. So I have this:
$data = json_decode($this->input->post('data'), true);
Which originally (when the groups weren't added yet) was transformed in this:
$data = array(
'id' => $data['id'],
'email' => $data['email'],
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'usertype' => $data['usertype']
);
And then parsed to the model named 'mUsers' where is the update function:
$query = $this->mUsers->update($data);
And the model since now was very simple because there was only one table:
public function update($data)
{
$this->db->where('id', $data['id']);
$query = $this->db->update('users', $data);
return true;
}
But now I have to update another table - 'users_groups' and I'm thinking about my options.Is it possible to just add to the $data array show above another record:
'groups' => $data['groups']
And try to implement all the logic in one function(If this even possible, I couldn't find info if I can have two different active record queries in one function) or the second thing I thought of was after decoding the JSON to make something like:
$dataGroups = array(
'groups' => $data['groups'] //adding groups data to be fetched for update
);
and then parsing the data to a new function, something like:
$q = $this->mUsers->updateGroups($dataGroups);
What would be the better approach in this case?
Thanks
Leron
Make both function. And call them when necessary also you will be able to to call only usergroup function alone
//this function update all user data including group
public function updateUser($data, $changeGroup = true)
{
$this->db->where('id', $data['id']);
$query = $this->db->update('users', $data);
if($changeGroup) {
$this->updateUserGroup($data['id'], $data['groups']);
}
}
//Update user group
public function updateUserGroup($userid, $usergroups)
{
//Update user groups
}