SugarQuery build up queryOr in a for loop - sugarcrm

I am trying to build up a queryOr based on a comma separated list that is passed in by the user that I will loop over and add the values.
So far I got this which just builds it:
$query = new SugarQuery();
$query->from(BeanFactory::getBean('rdwrk_Request'));
$skills = $query->join('rdwrk_request_skills_rdwrk_request', array('alias' => 'skills'))->joinName();
$query->select(array(array('name', 'requestName'), array('skills.name', 'skillName')));
if($args["requestName"])
{
$requestName = $args["requestName"];
if(strpos($requestName, ',') !== false)
{
$requestNames = explode(",", $requestName);
foreach($requestNames as $name)
{
$query->where()->queryOr()->contains('name', $name);
}
}
else
{
$query->where()->contains('name', $requestName);
}
}
if($args["skillName"])
{
$query->where()->contains('skills.name', args["skillName"]);
}
$results = $query->execute();
Is there any way to build it so all the values I loop over go into the same queryOr?

You can set the current SugarQuery object into its own variable, and then feed it through your loop as follows:
$currentQuery = $query->where()->queryOr();
foreach($requestNames as $name)
{
$currentQuery->contains('name', $name);
}
This calls the contains function on the same query object with the queryOr established. Since the $currentQuery variable references the same SugarQuery object, running $query->execute() later on will include the parameters.

Related

How to fetch data as array in codeigniter

I am fetching data from the database and the result is multiple rows but it is only showing 1st row in echo. please let me know where is the problem so that I can get all matched results.
public function review_email()
{
$date= date("Y-m-d");
$this->load->model("site_model");
$query = $this->db->get_where('review_email', array("date"=>$date));
$row = $query->row();
if (isset($row))
{
echo $name=$row->name;
}
}
Try This --
public function review_email()
{
$date= date("Y-m-d");
$this->load->model("site_model");
$query = $this->db->get_where('review_email', array("date"=>$date));
//$row = $query->row();
$query->result();
$row = $query->row_array();
foreach ($row as $c)
{
echo $c->name;
}
}
Your code $query->row(); means one row as an object, you also have $query->result(); that will give you all the results as an object too, then $query->row_array(); will give you one as an array, finally you have $query->result_array(); which will give you all the results as an array.

Access an array item in Perl after generating that array

