Zend DB Selecting constants - columns that do not exist in table - zend-framework

I'm trying to do this query using Zend DB select but I'm not able to do so
This is the sql query
select shopping_id,shopping_details,"friend" as type
from shopping
Notice here how I'm specifying "friend" as type and friend is not a column in the shopping table.
Now how do I do this in Zend. I have tried this but it gives me an error saying "sh.friend Column does not exist"
$select->from(array('sh'=>'shopping'),array('shopping_id','shopping_details','"friend" as type');
Any help will be appreciated
thanks

Try with Zend_Db_Expr, maybe something like:
$select->from(array('sh'=>'shopping'),
array('shopping_id','shopping_details',
new Zend_Db_Expr('"friend" as type'));

$select->from(
array('sh'=>'shopping'),
array('shopping_id','shopping_details','friend'=>'type', 'alias'=>'column or expression')
);

For Zend Framework 2/3 or Laminas you have to use Laminas\Db\Sql\Expression. Make sure to quote your constant with a double-quote "".
$select->from(['e' => 'experience'])
->columns([
'id' => 'id',
'value' => 'title',
'name' => new Laminas\Db\Sql\Expression('"skill"')
]);
*for Zend Framework the name of the expression class is Zend\Db\Sql\Expression.

Related

Drupal 8 - Entity Reference - Autocomplete - Add fields to search and result in autocomplete

I have an entity called "lawyers."
And another entity refers to lawyers.
The problem is that when searching the reference field with the autocompletion system many repeated names appear:
Pablo
Pablo
Pablo
Pablo
I need the reference field to be able to show the surnames of that person so that it turns out to be
Pablo Martínez
Paglo Gutirerrez
Pablo Iglesias
Pablo López
how can I do this?
You will have to create a Entity Reference View to use as handler for doing the autocomplete lookup. Then you can add additional fields (such as last name) to the autocomplete results. This article outlines that process well enough:
https://www.cmsminds.com/blog/entity-reference-entity-reference-view-in-drupal-8/
If the field is a base field and is not available on the Manage form Display page, you will have to modify the entity class Lawyer::baseFieldDefinitions function. Specifically, you need to change the handler and set the form display settings. In your BaseFieldDefinition::create call:
->setSetting('handler', 'default')
Needs to change to this:
->setSetting('handler', 'views')
->setSetting('handler_settings', [
'view' => [
'view_name' => 'name_of_entity_reference_view',
'display_name' => 'name_of_view_display',
],
])
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 2,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
Alternatively, if you want to make base fields available in the UI, you can use this line to make the field available in the form display settings ui (and then export your form display settings as config:
->setDisplayConfigurable('form', TRUE);

Setting a drupal entity date field without EMW

Is it possible to set a date field (capturing a start and end time) on a drupal entity WITHOUT using entity metadatawrapper?
with EWM I could do (with a corresponding ->save() after, of course)
$wrapper->field_custom_date->set(array(
'value' => $date_start_value,
'value2' => $date_end_value,
));
I've tried
$entity->field_custom_date = array(LANGUAGE_NONE => array(0 => array(
'value' => $date_start_value,
'value2' => $date_start_value,
)));
and a few other very similar variations to no avail. I've tried Googling but I can only find solutions and examples using EMW. Is this possible or do I have to use the entity metadata wrapper?
Thanks!
Try this
$entity->field_custom_date[LANGUAGE_NONE][0]['value'] = $date_start_value;
$entity->field_custom_date[LANGUAGE_NONE][0]['value2'] = $date_end_value;

get_entry_list() method with query parameter on sugar crm 7 does not work

My code is as below..
Please note that I am trying to retrive all the accounts whose where the Accounts.name='bhagya'.
The same query workes for me in SugarCRM Version 6.5 (Community edition) but in case of SugarCRM 7 it is not working when I mention query parameter. If I mention 'query'=>'' then I get all the records from the sugarcrm 7 server. It fails when I specify any filter for query parameter. I am using RestAPI - 4.1
$get_entry_list_parameters = array(
//session id
'session' => $session_id,
//The name of the module from which to retrieve records
'module_name' => 'Accounts',
//The SQL WHERE clause without the word "where".
//'query' => "Accounts.billing_address_postalcode='60329'",
// 'query' => "Accounts.name='Ingrid Rofalsky'",
'query'=>'',
//The SQL ORDER BY clause without the phrase "order by".
'order_by' => "",
//The record offset from which to start.
'offset' => 0,
//A list of fields to include in the results.
'select_fields' => array(
'id',
'name',
),
//A list of link names and the fields to be returned for each link name.
'link_name_to_fields_array' => array(),
//The maximum number of results to return.
'max_results' => 10,
//If deleted records should be included in results.
'deleted' => 0,
//If only records marked as favorites should be returned.
'favorites' => false,
);
print_r($get_entry_list_parameters);
$get_entry_list_result = call('get_entry_list', $get_entry_list_parameters, $url);
echo '<pre>';
print_r($get_entry_list_result);
echo '</pre>';
Can some one help me on this..
Thank you.
Regards
- Bhagya
The Rest API has changed a lot in Sugar7.
The URL for the invokation should look like
http://servname.com/pro720/rest/v10/Accounts?filter[0][name][$starts]=B&filter[0][email_addresses.email_address]=burgers#example.com&fields=name,account_type,description,email
Where you are filtering the accounts with a name that starts with B, the mail address is burgers#example.com and where you retrieve only the fields name,account_type,description and email
Changing the module name to lowercase in query option solved this problem.
I got the answer for my query from below link.
https://community.sugarcrm.com/sugarcrm/topics/get_entry_list_method_with_query_parameter_on_sugar_crm_7_does_not_work?topic-reply-list%5Bsettings%5D%5Bfilter_by%5D=all&topic-reply-list%5Bsettings%5D%5Bpage%5D=1#reply_14588280
Thanks again
Regards
- Bhagya

Cakephp is adding quotes to my query. How to fix this?

Cakephp is adding quotes to my field where i do some postgress functions. I use $this->paginate where the fields array looks like this:
'fields' => array( 'DealRegistration.id',
'DealRegistration.company_name',
'RequestSpecialPrice.total_price',
'RequestSpecialPrice.request_price',
'RequestSpecialPrice.created',
'RequestSpecialPrice.partner_status',
'RequestSpecialPrice.status',
'RequestSpecialPrice.discount',
"**concat('SP',lpad(CAST(RequestSpecialPrice.deal_registration_id as TEXT),8,'0'))**",
)
The query generated by cake looks like this:
SELECT concat('SP',lpad(CAST("RequestSpecialPrice"."deal_registration_id" AS **"TEXT"**),8,'0')) FROM table
If i remove the quotes from "TEXT" then the query works:
SELECT concat('SP',lpad(CAST("RequestSpecialPrice"."deal_registration_id" as **TEXT**),8,'0'))) FROM table
How can i force cakephp not to add those quotes?
L.E.
The solution was to use cake's virtual fields.
I've declared:
"concat('SP',lpad(CAST("RequestSpecialPrice"."deal_registration_id" AS "TEXT"),8,'0')) as aaa"
as a virtual field in the model and just selected aaa as a field.
When using native SQL functions like this, it's usually best to create a virtual field.
http://book.cakephp.org/2.0/en/models/virtual-fields.html
app/Model/RequestSpecialPrice.php
public $virtualFields = array(
'my_virtual_field' => "CONCAT('SP', LPAD(CAST(RequestSpecialPrice.deal_registration_id as TEXT), 8, '0'))"
);
Usage
$this->RequestSpecialPrice->find('first', array(
'fields' => array(
'DealRegistration.id',
'DealRegistration.company_name',
'RequestSpecialPrice.total_price',
'RequestSpecialPrice.request_price',
'RequestSpecialPrice.created',
'RequestSpecialPrice.partner_status',
'RequestSpecialPrice.status',
'RequestSpecialPrice.discount',
'RequestSpecialPrice.my_virtual_field' // Use virtual field
)
);

Customize errors symfony

There are some "best practice" in Symfony to customize form errors?
For exemple, if i would to show "Campo obligatorio" when the field is required.
1)How can i do that better way and independent from what forms call it?
2)How can i customize message 'An object with the same "%namefield" already exist.' ?
Thanks
updated
sorry, but if i try to do 'invalid' how you said me... it print me the same error
$this->setValidator('urlres', new sfValidatorString(array(
'min_length' => 6,
), array(
'min_length' => 'URL must be longer',
'required' => 'Required field',
'invalid' => 'URL exist'
)));
prints me:
* An object with the same "urlres" already exist.
updated
Felix, your solution is fantastic but it prints me this error:
"urlres: that url already exists"
Are there some way to delete "field:" ??
Thanks
Maybe this form post helps you:
Put the code
sfValidatorBase::setDefaultMessage('required', 'Field required');
in the "configure" of you application configuration apps/youApp/config/yourAppConfiguration.class.php.
You should be able to set the default value for every error message type this way.
If you want to set certain error messages for certain fields, think about to create a form class that defines all this and let all other forms inherit from this one.
The subclasses then only specify which fields should be displayed (and maybe custom validation logic).
You can find an example how to do this in the Admin Generator chapter of the symfony book.
This is the cleanest approach IMHO.
Edit:
If you want leave fields blank, you have to add the required => false option:
'email' => new sfValidatorEmail(array('required' => false))
Regarding the error message: This sounds like the urlres is marked as unique in the database table and the value already exists. Maybe you should check the database schema definition.
Edit 2:
To test both, length and uniqueness, you should use sfValidatorAnd and sfValidatorDoctrineUnique:
$this->setValidator('urlres', new sfValidatorAnd(
array(
new sfValidatorString(
array( 'min_length' => 6, ),
array( 'required' => 'Required field',
'min_length' => 'URL must be at least %min_length% chars long.' )
),
new sfValidatorDoctrineUnique(
array( 'model' => 'yourModel',
'column' => 'theColumn',
'primary_key' => 'thePrimaryKeyColumn',
'throw_global_error' => false),
array('invalid' => "That URL already exists")
)
));
Also your use of the invalid error code in the string validator is not correct. You set the invalid message to
URL exists but how can a string validator know this? It only checks whether the given string meets the min_length, max_length criteria or not.
Btw I assumed that you use Doctrine but I think the same validators are available for Propel.
Edit 3:
Set the option 'throw_global_error' => false. But I am not sure if that works.
You can also have a look at the source code if it helps you.
Let me try to help you.
You can easily customize standard form errors in configure method of your form class. Here is an example:
1)
<?php
class myForm extends BaseMyForm
public function configure(){
parent::configure();
$this->setValidator(
'my_field' => new sfValidatorString(
array('min_length'=>3, 'max_length'=>32, 'required'=>true),
array('required' => 'Hey, this field is required!')
)
);
}
2) Just change a message that has a code 'invalid'.
All you need is just find a valid message code to customize particular default messages. More info - Symfony Forms in Action: Chapter 2 - Form Validation
Updated:
And if you don't want to customize error messages in all your form classes, just create your own base validator class:
abstract class myBaseValidator extends sfValidatorBase
and there redefine default 'required' and 'invalid' messages.
Are there some way to delete "field:"
??
Yes: throw_global_error => true