I have a Courses model that gives me this array:
[User] => Array
(
[0] => Array
(
[id] => 4
[username] => itaita
[password] => sdfasdgadgadfgsfdgsdgsdgsdgdsg
[email] => itaita#xyz.com
[name] =>
[role] => admin
[hash_change_password] =>
[created] => 2014-03-16 21:06:48
[modified] => 2014-03-16 21:06:48
[CoursesUser] => Array
(
[id] => 1
[user_id] => 4
[course_id] => 1
)
)
)
I want to create a subscribe/unsubscribe button that will delete the CoursesUser where id=this user id and course_id = this course but I don't know how to access it from the Courses controller and view.
Currently I have:
public function unsubscribe($id = null) {
$this->Course->id = $id;
$userid = CakeSession::read("Auth.User.id");
if (!$this->Course->exists()) {
throw new NotFoundException(__('Invalid course'));
}
$this->request->onlyAllow('post', 'delete');
if ($this->Course->User->delete(array('CoursesUser.user_id'=> $userid), false)) {
$this->Session->setFlash(__('The course has been deleted.'));
} else {
$this->Session->setFlash(__('The course could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}
But this deletes the user.
I know the problem is here: $this->Course->User->delete but I don't know how to change this to address the CoursesUser array.
Help!
You could use this:
$this->Course->User->CoursesUser->deleteAll(array('CoursesUser.user_id' => $user_id, 'CoursesUser.course_id' => $id), false);
Basically, assuming that you have your model relations set up correctly, this will delete only the CoursesUser rows that match the user_id that you have retrieved from auth that also have the course_id supplied to the action.
Related
I've been digging through similar questions but couldn't get my app to save HABMT relationsships.
I want to have "Test" HABMT "Browser".
The Tables:
cake_browsers (id, name, ...)
cake_tests (id, name, ...)
cake_browsers_tests(browser_id, test_id)
The Relationsships that are already in the middle-table can be read.
The result of $this->Test->findById($id) is:
Array(
[Test] => Array
(
[id] => 94
[name] => NM Test 2
[description] =>
[created] => 2015-03-22 18:54:41
[modified] => 2015-03-22 19:53:28
)
[Browser] => Array
(
[0] => Array
(
[id] => 2
[name] => Firefox
[height] => 768
[width] => 1366
[active] => 1
)
)
)
The Model:
class Test extends AppModel {
public $actsAs = array('Containable');
public $hasAndBelongsToMany = array(
'Browser' => array(
'className' => 'Browser',
)
); ...
The controller-function:
public function add() {
$this->set('testDomains', $this->Test->DomainSchedule->TestDomain->find('all'));
$this->set('browsers', $this->Test->Browser->find('list'));
if ($this->request->is('post')) {
$this->Test->create();
$newTest = $this->request->data;
print "<pre>"; print_r($newTest); print "</pre>";
if ($this->Test->saveAll($newTest)) {
if($this->scheduleAutoTestRun($this->Test->id)){
$this->Session->setFlash(__('Your test has been saved. Additionally it has been added to the queue.'));
}else{
$this->Session->setFlash(__('Your test has been saved.'));
}
//return $this->redirect(array('action' => 'index'));
}else{
$this->Session->setFlash(__('Unable to add your test.'));
}
}
}
The View:
...
<?php echo $this->Form->input('Browser',array('multiple'=>'checkbox'));?>
...
The Content of $this->request->data
Array(
[Test] => Array
(
[name] => NM Test
[description] =>
)
[Browser] => Array
(
[Browser] => Array
(
[0] => 2
)
)
So, as far as I've been reading, I understand that this is the way it should look like. Unfortunately it is not saving the relationship that Browser with id=2 is connected to the new created Test.
Could anybody give me a hint what I am doing wrong?
Thanks!
I believe this needs the param deep as in $this->Test->saveAll( $data, array('deep' => true) )
I have two models in HABT relation.Doctor and Patience.
I'm trying to create an edit form for Patient inside the Doctors controller.When I'm in Doctor controller I want to get all data of the Patients (not related Patients of Doctor )and its related Doctor. I tried the code below but it returns only Patient not its related data.
$options = array('recursive' => 2,'conditions' => array('Patience.' .$this->Doctor->Patience->primaryKey => $id));
$this->request->data = $this->Doctor->Patience->find('first', $options);
This only getting Patience model data not its related data.
Array ( [Patience] => Array ( [username] => akhil [password] => 93477851451f6583c7efd383e715cb6f8accb804 [id] => 4 [name] => akhil [age] => 33 [address] => w4ewrf [cellno] => 324323 [email] => awad#gmail.com [medicines] => sd [diseases] => sdf [misc] => sdf ) [Doctor] => Array ( ) )
I have 3 users tables on my DB:
Users
users_twitter
users_web
Users is the parent of both users_twitter and users_web.
And then another table for comments. Both kind of users can comment.
So, when I want to show the comments I need to get information of the author of each comment.
In order to do this I am using the variable $belongsTo on the /Model/Comment.php like this:
public $belongsTo = array(
'users' => array(
'foreignKey' => 'idUser'
)
);
In Controller/CommentController, when i do this:
public function index($idPost) {
$this->Comment->recursive = 0;
return $this->Comment->find('all');
}
I get this kind of array:
[0] => Array
(
[Comment] => Array
(
[id] => 1
[idPost] => 441
[idUser] => 387371023
[comment] => hello word
[created] => 2012-03-01 00:00:00
)
[User] => Array
(
[id] => 1
[username] => myname
)
)
And what I want is to get more info yet, the one of the subtype of Users, depending on the existence of the ID on the subtype tables.
In case it was an id of a user_web table, I would like to get this:
[0] => Array
(
[Comment] => Array
(
[id] => 1
[idPost] => 441
[idUser] => 387371023
[comment] => hello word
[created] => 2012-03-01 00:00:00
)
[User] => Array
(
[id] => 1
[username] => myname
[users_web] => Array
(
[id] => 1
[username] => myName
[password] => XXXXX
[verified] => 1
[created] => 1
)
)
Do you know if that's possible with CakePHP?
As far as I know, with $hasOne I only get one level more, I need two.
In your AppModel add
public $actsAs = array('Containable');
In your index:
public function index($idPost) {
return $this->Comment->find('all', array(
'contain' => array('User.UsersWeb')));
}
Your User model has to be associated with a UsersWeb model to make this work.
See http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html#containing-deeper-associations for a complete description.
Also why do you return the data in the controller index? Are you using requestAction for this? And why don't you use pagination? Do you really want to display hundreds of comments on one page?
See http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html
I have a page for editing records of the Venue model in my app. This page was working at some stage, but is now broken.
in the controller action, debugging $this->data gives the expected array of form values. However, in the Venue model, debugging $this->data in beforeSave gives only the values for fields from a related (HABTM) model, Category:
app/models/venue.php (line 89)
Array
(
[Category] => Array
(
[Category] => Array
(
[0] => 1
[1] => 2
[2] => 8
)
)
)
What could be happening to this data between the form being submitted to the controller action, and the call to beforeSave? Where should I be looking to debug this?
THanks
Edit - here's what's in $this->data in the controller (actual data changed to remove phone numbers, addresses etc).
app/controllers/venues_controller.php (line 63)
Array
(
[Venue] => Array
(
[id] => 19
[city_id] => 1
[user_id] => 130
[name] => Acme Zoo
[email] => events#acmezoo.org.uk
[description] =>
Some text...
[blurb] => Truncated description...
[contact_id] =>
[address_1] => Acme Zoo
[address_2] => Some Road
[postcode] => PP9 4DD
[telephone] => 010101010101
[website] =>
[latitude] => 55.21222
[longtitude] => -2.111111
[featured] => 0
[active] => 1
[flagged] => 0
[smg] => 0
[smg_custom_icon] => 1
[listings] => 1
[send_email] => 0
[file] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)
[Category] => Array
(
[Category] => Array
(
[0] => 3
[1] => 6
[2] => 10
)
)
)
And here's my code to save the data...
if (!empty($this->data)) {
if ($this->Venue->save($this->data)) {
$this->Session->setFlash('The venue has been saved','success');
$countryId = $this->Venue->City->field('country_id',array('id'=>$this->data['Venue']['city_id']));
if (!empty($this->data['Venue']['send_email'])){
$this->_emailVenue($this->Venue->id,'venue_added',$countryId);
}
$this->redirect(array('action' => 'index','city'=>$this->data['Venue']['city_id']));
} else {
$this->Session->setFlash('The venue could not be saved. Please, try again.','failure');
}
}
I think i found a solution to this but I am really unsure if this should be considered a "good" solution.
I backup the request data before the save and then restore it if it fails.
$temp = $this->request->data;
if ($this->Post->save($this->request->data)) {
}else{
$this->request->data = $temp;
}
Maybe a stupid question, but do you pass the content of controller $data to the model when you call the save() method ?
$this->Venue->save($this->data)
Are you trying to save an entry to the categories table at the same time? If so, you can use $this->Venue->saveAll($this->data) instead of save(). If you just want to save the Venue data, just pass that in to save() instead of the entire $this->data like this: $this->Venue->save($this->data['Venue']);
I got problem when save belongto
class Script extends AppModel {
var $name = 'Script';
var $belongsTo = array(
'ScriptCatagories' => array(
'className' => 'ScriptCatagories',
'foreignKey' => 'script_catagories_id',
function add_script() {
pr($this->data);
if (!empty($this->data)) {
$this->Script->create();
$this->data['Script']['script_catagories_id'] = $this->Script->scriptCategory->id;
$this->data['Script']['user_id'] = $this->Auth->user('id');
if ($this->Script->save($this->data)) {
$this->Session->setFlash(__('The script has been saved', true));
$this->redirect(array('action' => 'script_index'));
} else {
$this->Session->setFlash(__('The script could not be saved. Please, try again.', true));
}
}
pr($scriptCatagories = $this->Script->ScriptCatagories->find('list'));
$this->set(compact('scriptCatagories'));
}
when click save
Array
(
[Script] => Array
(
[name] => fdfvvds
[description] => dfvdfvdfvdf
[tags] =>
)
)
Array
(
[0] => performance
[1] => cleanup
)
question in
$this->data['Script']['script_catagories_id'] = $this->Script->scriptCategory->id;
I need to save categoryid to script table but I don't know to get it
thank you
var $belongsTo = array(
'ScriptCatagories' => array(
'className' => 'ScriptCatagories',
'foreignKey' => 'script_catagories_id',
first, it doesn't have close parentheses. And it should be singular ScriptCategory, also the foreign key script_category_id (just to follow the convention). And you misspelled category (you have $this->Script->scriptCategory->id; later in the code)