updating a line in cakephp - mysql

I'm trying to create a simple request/accept function. A user adds a person - both users usernames are added to the table along with an autogenerated id. Now when the other user checks their requests they click approve and add an expiry date to the the record. Currently whats happening is that when a user clicks accept and sets an expiry date it creates a new record in the Relationship table.
the structure of my relationship table is
id
partyone
partytwo
active
expirydate
public function add(){
if($this->request->is('post')){
$this->Relationship->create();
if ($this->Relationship->save($this->request->data))
{
$id=$this->Relationship->id;
$this->Session->setFlash('The relationship has been saved');
}
else { $this->Session->setFlash('The relationship could not be saved. Please, try again.'); }
}
}
public function approve(){
if($this->request->is('post')){
$this->Relationship->id;
$this->Relationship->getLastInsertID();
$this->Relationship->save($this->request->data);
$this->Session->setFlash('The information has been saved');}
else{
$this->Session->setFlash('The information couldnt be saved');}
}
}
here is my approve view
<?php
echo $this->Form->create('Relationship', array('action'=>'approve'));
echo $this->Form->input('expirydate',array('label'=>'Expiry Date: ', 'class' => 'dateclass'));
echo $this->Form->end('Submit');
?>
here is my add view
<?php
echo $this->Form->create('Relationship', array('action'=>'add'));
echo $this->Form->input('partyone',array('label'=>'Enter your username: '));
echo $this->Form->input('partytwo',array('label'=>'Username of user: '));
echo "<br />";
echo $this->Form->end('Click here to add relationship');
?>
how can i code this so it updates and not creates a new line with expiry date, please help I am losing my mind over this.

This is not making anysense. Is this a typo :
$this->Relationship->id;
$this->Relationship->getLastInsertID();
It should obviously read :
$this->Relationship->id = $this->Relationship->getLastInsertID();

You have to add a hidden field to your "approve" view with the id of the relationship you are going to edit. If the submitted data contains an id, the save() method will update the specified record instead of creating a new one.
You can find an example in the cookbook: http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/part-two.html#editing-posts

You need to provide more information, but the basic idea is:
On your approve view you need to have a field that stores the id of the relationship to be approved.
You can store the ID as a param for the approve method, or as a hidden field.
function approve($id=null) {
/*Form action = Relationship/Approve/ID*/
$this->Relationship->id = $id;
/*or - hidden field/an input*/
$this->Relationship->id = $this->request->data['Relationship']['id'];
$this->Relationship->save($this->request->data);
}
providing id is set before you save, Cake will update, instead of adding.

Related

Trying to update a column in database but NOT NULL attributes are not letting me update the values. I am using Laravel

I am trying to update the column with some other data but database table is not letting me update the table because of the NOT NULL constraints in it. I have this option of setting all the fields to NULL but I dont think that will be a good practice. Please I need a solution to it if anyone can help. I get the following error
Illuminate \ Database \ QueryException (HY000)
SQLSTATE[HY000]: General error: 1364 Field 'first_name' doesn't have a default value (SQL: insert into users (subject_id, updated_at, created_at) values (?, 2019-07-30 13:46:42, 2019-07-30 13:46:42))
Previous exceptions
SQLSTATE[HY000]: General error: 1364 Field 'first_name' doesn't have a default value (HY000)`
I have tried setting all the values to NULL and it worked but I want to work with some fields setting as NOT NULL and update the ones which are NULL and also if we can fetch or set the fields automatically to what we have ?
This is my controller where I am trying to update the field if this is required or help you understand my problem
public function deleteSubject($id) {
echo $id;
// die();
if(Auth::check()) {
$findSubject = Auth::user()->where('subject_id', $id);
$users = new User();
$users->subject_id = null;
$users->save();
// echo($findSubject);
// die();
Session::flash("message", "You subject has been deleted. You can add a new Subject now.");
return redirect('/subjects');
} else {
Session::flash("message", "Please sign in to access this page");
return redirect('/signup');
}
}
The following should work for your code. As it was said in the previous comment, it's because you try to create a new instance of a user without inserting value.
It look like you are trying to delete the subject associate with the authenticated user, so I suppose that you don't really need to create a new user, instead I think you should dissociate the user and the subject. So, the following should work for your code.
The purpose of that variant is to take the authenticated user and put a null value for the subject_id.
public function deleteSubject($id) {
echo $id;
// die();
if(Auth::check()) {
$user = User::where('subject_id', $id)->first(); // This will get the user that have the subect_id, but it's assuming that only one user have this subject_id.
// You can also do this just uncomment the first line below and comment the other one above.
// $user = User::find(Auth::user->id);
$user->subject_id = null;
$user->save()
Session::flash("message", "You subject has been deleted. You can add a new Subject now.");
return redirect('/subjects');
} else {
Session::flash("message", "Please sign in to access this page");
return redirect('/signup');
}
}
I think that you should take a look about how MVC work.
https://selftaughtcoders.com/from-idea-to-launch/lesson-17/laravel-5-mvc-application-in-10-minutes/
You should also take a look at relationship in Laravel: https://laravel.com/docs/5.8/eloquent-relationships
MVC and Eloquent-Relationships will help you understand some function in laravel to achieve this kind of goal really quickly.
If you get a User model and a Subject model, you can simply do something like this:
$user = User->find(Auth::user()->id);
$user->subjects()->dissociate($id);
I'm not sure, but I think the Auth facade let you use the user model function, so maybe this could work to:
Auth::user()->subjects()->dissociate($id);
You should also take a look at middleware: https://laravel.com/docs/5.8/middleware
With middleware, you can put rules like the one you are using to send a message to the user saying that he/she need to be log in to access the page into the middleware and reusing it whenever you need.