Hello I want to access an specific array item based in a condition previously checked. I leave the code here:
elsif (scalar(#{$boss->bosses}) > 1) {
foreach my $pa (#{$boss->bosses}) {
my $p = My::Model::Group->new(id => $pa->group_id);
push(#$groups, $p);
$valid_pass = 1 if ($pa->checkPassword($self->param('password')));
}
if ($valid_pass) {
my $pa_id = $pa->id;
my $pa_partner_id = $pa->group_id;
}
else {
}
}
What I want to do is, if that if in the array that comes, I check if the password is correct, so if it's correct, then I want to take the id and the group_id of the array item to use it in a function to be able to log them in.
Your for loop is doing two things at once: producing a list of My::Model::Group objects in #$groups, and finding the first boss whose password checks out.
I suggest that you split them up into two clear operations, and the List::Util modules first operator is ideal for the second task
Here's how it would look. I've extracted the result of the method call $boss->bosses into a variable $bosses to avoid repeated calls to the method
Note that you don't need to apply scalar to an array when checking its size. The > and all the other comparators impose scalar context anyway
I've taken much of my code from your question, and I'm a little concerned that you extract values for $pa_id and $pa_partner_id and then just discard them. But I imagine that you know what you really want to do here
use List::Util 'first';
my $bosses = $boss->bosses;
if ( ... ) {
...;
}
elsif ( #$bosses > 1 ) {
#$groups = map { My::Model::Group->new( id => $_->group_id ) } #$bosses;
my $password = $self->param( 'password' );
my $pa = first { $_->checkPassword( $password ) } #$bosses;
if ( $pa ) {
my $pa_id = $pa->id;
my $pa_partner_id = $pa->group_id;
}
else {
...;
}
}

Filemaker: No records match the request

I am newbie with filemaker. I am trying to set search function but something wrong and it returns No records match the request even if it is present there. Here is code
public function get_row($table, $search='')
{
$layout_object = $this->fm->getLayout($table);
if (FileMaker::isError($layout_object)) {
return array();
}
$request = $this->fm->newFindCommand($table);
if ($search)
{
$request->addFindCriterion($search['key'], 'hh#kkk.nn'); // hardcoded.
}
$result = $request->execute();
if (FileMaker::isError($result)) {
echo $result->getErrorString();
}
//.....Result: No records match the request
}
what I am doing wrong?
You need to escape the # symbol as it's a special character in Find mode to match any one character, so try this:
$request->addFindCriterion($search['key'], 'hh\#kkk.nn'); // hardcoded.

Is this a valid way to check if db_row exists?

I am working with Zend and I needed to check whether a row in the DB already exists (A simple solution to get rid of the duplicate key error I was getting). I tried several things but nothing seemed to work... (for example the Zend_Validate_Db_NoRecordExists method)
So I wrote the following the code and I was wondering if this is a valid way to do it, or if I should do things differently:
In the model:
$where = $condition = array(
'user_id = ' . $user_id,
'page_id = ' . $page_id
);
$check = $this->fetchRow($where);
if(count($check) > 0) {
return null;
}else{
// Here I create a new row, fill it with data, save and return it.
}
And then in my view:
if($this->result != null) { /* do stuff */ }else{ /* do other stuff */ }
It does work but it does seem to take more time (duh, because of the extra query) and I am a bit unsure whether I should stick with this..
Any recommendation is welcome :)
Assuming you have coded your function in your controller
$row = $this->fetchRow($where); //If no row is found then $row is null .
if(!$row)
{
$row = $dbTb->createNew($insert); //$insert an associative array where it keys map cols of table
$row->save();
$this->view->row_not_found = true;
}
return $row;
In your view you can do this
if($this->row_not_found)
{
}else {
}

How can I set the order of Zend Form Elements and avoid duplicates

In Zend Form, if two elements have the same order, then Zend will totally ignores the second element (instead of displaying it under the first). Take the following code as an example. Notice that the City and Zip Code elements have the same order of 4
$address = new Zend_Form_Element_Textarea('address');
$address->setLabel('Address')
->setAttrib('cols', 20)
->setAttrib('rows', 2)
->setOrder(3)
;
$city = new Zend_Form_Element_Text('city');
$city->setLabel('City')
->setOrder(4)
;
$postal = new Zend_Form_Element_Text('postal');
$postal->setLabel('Zip Code')
->setOrder(4);
When this form renders, the Zip Code element is nowhere to be found.
If I want to set elements like a buttons dynamically, but tell it to render at the end of the form, how would I do this and not run into the problem of having two elements with the same order?
public function addSubmitButton($label = "Submit", $order = null)
{
$form_name = $this->getName();
// Convert Label to a lowercase no spaces handle
$handle = strtolower(str_replace(" ","_",$label));
$submit = new Zend_Form_Element_Submit($handle);
$submit->setLabel($label)
->setAttrib('id', $form_name . "_" . $handle)
;
///////// Set the button order to be at the end of the form /////////
$submit->setOrder(??????);
$this->addElement($submit);
}
If you really need to use the setOrder() method, I'd work with order numbers 10, 20, 30, 40, ... This way it will be easy to add elements in between already set Elements.
Furthermore, in order to avoid using order-numbers twice, you could use an array, where you store all the numbers from 1 to X. Whenever you set an order number, you set it via a method called getOrderNumberFromArray() which returns the next higher or lower order number still available in the array and unsets this array element.
Alternatively, and maybe even better, you could do getOrder() on the element you want to have before the new element, then increment this order number by X and then loop through the existing form elements and check that the order number doesn't exist yet.
Or you could just use getOrder() on the Element you want to show before and after the new element and make sure you don't use the same order numbers for the new element.
Sorry to be late to the question. What I did was extend Zend_Form and override the _sort() method as follows:
/**
* Sort items according to their order
*
* #return void
*/
protected function _sort()
{
if ($this->_orderUpdated) {
$items = array();
$index = 0;
foreach ($this->_order as $key => $order) {
if (null === $order) {
if (null === ($order = $this->{$key}->getOrder())) {
while (array_search($index, $this->_order, true)) {
++$index;
}
$items[$index][]= $key;
++$index;
} else {
$items[$order][]= $key;
}
} else {
$items[$order][]= $key;
}
}
ksort($items);
$index = 0;
foreach($items as $i=>$item){
foreach($item as $subItem){
$newItems[$index++]=$subItem;
}
}
$items = array_flip($newItems);
asort($items);
$this->_order = $items;
$this->_orderUpdated = false;
}
}
This differs from the original sort method by putting the items in an array based off of their index and then doing a depth-first traversal to flatten the array.
Try this code:
$elements = array();
$elements[] = new Zend_Form_Element_Textarea('address');
......
$elements[] = new Zend_Form_Element_Text('city');
.......
$elements[] = new Zend_Form_Element_Submit($handle);
.....
$this->addElements($elements);
All you need to do is add them in the order you want them to show
what i would do is - use a temp array for that - in that keep the element names in desired order (don't mind the keys). Then use foreach like this:
foreach(array_values($tempArray) as $order => $name) {
$form->$name->setOrder($order+1);
}
Note the array_values - it will return the values as numbered array ;) Not sure if setOrder(0) works - that's why there is +1