I am trying to save multiple data at once, but cannot save data.
I have model of Post and model of Comment.
Post has many comments.
$this->WhAreaAllocation->saveAll($tempArray, array('deep' => true));
$tempArray is as below :
array(
'post' => array(
(int) 0 => array(
'title' => 'title1',
'content' => 'content1',
'comment' => array(
(int) 0 => array(
'comment'=>'1st comment for post 1'
),
(int) 1 => array(
'comment'=>'2nd comment for post 1'
)
)
),
(int) 1 => array(
'title' => 'title2',
'content' => 'content2',
'comment' => array(
(int) 0 => array(
'comment'=>'1st comment for post 2'
),
(int) 1 => array(
'comment'=>'2nd comment for post 2'
)
)
)
)
)
Saving hasMany through data
I don't know that you are saving post comments using form and also i am not sure about that which version of cakephp you are using but my solution is for cakephp2.x .If you wanna save data using form then create your post form like this and for saving has many mentioned related model name in fields.For example here you want save related comment of post see the code below
<?php echo $this->Form->create('Post'); ?>
<?php echo $this->Form->input('Post.title'); ?>
<?php echo $this->Form->input('Post.content'); ?>
<?php echo $this->Form->input('Comment.comment'); ?>
<button type="submit">Save</button>
<?php echo $this->Form->end(); ?>
Using this form u will get the request data like this.
Array
(
[0] => Array
(
[Post] => Array
(
[title] => It is a post title
[content] => you post content
)
[Comment] => Array
(
[comment] => this is my post comment
)
)
)
or if you wanna save data with out using form then u have to create data like this and also see CakePhp Save hasmany
Array
(
[0] => Array
(
[Post] => Array
(
[title] => It is a post title
[content] => you post content
)
[0] => Array
(
[Comment] => Array
(
[comment] => this is my post comment1
)
)
[1] => Array
(
[Comment] => Array
(
[comment] => this is my post comment2
)
)
)
)
Related
I have two controllers: events and results. Events hasMany results, results belongsTo Events. I can save just fine, but when I go to edit, I can only get the information for the Event part of the form, to come in automagically.
I build the Results form info like this:
$option_number = 5;
for ($i = 0; $i < $option_number; $i++) {
echo $this->Form->select("Result.{$i}.object_id", $qual_options, array('empty' => false, 'class' => 'result-name'));
echo $this->Form->hidden("Result.{$i}.id");
echo $this->Form->hidden("Result.{$i}.type", array('value' => 'qual'));
echo $this->Form->hidden("Result.{$i}.action", array('value' => 'add')); ?>
}
In the backend, when I'm doing this to get the automagic population:
if ($this->request->is('get')) {
$this->request->data = $this->Event->findById($id);
}
It works just fine, but I can't figure out how to get it to show the Results. I've tried many things, most probable being:
$this->request->data['Result'] = $this->Result->findAllByEventId($id);
With that, I end up with a data structure like:
[Result] => Array
(
[0] => Array
(
[Result] => Array
(
[id] => 1
[object_id] => 1
[type] => qual
[action] => add
[amt] => 10
[event_id] => 1
)
)
[1] => Array
(
[Result] => Array
(
[id] => 2
[object_id] => 2
[type] => qual
[action] => add
[amt] => 1
[event_id] => 1
)
)
... etc.
)
)
Which definitely looks fishy, I just can't seem to manipulate it to work.
UPDATE I should have mentioned this; this is what my data looks like when I SAVE it, and I want to mimic this!
[Result] => Array
(
[0] => Array
(
[object_id] => 1
[type] => qual
[action] => add
[amt] => 0
[event_id] => 3
)
[1] => Array
(
[object_id] => 1
[type] => qual
[action] => add
[amt] => 1
[event_id] => 3
)
You can see that each numeric key after just has the information in it; instead, my numeric keys ALSO have an array INSIDE them name Result, and I have no idea how to make that go away properly! :} I could always loop through and build it in the format CakePHP wants, but I want to do it properly. And that single line above is what needs changing, but I have run out of ideas.
What about just using find('first') against the event? Since it's hasMany, it will return the Result model in a single [Result] key with many numeric keys.
$this->request->data = $this->Event->find('first', array(
'conditions' => array(
'Event.id' => $id
),
'contain' => array(
'Result'
)
));
This will return something like:
array(
'Event' => array(
'id' => 1,
'name' => 'event name'
),
'Result' => array(
0 => array(
'id' => 1,
...
),
1 => array(
'id' => 2,
...
)
)
);
You could unset the Event key if you needed to.
From the CakePHP manual:
Let's say you had a belongsTo relationship between Posts and Authors. Let's say you wanted to find all the posts that contained a certain keyword ("magic") or were created in the past two weeks, but you want to restrict your search to posts written by Bob:
array (
"Author.name" => "Bob",
"OR" => array (
"Post.title LIKE" => "%magic%",
"Post.created >" => date('Y-m-d', strtotime("-2 weeks"))
)
)
However, its not working for me. I've got Scrapes belongsTo Vargroup, and Vargroup hasMany Scrape. My scrapes table has a field vargroup_id
When I do:
$this->Vargroup->contain('Scrape');
$this->Vargroup->find('first');
I get:
Array
(
[Vargroup] => Array
(
[id] => 16950
[item_id] => 1056
[image] => cn4535d266.jpg
[price] => 22.95
[instock] => 1
[timestamp] => 2012-10-29 11:35:10
)
[Scrape] => Array
(
[0] => Array
(
[id] => 18163
[item_id] => 1056
[vargroup_id] => 16950
[timestamp] => 2012-05-23 15:24:31
[instock] => 1
[price] => 22.95
)
)
)
But when I do :
$this->Vargroup->contain('Scrape');
$this->Vargroup->find('first', array(
'conditions' => array(
'not' => array(
array('Scrape.timestamp >' => $today)
)
)
));
Im getting The following error with associated sql output
Warning (512): SQL Error: 1054: Unknown column 'Scrape.timestamp' in 'where clause'
Query: SELECT `Vargroup`.`id`, `Vargroup`.`item_id`, `Vargroup`.`image`, `Vargroup`.`price`, `Vargroup`.`instock`, `Vargroup`.`timestamp` FROM `vargroups` AS `Vargroup` WHERE `Scrape`.`timestamp` = '2012-10-29
It doesn't look like its binding the table at all..
Any help would be appreciated.
Thanks
Try with this:
$this->Vargroup->contain('Scrape');
$this->Vargroup->find('first', array(
'conditions' => array(
'not' => array(
'Scrape.timestamp >' => array($today)
)
)
));
i think this examples from cakephp page tell the right way to do a "not" condition,
check it out.
array(
"NOT" => array("Post.title" => array("First post", "Second post", "Third post"))
)
this is the complex find conditions page if you have more questions. Cakephp complex find conditions
I'm trying to save IssueHistoryDescription, which belongsTo IssueHistory.
So IssueHistory hasMany IssueHistoryDescription. This has all been set in the model.
Yet, when I save this in IssueHistory, using $IssueHistory->save($data);
(With or without a $IssueHistory->create(); before...)
Array
(
[IssueHistory] => Array
(
[id] => 22
)
[IssueHistoryDescription] => Array
(
[old_description] => OLD
[description] => NEW
)
)
It doesn't work, nothing is saved.
When I try to use saveAssociated() I get an error:
Fatal error: Cannot use string offset as an array in /var/www/xdev/kipdomanager/cakephp/lib/Cake/Model/Model.php on line 2248
You can try this:
$data = array(
'IssueHistory' => array('id' => 2),
'IssueHistoryDescription' => array(
array('old_description' => 'OLD', 'description' => 'new')
)
);
$IssueHistory->create();
$IssueHistory->saveAll( $data );
Hi Im just wondering if it is possible to save multiple data that has hasMany: and if it is possible what is the array structure before executing $this->Model->saveAll($this->data).
example is that you are going to save multiple posts at a time like this:
array(
[Post] => Array
(
[0] => Array
(
[title] => title One
[content] => desc One
)
[1] => Array
(
[title] => title two
[content] => desc two
)
)
So in the given array above we can save all the Post with saveAll but what if each Post have hasMany comment each. how the array should look like if i have to insert the array below:
array(
[Comment] => Array
(
[0] => Array
(
[comment] => 1st Comment for Post One
)
[1] => Array
(
[comment] => 2nd Comment for Post One
)
[2] => Array
(
[comment] => 1st Comment for Post Two
)
[3] => Array
(
[comment] => 2nd Comment for Post Two
)
)
How can I combine the two array to execute saveAll();
Thanks in advance. ^_^
Assuming the association for "post has many comments" is called "Comments", the data would look something like
array(
'Post' => array(
array(
'title' => 'title1',
'content' => 'content1',
'Comments' => array(
array('comment'=>'1st comment for post 1'),
array('comment'=>'2nd comment for post 1'),
),
array(
'title' => 'title2',
'content' => 'content2',
'Comments' => array(
array('comment'=>'1st comment for post 2'),
array('comment'=>'2nd comment for post 2'),
),
),
),
)
To save you could use something like:
$this->Model->saveMany($data, array('deep'=>TRUE));
Note that the "deep" option requires CakePHP 2.1. Without it the associated Comment records would not be saved.
All this is documented in http://book.cakephp.org/2.0/en/models/saving-your-data.html
I am trying to implement selectbox in my site.Option for that selectbox is set in my controller like below
$tmp_user = $this->User->find('first',array('id'=>$this->Auth->user('id')));
$zip_info = $this->Zipcode->find('first',array('id'=>#$tmp_user['User']['zip_id']));
$region_admins = $this->AdminRegion->find('all',array('conditions'=>array('AdminRegion.region_id'=>#$zip_info['Zipcode']['region_id'])));
if(!empty($region_admins)){
foreach($region_admins as $radmn):
//pr($radmn);
$admin_user = $this->User->find('list',array('conditions'=>array('id'=>$radmn['AdminRegion']['user_id']),'fields'=>array('id','username')));
pr($admin_user);
$this->set('users',$admin_user);
endforeach;
I am getting value like this when i print from controller
Array
(
[137] => governmentuser1
)
Array
(
[198] => testadmin
)
Array
(
[215] => adminregion
)
Array
(
[224] => testcompany1234
)
Array
(
[225] => testuser12345678
)
but only last value is set in select box....
Where did i made mistake?
How about something like this:
<?php
$tmp_user = $this->User->find('first', array(
'id' => $this->Auth->user('id')
));
$zip_info = $this->Zipcode->find('first', array(
'id' => #$tmp_user['User']['zip_id']
));
$region_admins = $this->AdminRegion->find('list', array(
'conditions' => array(
'AdminRegion.region_id' => #$zip_info['Zipcode']['region_id']
),
'fields' => array('AdminRegion.user_id', 'AdminRegion.user_id')
));
$admin_users = $this->User->find('list', array(
'conditions' => array(
'id' => $region_admins
),
'fields' => array(
'id','username'
)
));
?>
A few side notes:
Why do another query for the user data? Isn't it all in the session?
I wouldn't use the # anywhere, it's slower and suppresses errors. It will make debugging a nightmare too.
I think you missed array:
$admin_user[] = $this->User->find('list',array('conditions'=>array('id'=>$radmn['AdminRegion']['user_id']),'fields'=>array('id','username')));
Hope it helps