Add a user in Cakephp - cakephp

There is a problem when I write an add() function for UsersController.
public function add() {
if ($this->request->is('post')) {
$this->request->data['User']['book_id'] = $this->Book('id');
if ($this->User->saveAssociated($this->request->data)){
$this->request->data['User']['book_id'] = "ff";
$this->Session->setFlash(__('Save User'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('User Error.'));
}
}
If I add a User I want to save the book ID in my User book_id.
Anyone know how to deal with this problem? Thanks!

When you're adding a new user, does he get to select a book from a drop down(in the form)? If so, $this->request->data['User']['book_id'] will already contain the Book id.
There's no need to explicitly write $this->request->data['User']['book_id'] = $this->Book('id'); like you have done.
The way I see it, the add.ctp view file should contain a select box containing a list of all books which the user can select. On selecting one of those and submitting the form, $this->request->data['User']['book_id'] will automatically store the book_id, without having to write anything in the controller.
Please specify the add.ctp form fields so that I can assist you better.
Thanks!

Related

CakePhp save changes only in middle table

Here is the problem what I have
I have 3 tables m:n
Users -> addresses_users -> Addresses
During adding new user I am selecting from dropdown X Addresses and after I click save I have to save new user data (new user in Users table), make mapping in addresses_user table and Addresses table shouldnt be changed, cos I have already those addresses in DB.
So, if i use code like this
input in ctp with
name="addresses[]"
In controller
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['controller' => 'users', 'action' => 'view/' . $user->id]);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
Cakephph makes mapping automatically and want to save new user, mapping and information about addresses.
Is there way to make this automatically with cakephp that he saves new user and make mapping or I should use another input name (that cakephp doesnt make patch with addresses) and after save make foreach with all addresses_id and save values direct to addresses_users table?
Or there is better ideas?

Weird problems while getting ID just after saving data

I have different models which are an extension of another one.
classes (SubModel1, SubModel2, ..,6) extends -> HighModel extends -> Appmodel
Everytime I save data in each submodel I call a function in my HighModel to save the id of the just saved SubModel. It works perfectly with five SubModels, but in one the id is always: ($shouldBeThisId - 1)in both add and edit functions.... o_O
Here's my add function:
public function add() {
if ($this->request->is('post')) {
$this->M18Tab->create();
if ($this->M18Tab->save($this->request->data)) {
$data = $this->M18Tab->findById($this->M18Tab->id); //<---problem's here
if($this->M18Tab->M18Model->saveVersion($data)){
$this->Session->setFlash(__('The m18 tab has been saved'));
$this->redirect(array('action' => 'view', $this->M18Tab->id));
} else {
$this->Session->setFlash(__('Error with versioning system'));
}
} else {
$this->Session->setFlash(__('The m18 tab could not be saved. Please, try again.'));
}
}
//view variables
}
ok. so after saving I recall the "just saved data" and pass it to $this->SubModel->HighModel->saveVersion($data). and the weird thing is that if I try to force the id to find:
$data = $this->M18Tab->findById(33);
it'll still find the id preceding the just inserted one :0.
what could this be caused by? the code is the same for all 6 models and in only one I have this issue..
This is more of a comment/suggestion than an answer, but anyway...
If you're adding a new record, after calling save, you can get it's ID via:
$this->MyModel->getInsertId();
See that method in the cookbook. I know the id should also be available at $this->MyModel->id, but since it's not working in this case, you might want to try:
$data = $this->M18Tab->findById($this->M18Tab->getInsertId());

how to add hidden value from registeration from in db field in cakephp

I have one registration form. I want to pass hidden value in that form. I have created one field in database 'lang'. In registration form I did like this: echo $form->hidden('lang', array('value' => '1')); But its not saving the value in db. Sorry I have no experience in cakephp, so please if anybody help me in this with all processes. Thanks
You should not just pass hidden stuff through the form just for the heck of it.
If you can, add those values prior to actually saving.
See http://www.dereuromark.de/2010/06/23/working-with-forms/ for details
Basically, you do:
if ($this->request->is('post') || $this->request->is('put')) {
$this->Post->create();
// add the content before passing it on to the model
$this->request->data['Post']['lang'] = '1';
if ($this->Post->save($this->request->data)) {
...
}
}

How to add index to database with belongsTo association

I'm looking for the best practice for adding an id to a database table. As an example I have a Member (model) that hasMany Recipe. The Recipe model states that it belongsTo Member:
public $belongsTo = array(
'Member' => array(
'className' => 'Member',
'foreignKey' => 'member_id',
)
);
I hoped that this would be sufficient when saving new recipes to the database, but I see that cake cannot figure out the relationship on its own because I am getting an error ("General error: 1364 Field 'member_id' doesn't have a default value") when using add:
public function add() {
if ($this->request->is('post')) {
$this->Recipe->create();
if ($this->Recipe->save($this->request->data)) {
$this->Session->setFlash(__('The recipe has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The recipe could not be saved. Please, try again.'));
}
}
}
Now, in this case I can easily get around the problem by adding
$this->request->data['Recipe']['member_id'] = $this->Auth->user('id');
but in general the ids that I will want to add will not be stored with the Auth data. So I would like to know how to access the id when saving something that belongsTo something else.
edit: I think my question was probably not worded well enough. I will restate what I'm looking for:
Say User hasMany A, which hasMany B, which hasMany C. C belongsTo B, A, and User (table for C contains user_id, a_id, and b_id). A user logs in, and picks a certain A, and then a certain B. The user now wants to add a new C. What is the best way to get all the id values for the new C entry? Do I just set a bunch of session variables? Should they all be passed in the URL? Is there some better way?
You need to provide any information that you want to save in the database. This means you either use the Auth information like you already stated or you get the information via a form where you can select from all the members available.
Either way the information has to come from somewhere. It can not create itself out of nothing.
If the user is selecting or picking A, B or C, them, somehow you can store their data and deal with them.
Usually, the common way is using forms. If you are using Javascript, you can set hidden inputs or if you are using AJAX, just using them inside it to call the final Cake functions.
You have to see how the user picks them, and then, store that ID in some variable to play with it. POST, GET, SESSIONS, COOKIES...

CakePHP - Passing Data Between Models

This is my last hope...I have been trying to figure this out all day and I am out of gas. I have an appointment application that an operator inputs phone, business, name, and call result (Customers Controller). The call result then goes to the Appointments controller. Lets say the operator setups up an appointment, I then now need to setup an appointment for the customer I just entered. But when I try and setup a new appointment for the new customer I just get sent back to the Appointments index view instead of the form. What am I doing wrong? I need some other eyes on this. My development team has all been laid off and I am the last one left, so I don't have anyone to bounce ideas and code off. Please help. I don't even know if I am asking the right question at this point.
function step1() {
$this->set('title_for_layout','Make a New Call');
if (!empty($this->data)) {
$this->Customer->create();
$this->data['Customer']['user_id'] = $this->Auth->user('id');
if ($this->Customer->save($this->data)) {
$this->Session->setFlash(__('The customer has been saved', true));
//??? WHAT DO I PUT HERE TO GO TO THE APPOINTMENTS CONTROLLER WITH THIS CUSTOMER DATA
} else {
$this->Session->setFlash(__('The customer could not be saved. Please, try again.', true));
}
}
}
Try this after saving the data
if ($this->Customer->save($this->data)) {
$this->Session->setFlash(__('The customer has been saved', true));
$id = $this->ModelName->getInsertID();
$this->redirect("customers/index/$id");
Now you've the id and fetch the data over there.
Or if you want to pass the entire array to that function then try this link simple cakephp problem
Where did you get $customer ? it isn't initialize
so you can save data in session
but it isn't good idea
$this->Session->write('data',$this->data);
also uoy can transfer customer id to another controller like this
$this->redirect("/appointments /step2/{$id}/");
// this is in appointments controller
function step2($id = null){
if($id){
$this->loadModel('Customer');
$customer = $this->Customer->findById($id);
}
}
something like this, hope it helps

Resources