Update multitables using one form in symfony+Doctrine - mysql

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.

Related

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.

Laravel 5.2 how to update my User table

I am trying to update my rows. The figure provided is the Users table structure. When I register using Laravel Authentication. Only the yellow marked rows are used and others remain empty. I have a created another page called profile and want to add data to the remaining empty rows using a profile page. But I am stuck and can't figure out how to do it. Any suggestions?
You can create Profile controller for interaction with User table from profile page, like this:
use App\User;
class ProfileController extends Controller {
public function function_name(Request $request)
{
...
$user = Auth::User(); //get authorized user, or you can get user by ID
$user->city = 'City';
$user->country = 'Country ';
$user->save();
...
}
In your user model, dont forget to add thoes fields to fillable variable;
for exemple
protected $fillable = [
'name', 'email', 'password', 'company_name','street_name','whatever'
];
this way the ->create($data); method will work just fine
or you can just add those fields to registration form and update your registration method

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!

updating a line in cakephp

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.

Retrieve session data Codeigniter

I'm working on a messaging system and want the user's userid to be posted to the database along with the message. Right now, the message is posting to the database, but with a user ID of 0.
How can I get the user ID from the session data to post to the database along with the message? Sidenote: I'm using Tank Auth for authentication. (From the mysql side, user_id in the message table is a foreign key referencing id in the users table).
Controller
function index() {
if ($this->input->post('submit')) {
$id = $this->input->post('user_id');
$message = $this->input->post('message');
$this->load->model('message_model');
$this->message_model->addPost($id, $message);
}
}
Model
function addMessage($id, $message) {
$data = array(
'user_id' => $id,
'message' => $message
);
$this->db->insert('message', $data);
}
For tank_auth, get the user_id using the following, and then assign that to your sessions
$user_id = $this->tank_auth->get_user_id();
Taken directly from CI's documentation:
Retrieving Session Data
Any piece of information from the session array is available using the
following function:
$this->session->userdata('item');
Where item is the array index
corresponding to the item you wish to fetch. For example, to fetch the
session ID you will do this:
$session_id = $this->session->userdata('session_id');
Note: The
function returns FALSE (boolean) if the item you are trying to access
does not exist.
So, if you have a piece of session data named user_id, you would access it like this:
$user_id = $this->session->userdata('user_id');