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

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
)
);

Related

Populating a field when choosing a relate field _ suite crm

i'm using suite crm 7.7.5
when i create an opportunity and i choose an account from a relate field, i want a field (country) to be auto populated with the value of the country of the account chosen.
for this, i tried to add the code
$dictionary['Opportunity']['fields']['country_c']['populate_list']= array('id','name','country_c');
$dictionary['Opportunity']['fields']['country_c']['field_list'] = array('account_id_c','account_name','country_c');
in the file \custom\Extension\modules\Opportunities\Ext\Vardefs\sugarfield_country_c.php
knowing that country_c is the name of the column country in the table accounts and the second country_c is the id of the field country in the layout opportunity
but that doesn't work, could someone help me to figure out the reason?
PS : i've tried to follow this tutorial https://developer.sugarcrm.com/2011/08/31/howto-using-a-relate-field-to-populate-a-custom-field/
Here is the extension on #Bozic solution.
If someone is trying to autopopulate related fields based on selection then here is the solution. (Those who are facing "No match for field: Account Name")
Scenario: I have Account Owner (relate to User module) field in Accounts module. And in Cases module I am fetching Account owner fields based on Account selection.
In /custom/modules/Cases/metadata/editviewdefs.php
0 => array(
'name'=>'account_name',
'displayParams' => array (
'field_to_name_array' => array(
'id'=>'account_name',
'assigned_user_name' => 'account_owner_case_c',
'assigned_user_id' => 'user_id2_c',
),
),
),
Note:
assigned_user_name is field id of Account owner from Accounts module
assigned_user_id is id field of Account owner
account_owner_case_cis field of Case Account Owner from Case module
account_name is selection field from case module (on selection of this field account owner will get populate)
You should go to custom/modules/{YOUR MODULE}/metadata/editviewdefs.php and edit editviewdefs.php file.First you need to find array in which your relate field(account_name) is defined. It will look similar to this, maybe with some more parameters.
array (
'name' => 'account_name',
),
Now you need to map data from relate field(country_c) to new field(lets say populated_country_c). After editing your array will look something like this.
array (
'name' => 'account_name',
'displayParams' => array (
'field_to_name_array' => array(
'id'=>'account_id_c',
'name'=>'account_name',
'country_c' => 'populated_country_c',
),
),
),
Now populated_country_c is the new field in which the data about country will be populated when you choose account in the relate field. So we also need to create that new field. You can do it through studio or manually just by adding new array. Finally your file will look like this
array (
'name' => 'account_name',
'displayParams' => array (
'field_to_name_array' => array(
'id'=>'account_id_c',
'name'=>'account_name',
'country_c' => 'populated_country_c',
),
),
),
array (
'name' => 'populated_country_c',
'label'=> 'LBL_POPULATED_COUNTRY'
),
Now when choosing new account from relate field, populated_country_c will be populated with country_c field from selected account.
Use field billing_account_country rather than country_c, and also use account_id with account_name. The name and the id have to coincide to the same table, I believe.

cake php habtm select does not set selected values

Hello I have several troubles with HABTM and Form.
I am using CakePHP 2.3 and PHP 5.3.3
This is my code situation for the understanding of the scenario: Albums and Singles related with a HABTM relation. Albums cointains many Singles and Singles belongs to many Albums.
Model Album.php ( don't care about raisins: is my personal plugin for managing images )
App::uses('Raisin', 'Raisins.Model');
App::uses('PastryBehavior', 'Raisins.Model/Behavior');
class Album extends AppModel
{
public $name = 'Album';
public $actsAs = array(
'Tags.Taggable',
'Utils.Sluggable' => array(
'label' => 'title'
),
'Raisins.Pastry',
'SrEngine.Indexable',
'SrEngine.HitCounter'
);
public $hasOne = array(
'Image' => array('className' => 'Raisins.Raisin')
);
public $hasAndBelongsToMany = array(
'Single'
);
public $belongsTo = array(
'Users.User'
);
}
Controller AlbumsController.php ( edit part )
$this->request->data = $this->Album->read(null, $id);
$this->set('singles',$this->Album->Single->find('list'));
View
<?php echo $this->Form->input('Single',array('class' => 'span12','label' => 'Singoli' ) ); ?>
In the view I see the select populated from the values coming from the controller singles set, but the selected item, previously saved, is not highlighted.
Consider that in the database the habtm table contains the correct row populated with the data saved.
I read , somewehere in the middle, that some previous version of cake, prior to 2.3, used to have some problem related to habtm form select input.
I am very stuck at this because I have almost tried every known workaround without sorting any positive effect.
Any help, hint would be very appreciated.
Thanks in advance
Rudy

Duplicate Validation on Combined Fields in zend form

Hi there I have a table in which combination of three fields is unique. I want to put the check of duplication on this combination. Table looks like
I know how to validate single field, But how to validate the combination is not know. To validate one field I use the following function
public function isValid($data) {
// Options for name field validation
$options = array(
'adapter' => Zend_Db_Table::getDefaultAdapter(),
'table' => 'currencies',
'field' => 'name',
'message'=> ('this currency name already exists in our DB'),
);
// Exclude if a id is given (edit action)
if (isset($data['id'])) {
$options['exclude'] = array('field' => 'id', 'value' => $data['id']);
}
// Validate that name is not already in use
$this->getElement('name')
->addValidator('Db_NoRecordExists', false, $options
);
return parent::isValid($data);
}
Will any body guide me how can I validate duplication on combined fields?
There is no ready to use validator for this, as far as I know. You have either to write your own, or do a check with SQL-query with three conditions (one for each field).
you have to Apply a validation on name element of zend form.
Here is code for add validation on name field.
$name->addValidator(
'Db_NoRecordExists',
true,
array(
'table' => 'currencies',
'field' => 'name',
'messages' => array( "recordFound" => "This Currency Name already exists in our DB") ,
)
);
And you must set required true.

Yii fail to retrieve max column value

I have two models, one is Auction, the other is Bid.
An Auction has many Bids. they are associated by foreign key auction_id in Bid
Now, I want to find the max value of the Bid's price for each Auction.
$dataProvider = new CActiveDataProvider('Auction', array('criteria' => array(
'with' => array(
'bids' => array(
'alias'=>'b',
'group' => 'auction_id',
'select' => 'max(b.price) as maxprice'
)
)
)
)
);
And I have defined a maxprice property in Auction's model class.
However, if I try to retrieve the maxprice property, it returns NULL.
To be more specific, I render the $dataprovider to a view page, it fails to get the maxprice property.
PS:
I executed the query in mysql, the query result turns out to be correct.
So, there must be something wrong with the Yii code
SQL code:
SELECT `t`.`id` , max(b.price) as maxprice
FROM `auction` `t`
LEFT OUTER JOIN `bid` `b` ON (`b`.`auction_id`=`t`.`id`) GROUP BY auction_id
Put the value you want before the relation, like so:
$dataProvider = new CActiveDataProvider('Auction', array('criteria' => array(
'select' => 't.*, max(b.price) as maxprice',
'with' => array(
'bids' => array(
'alias'=>'b',
'group' => 'auction_id',
'together'=>true,
)
You can replace the "t.*" with specific field names if you like.
OR you can simply use the select, join and group attributes on your Auction model and skip the relation altogether.

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

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.