CodeIgniter DataTable with hyperlink

How can I pass data from datatable using hyperlink. I want to pass it by id. here is my code.
my controller
my model
index
I want to pass the data here when i click User ID. Thank you.
Student Profile
You can create the hyperlink on foreach loop.
Eg. <?php echo base_url('/users/' . $id ); ?>
After you need to create the route record.
Eg. $routes['/users/(:num)'] = 'login/view/$1';
Lastly, create the method that will receive the ID clicked and do what you want.
public function view( $id ) {
echo $id;
}

Expanding this method to write to the database

Hi I followed a tutorial to implement a friend system. It all works find, but I need to post other columns to the row that just the id's. How would I expand that.
This is the method that is accessed when the add friend button is clicked
public function getAdd($id){
$user = User::where('id', $id)->first();
//After passing all checks. Add other account
Auth::user()->addFriend($user);
echo "Sent";
}
AddTenancy Method
public function addFriend(User $user){
$this->friendsOf()->attach($user->id);
}
I assume the relationship is many-to-many between users. And you need to add additional data to the pivot.
Here's how you'd do that:
public function addFriend(User $user){
$this->friendsOf()->attach($user->id, ['another_col' => 'some data']);
}
Replace 'another_col' and some data with your column and your data. You can also add more than 1 column into the array.

Get only personal record of the DB and not all of the records

When you log in on my site, you'll directly be directed to your profiles page. So that you can see your name, phone, email, etc. But at the moment when I log in on my site, I get directed to my profile page but I get all data for every user in my DB. So I'm getting the name of every user, the phone of every user, etc. I only want to get the data of the person who logged in. How can I achieve this?
I did some thinking about it, and came up with doing a where userID = ID of the user HERE
But I don't know where I'll be able to get the ID of this user. When logging in I'm starting a session, so would I need to store the logged in users ID in the session? Or isn't that secure?
I'm learning and working with CodeIgniter, so I'm doing it in a MVC pattern.
Controller where I login and set my session
if($query){
$data = array(
'username' => $this->input->post('loginEmail'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
Controller
if($logged_in){
//if the user is logged in
$this->load->model('profile_model');
if($query = $this->profile_model->userInfo()){
$data['records'] = $query;
$data['content'] = 'profile_view';
$this->load->view('templates/template', $data);
}
}
Model
class Profile_model extends CI_Model{
function userInfo(){
$query = $this->db->get('tbl_users');
return $query->result();
}
}
View where my I want to show my data
if(isset($records)){
foreach($records as $row){
echo $row->username;
echo $row->cellphone;
}
}
All you are missing is a WHERE statement in your model. As an argument for the WHERE statement you can use your session variable containing your email address(assuming your database table stores emails in a field called 'username':
class Profile_model extends CI_Model{
function userInfo(){
$this->db->where('username', $this->session->userdata('username'));
$query = $this->db->get('tbl_users');
return $query->result();
}
}
Or you can pass the email/username to the model from the controller:
if($logged_in){
//if the user is logged in
$this->load->model('profile_model');
$username = $this->session->userdata('username');
if($query = $this->profile_model->userInfo($username)){
$data['records'] = $query;
$data['content'] = 'profile_view';
$this->load->view('templates/template', $data);
}
}
and then in your model:
class Profile_model extends CI_Model{
function userInfo($username){
$this->db->where('username', $username);
$query = $this->db->get('tbl_users');
return $query->result();
}
}
I don't know what your columns are named in your database, or I could be more precise.
If usernames in the database are different from email addresses, change the line in the model to:
$this->db->where('email', $username);
Hope this helps!

Update multitables using one form in symfony+Doctrine

Hi I am building a registration system in symfony. It has three models -User, jobSeeker and employer. Jobseeker and employer inherit user.
There are 4 steps in the jobseeker registration process, in the first step users have to enter their login detail & it will be added to the user table. In the next steps user has to enter his personal details, it will be added to the jobseeker table.
I want to update the user table and jobseeker table using one form, how can I do it??
(For example address and tp number are in the user table, but it will be updated in the second step)
Thankyou for ur replies
It is working
what I did was
In my dao class
public function updateStep($step,$address, $phone, $id)
{
Doctrine_Query::create()
->update('User u')
->set('u.step', '?', $step)
->set('u.address', '?', $address)
->set('u.telephone', '?', $phone)
->where('u.user_id = ?', $id)
->execute();
}
In the form class
public function updateStep()
{
$step = $this->getValue('step');
$phone = $this->getValue('phone');
$address = $this ->getValue('address');
$id = $this->getValue('user_id');
$updateStep = $this->getUserManagementService()->updateStep($step, $address, $phone, $id);
return $updateStep;
}
Finally in the registration action
$this->form->updateStep();
It is working but am I doing it in the right way or is there any easier way exist?
Simply ... you can create new object from User and new object from jobseeker in first step and put this object in session and in step two retrieve this object from session and update it with data
and in last step save those two objects.