How to unset field from Document - mongodb

I have one document in which I want to remove one field. I got answer from Here. But, It is for core PHP code and I want in CakePHP how to unset field from document.
I tried following code in CakePHP.
$this->MyModal->id = $id;
$this->MyModal->updateAll(
array('myField' => array('$exists' => true)),
array('unset' => array('myField' => true))
);
Edit
I also tried
$this->MyModal->updateAll(
array('$unset' => array('myField' => 1))
);
But it does not work.How can unset field?

It looks like you're using wrong the update all method. First you must provide an array with the new values you want for your fields and then you should pass an array with the search criteria. So in order for it to work you could write something like
$conditions=array('myField'=>array('$exists'=>true));
$this->MyModal->updateAll(
array('$unset'=>array('myfield'=>1)),
$conditions//array of search conditions
)

Related

add column to select object zend framework

Here is my current code:
$Select=new Select();
$Select->from($this->getTable());
What I want now is to add the id column but as DT_RowId instead of id. How do I accomplish this? The goal would be to have all of the table columns as well as this new column.
If you need both "old" and "new" fields, don't forget to add an asterisk.
$Select=new \Zend\Db\Sql\Select();
$Select->from($this->getTable());
$Select->columns([
'*',
'DT_RowId' => 'id',
'furtherColumn' => 'furtherColumn'
]);
The simplest soloution would be to use the columns function with an associative array with aliases as the keys for example:
$select=new Select();
$select->from($this->getTable());
$select->columns(array(
'DT_RowId' => 'id',
'furtherColumn' => 'furtherColumn',
));

MongoDb replacing a document and inserting when non-existant

I want to replace a document when this already exists and if it doesn't I want it inserted.
How can I do that in mongoDb?
I need something like this, but in one query:
find by a "where statement"
if exists, replace whole document
else, insert
Thank you!
you could also use the save operation, that is much faster than the update (x70 faster from my tests), and is adapt to your purpose, but in case remember to give in input the whole document
Use collection update.
In the example below, the first update call will "insert or replace" the document (including name field from the query). In the second the update call will insert the document or just update Joe's job leaving the rest of the document intact. The difference is the "$set" operation.
<?php
$c->update(
array("name" => "joe"),
array("username" => "joe312", "job" => "Codemonkey"),
array("upsert" => true));
$c->update(
array("name" => "joe"),
array("$set" => array("job" => "Bartender")),
array("upsert" => true));
?>

SugarCRM restriction on custom field

i'm totally new to SugarCRM module development, however i have very good knowledge of PHP, ajax and database programming.
Here is my task:I have been asked to create a restriction on a custom field from the clients module.
There is a custom field called identification number, what i need to do is avoid a new client to be saved into the database based on that field, in other words the client has to be unique. It has to display a pop up window saying "that client already exists"
Copy editviewdefs.php of Accounts to the custom folder and change the custom field definition for id_number to this
array(
'name' => 'id_number',
'displayParams' =>
array (
'field' =>
array (
'onChange' => 'check_is_duplicate(this);',
),
),
),
Create a javascript function check_is_duplicate
function check_is_duplicate(obj) {
// Call a script via Ajax. Pass values for id and id_number with the request.
if (o.responseText > 0) {
alert('duplicate');
document.getElementById('SAVE').disabled = true;
} else {
document.getElementById('SAVE').disabled = false;
}
}
You would have to create the script which would be called via ajax request.
In that script you would have to run a query like -
SELECT COUNT(*) AS count FROM accounts
WHERE deleted = 0 AND id != {$record} AND id_number = {$id_number}
Execute the query and return the count.
Afterwards on saving check for duplicates from server side using the beforeSave logic hook.
You can create a custom duplicate check by the following code:
$dictionary['Account']['duplicate_check']['FilterDuplicateCheck']['filter_template'] = array(
array(
'$and' => array(
array('identification_number' => array('$equals' => '$identification_number')),
),
),
);
Add this code in the file: /custom/Extension/modules/Accounts/Ext/Vardefs/duplicate_check.php.
Then, perform a quick repair/rebuild.
How it works:
When you enter the identification number and press save, SugarCRM will perform a duplicate check. If duplicates are found, SugarCRM will list the duplicates and you can choose to use the duplicate or ignore.

Selecting multiple checking boxes with FormHelper in CakePHP

So in an Index view for a certain model, I'm including some checkboxes; I have an array of days of the week, and so have 7 checkboxes available, per record.
I can save data, no problem. What I can't figure out is how to pre-select the checkboxes, based on each record's saved data. Currently I'm having to do this:
<?php if ( isset($user['SurveyAssignment'][0]['active_days']['Monday']) && $user['SurveyAssignment'][0]['active_days']['Monday'] == 1 ) { $monChecked = true; } else { $monChecked = false; } ?>
<?php echo $this->Form->input('SurveyAssignment.' . $count .'.active_days.Monday', array('type' => 'checkbox', 'label' => false, 'div' => false, 'checked' => $monChecked));?>
And that's just for ONE day, for one record. Right now, I'm doing that 7 times, for each record. It's very bloated.
I would have thought I could just read in the array for each record, and select a group of checkboxes accordingly.
I'm sure I'm missing something very simple but I can't see past my nose at the moment.
If your associations are set up correctly, your field name is correct, and you're passing the correct data, they will be selected automatically for you.
The likely answer is you're not using the correct field or Model.field for your form input.

How to get the value of the form field in drupal 7?

I have added a field on the page_node_form with the following code.
switch($form_id){
case 'page_node_form':
$form['comment_popup'] = array(
'#type' => 'select',
'#title' => t('Comments popup'),
'#options' => array('On' => 'On', 'Off' => 'Off'),
);
}
As the comment form appears on the node so I want when the comment_popup field has the value Off then the subject field should not be displayed in the comment form and when the comment_popup field has the value 'On' then the subject field should be displayed.
I have tried the following code but it did not work.
case 'comment_node_page_form':
if($form_state['comment_popup']['#value'] == 'Off') {
$form['subject']['#access']=FALSE;
}
Any one who can help me?
What you're doing can't work I'm afraid, you're setting an element in one form and then trying to read it from another form which doesn't make sense.
What you need to do is add a submit handler to the page_node_form function which saves the comment_popup value for that node to a custom table. Then in the form alter for `comment_node_page_form' you need to read in the comment_popup data for that particular node, and make the access decisions to the form elements based on that.
Hope that helps