In my controller, i fetched the values from db using doctrine in array format. The array look like this
Array(
[0] => Admin_Model_prod Object
(
[_id:protected] => 1
[_pname:protected] => mobile
[_categoryname:protected] => device
[_price:protected] => 10000
[_status:protected] => sold
)
[1] => Admin_Model_prod Object
(
[_id:protected] => 2
[_pname:protected] => tv
[_categoryname:protected] => device
[_price:protected] => 50000
[_status:protected] => sold
)
)
now i want to display the values with id=1..
any suggestion pls
$userId = Doctrine_Query::create()
->select('o.authorId')
->from("Offer o")
->where("o.id =$offerId")
->fetchArray();
Use your Query like this. you will get an array instead of object
Related
I have been trying and trying a lot but i just don't understand how to achieve this .. Here is what i want to achieve by the way..
I am using cakephp 3.0 and i am trying to append $this->request->data by trying below code in my controller...
$this->data['Leaveregisters'] = array(
'name'=>'another',
'surname'=>'whatever'
);
echo '<pre>';
print_r($this->request->data);
exit;
but i am keep getting below error
Notice (8): Indirect modification of overloaded property App\Controller\LeaveregistersController::$data has no effect
However, i am able to append data using below code
$this->request->data['name'] = 'another';
$this->request->data['surname'] = 'whatever';
But i am just wondering why i am not able to achieve the same thing using array() or what should i do to achieve this ?
Array should be appened in the same array as well if i do $this->request->data currently if i use below code
$this->request->data('Leaveregisters', [
'name' => 'another',
'surname' => 'whatever asdfa'
]);
echo '<pre>';
print_r($this->request->data);
exit;
Its getting appeneded but with new key like below which i do not want
Array
(
[leaveregister_user_id] => 2
[leaveregister_num_leaves] => 15
[leaveregister_sdate] => 2016-09-01
[leaveregister_edate] => 2016-09-22
[leaveregister_leave_type] => 1
[leaveregister_status] => 1
[leaveregister_created_date] => 2016-09-20 14:54:29
[leaveregister_modified_date] => 2016-09-20 14:54:29
[Leaveregisters] => Array
(
[name] => another
[surname] => whatever asdfa
)
)
I want something like this instead...
Array
(
[leaveregister_user_id] => 2
[leaveregister_num_leaves] => 15
[leaveregister_sdate] => 2016-09-01
[leaveregister_edate] => 2016-09-22
[leaveregister_leave_type] => 1
[leaveregister_status] => 1
[leaveregister_created_date] => 2016-09-20 14:54:29
[leaveregister_modified_date] => 2016-09-20 14:54:29
[name] => another
[surname] => whatever asdfa
)
So that i can just save my data using $this->request->data
Can someone guide me for the same ?
Thanks
There is no Controller::$data property anymore in CakePHP 3.
Cookbook > Appendices > 3.X Migration Guide > 3.0 Migration Guide > Controller
The only correct way is to access the request object, and of course you can set nested data if you want to the same way, ie:
$this->request->data['Leaveregisters'] = [
'name' => 'another',
'surname' => 'whatever'
];
Alternatively use the data() method:
$this->request->data('Leaveregisters', [
'name' => 'another',
'surname' => 'whatever'
]);
See also
Cookbook > Request & Response Objects > Request Body Data
API > \Cake\Network\Request::data()
I am saving form data in a Session, and trying to save to a model called Property using the session array. Please see the array below. I think it has something to do with the Session array but I am not sure.
When I try to save like so, it does not save:
$this->Property->save($propertyData) where $propertyData is the property array.
sql_dump:
INSERT INTO `fra`.`properties` (`type`, `address`, `city`, `state`, `zip`, `price`, `bed_rooms`, `bath_rooms`, `lot_size_sq_ft`)
VALUES ('0', '2720 Acapulco way', 'modesto', 'ca', '95355', 310000, 4, 3, 6040)
The session array is:
Array
(
[house_details] => Array
(
[form] => Array
(
[section] => house_details
)
[Property] => Array
(
[type] => 0
[address] => 2720 Acapulco way
[city] => modesto
[state] => ca
[zip] => 95355
[price] => 310000
[prop_year_build] => 2007
[prop_year_remodel] =>
[bed_rooms] => 4
[bath_rooms] => 3
[garage_spaces] => 3
[lot_size_sq_ft] => 6040
[house_size_sq_ft] => 3720
[stories] => 2
[condition_rating] => 8
)
You should be able to do this quite simply. You probably have a problem with $propertyData being set as the wrong thing so it doesn't provide a valid value for the model save.
What do you get if you do debug($propertyData)?
Does this work?
$propertyData = $this->Session->read('house_details.Property');
$this->Property->save(propertyData);
Is it possible to update existing data and save new data at the same time without having to loop through the array? I would like the array with the id to update and the array without an id to create a new entry. See the example below. Thank you for your help.
[Workload] => Array
(
[0] => Array
(
[phase_count] => 1
[id] => 17
[value] => {"phases":[{"rep_range":"20-30","rep_set_count":"1"}]}
[user_id] => 1
)
[4] => Array
(
[phase_count] => 1
[value] => {"phases":[{"rep_range":"20-30","rep_set_count":"1"}]}
[user_id] => 1
)
);
and then this
$this->Workload->saveAll($this->data['Workload']);
EDIT ======================================
Here is the code that actually saves this array
if($this->data){
array_walk($this->data['Workload'], function (&$value,$index){
// This will need to be changed once users are setup
if(empty($value['user_id'])){
$value['user_id'] = 1;
}
$value['value'] = json_encode($value['value']);
});
debug($this->data);
$this->Workload->saveAll($this->data['Workload']);
In fact, I do exactly that in one of my apps. If there is no 'id' a table entry will be created. If the 'id' is specified, the record will be updated.
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 would like to save several records for one model. This would have been done pretty easily with saveAll() if it hadn't been for a problem:
I have a notification form, where I select multiple users from a <select> and two fields, for subject and content respectively. Now, when I output what $this->data contains, I have:
Array([Notification] => Array
(
[user_id] => Array
(
[0] => 4
[1] => 6
)
[subject] => subject
[content] => the-content-here
)
)
I've read on Cake 1.3 book, that in order to save multiple records for a model, you have to have the $this->data something like:
Array([Article] => Array(
[0] => Array
(
[title] => title 1
)
[1] => Array
(
[title] => title 2
)
)
)
So, how do I 'share' the subject and content to all selected users?
First off, this database design needs to be normalized.
It seems to me like a Notification can have many Users related to it. At the same time, a User can have many Notifications. Therefore,
Introduce a join table named users_notifications.
Implement the HABTM Relationship: Notification hasAndBelongsToMany User
In the view, you can simply use the following code to automagically bring up the multi-select form to grab user ids:
echo $this->Form->input('User');
The data that is sent to the controller will be of the form:
Array(
[Notification] => Array
(
[subject] => subject
[content] => contentcontentcontentcontentcontentcontent
),
[User] => Array
(
[User] => Array
(
[0] => 4
[1] => 6
)
)
)
Now, all you have to do is called the saveAll() function instead of save().
$this->Notification->saveAll($this->data);
And that's the Cake way to do it!
Those values have to be repeat like this
Array([Notification] => Array(
[0] => Array
(
[user_id] => 4
[subject] => subjects
[content] => content
)
[1] => Array
(
[user_id] => 6
[subject] => subject
[content] => contents
)
)
)
$this->Notification->saveAll($data['Notification']); // should work
If you don't pass any column value, this cake will just ignore it
You're going to have to massage your form's output to suit Model::saveAll. In your controller:
function action_name()
{
if ($this->data) {
if ($this->Notification->saveMany($this->data)) {
// success! :-)
} else {
// failure :-(
}
}
}
And in your model:
function saveMany($data)
{
$saveable = array('Notification'=>array());
foreach ($data['Notification']['user_id'] as $user_id) {
$saveable['Notification'][] = Set::merge($data['Notification'], array('user_id' => $user_id));
}
return $this->saveAll($saveable);
}
The benefit here is your controller still knows nothing about your model's schema, which it shouldn't.
In fact, you could probably redefine saveAll in your model, which hands off correctly formatted input arrays to parent::saveAll, but handles special cases itself.
There might be a more cakey way of doing this, but I've used this kind of technique: First, change the form so that you get an array like this:
Array(
[Notification] => Array
(
[subject] => subject
[content] => contentcontentcontentcontentcontentcontent
),
[selected_users] => Array
(
[id] => Array
(
[0] => 4
[1] => 6
)
)
)
(Just change the multiselect input's name to selected_users.id)
Then loop through the user ids and save each record individually:
foreach( $this->data[ 'selected_users' ][ 'id' ] as $userId ) {
$this->data[ 'Notification' ][ 'user_id' ] = $userId;
$this->Notification->create(); // initializes a new instance
$this->Notification->save( $this->data );
}